rack-middleware

Alter response.body in Rack Middleware

心已入冬 提交于 2019-12-22 08:16:02
问题 I'm trying to write some Rack Middleware for a Rails 4.2 app that alters the response body using the gsub method. I found older examples that use a pattern like this: class MyMiddleware def initialize(app) @app = app end def call(env) status, headers, response = @app.call(env) # do some stuff [status, headers, response] end end What I'm finding is that there is no setter method for response.body . Is there another pattern I can start with to go about modifying the body? 回答1: The problem was

Sinatra rack middleware hijacks '/' root url

只愿长相守 提交于 2019-12-13 12:22:51
问题 I'm trying to use a Sinatra app as middleware in my Rails app. I've tested a basic Sinatra app in the /lib folder of the Rails app, use d the middleware and set a route. That worked fine. What I want to be able to do is extract the Sinatra app and include it as a gem. That way I can run the Sinatra app independently, or use it in multiple Rails apps. Sinatra App # myrackapp/lib/myrackapp.rb module Myrackapp class Application < Sinatra::Base set :root, File.dirname(__FILE__) get "/" do "Rack

Building Rack Middleware responses with Flash message functionality

房东的猫 提交于 2019-12-13 02:24:46
问题 I have a Sinatra app that's mounted on a Rails app under /admin . The Sinatra app is an admin dashboard, and therefore should only be available to authorized users. To enforce that, I built a piece of Rack Middleware that will run before the Sinatra app is called. The logic is simple - If user is authenticated, continue as normal If user is not authenticated, redirect to the root path with a flash alert message (I'm using the rack-flash gem to allow access to the flash messages in Rack) Code

Reloading rails middleware without restarting the server in development

馋奶兔 提交于 2019-12-12 09:35:53
问题 I have a rails 4 app with middleware located at lib/some/middleware.rb which is currently injected into the stack though an initializer like so: MyApp::Application.configure.do |config| config.middleware.use 'Some::Middleware' end Unfortunately, any time I change something I need to restart the server. How can I reload it on each request in development mode? I've seen similar questions about reloading lib code with either autoloading or wrapping code in a to_prepare block but I'm unsure how

What is ActiveSupport::Cache::Strategy::LocalCache used for?

本秂侑毒 提交于 2019-12-10 21:07:15
问题 In my production middleware stack in all environments I see this one-off instance: use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x7f38095d> I've experimented by removing everything I can think of related to caching, but I'm unable to figure out where it comes from. What is it? 回答1: In the abstract, it wraps another cache with an in-memory cache for the duration of a block, and then is cleared after the block. In practice I believe it is used in Rails/Rack to wrap whatever

How to rescue ActionDispatch::ParamsParser::ParseError and return custom API error in rails 5?

安稳与你 提交于 2019-12-10 04:04:47
问题 Whenever sending malformed JSON against my API-only Rails 5.x application, I get an exception and Rails is returning the entire stack trace as JSON. Obviously I'd like to respond with a nicely, custom, formatted error. => Booting Puma => Rails 5.0.0.1 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options Puma starting in single mode... * Version 3.6.0 (ruby 2.3.0-p0), codename: Sleepy Sunday Serenity * Min threads: 5, max threads: 5 *

Adding a custom middleware to Rails 4

风流意气都作罢 提交于 2019-12-10 01:40:06
问题 I have a Rails 4 sample project (Blog) and I have created a simple middleware called 'request_timer' in config/initializers/request_timer.rb #config/initializers/request_timer.rb class RequestTimer def initialize(app) @app = app end def call(env) start_time = Time.now status, headers, response = @app.call(env) stop_time = Time.now [status, headers, response.body] end end and I have added my middleware in config/application.rb in two ways 1 ) Adding as a constant #config/application.rb module

Thread running in Middleware is using old version of parent's instance variable

自作多情 提交于 2019-12-07 09:55:02
问题 I've used Heroku tutorial to implement websockets. It works properly with Thin, but does not work with Unicorn and Puma. Also there's an echo message implemented, which responds to client's message. It works properly on each server, so there are no problems with websockets implementation. Redis setup is also correct (it catches all messages, and executes the code inside subscribe block). How does it work now: On server start, an empty @clients array is initialized. Then new Thread is started,

Thread running in Middleware is using old version of parent's instance variable

白昼怎懂夜的黑 提交于 2019-12-05 15:20:12
I've used Heroku tutorial to implement websockets. It works properly with Thin, but does not work with Unicorn and Puma. Also there's an echo message implemented, which responds to client's message. It works properly on each server, so there are no problems with websockets implementation. Redis setup is also correct (it catches all messages, and executes the code inside subscribe block). How does it work now: On server start, an empty @clients array is initialized. Then new Thread is started, which is listening to Redis and which is intended to send that message to corresponding user from

Alter response.body in Rack Middleware

风格不统一 提交于 2019-12-05 12:38:55
I'm trying to write some Rack Middleware for a Rails 4.2 app that alters the response body using the gsub method. I found older examples that use a pattern like this: class MyMiddleware def initialize(app) @app = app end def call(env) status, headers, response = @app.call(env) # do some stuff [status, headers, response] end end What I'm finding is that there is no setter method for response.body . Is there another pattern I can start with to go about modifying the body? The problem was that it expects an Array for the 3rd argument in the call method. This pattern got me working again. # not