Add middleware from the outside

Open
#2,169 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
ruby
Domain
api, backend

Research direction

Start by tracing the Railtie after_initialize flow and how mounted Grape::API instances build and apply their middleware stack. Compare the effects of insert_after and compile!; the work is done when middleware added externally is invoked for requests to all mounted APIs.

Written by the indexing model from the issue text.

Description

discuss! question you can help

Hey everyone!

Currently iam working on a custom Railtie which wants to add a middleware to all mounted Grape::APIs in a Rails project in the Railties after_initialize block.
My first naive try was MyAPI.send(:insert_after, Rack::Head, MyMiddleware). It doesnt fail and MyAPI.middleware lists MyMiddleware but its not being called when requests come in. It just doesnt work.
So my understanding is that the mounted API is being instantiated before my addition runs and the change is not being picked up by the API.
Ive also experimented with MyAPI.compile! to kind of force a reload of the Grape API. This didnt succeed either. I have also realized that the instantiation process is not that easy to understand! Its quite complex.

What am i missing here?

Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ruby-grape/grape

All issues in ruby-grape/grape

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.