Trigger Rack middleware on specific Rails routes

后端 未结 3 1175
温柔的废话
温柔的废话 2020-12-17 16:50

Is it possible to trigger Rack middleware only on specific Rails routes?

For example, let\'s say I wanted to run a rate limiter middleware only on the api namespace.

3条回答
  •  眼角桃花
    2020-12-17 17:33

    You can also (now) use a Rails Engine to create an isolated set of routes that adds additional middleware to the stack for its mounted routes.

    See https://stackoverflow.com/a/41515577/79079

    (Unfortunately I found this question while looking to see if there was any simpler way to do it. Writing a custom middleware for every middleware that I wanted to add seems even more round-about than using a Rails::Engine)

提交回复
热议问题