Skip to content

Instantly share code, notes, and snippets.

@dylanninin
Last active August 17, 2016 15:14
Show Gist options
  • Save dylanninin/4c41cff6da79ca1a6b8c4126eda2dea2 to your computer and use it in GitHub Desktop.
Save dylanninin/4c41cff6da79ca1a6b8c4126eda2dea2 to your computer and use it in GitHub Desktop.
class MiddleWare1
def initialize(app)
@app = app
end
def call env
puts self.class
status, headers, body = @app.call(env)
append_str = "\nGreetings from #{self.class}"
body << append_str
[status, headers, body]
end
end
class MiddleWare2
def initialize(app)
@app = app
end
def call env
puts self.class
status, headers, body = @app.call(env)
append_str = "\nGreetings from #{self.class}"
body << append_str
[status, headers, body]
end
end
use Rack::Reloader
use MiddleWare1
use MiddleWare2
app = Proc.new {|env|
puts "#{Time.now}, env: #{env}"
# status_code, headers, body
[200, {}, ["Hello. The time is #{Time.now}"]]
}
run app
################################################################################
# request
# |
# |
#
# MiddleWare1
# |
# |
#
# MiddleWare2
# |
# |
#
# App
#
# onion architecture:https://github.com/dylanninin/dylanninin.github.com/issues/4
# - calling: request -> MiddleWare1 -> MiddleWare2 -> App
# - returning: App -> MiddleWare2 -> MiddleWare2 --> response
################################################################################
@dylanninin
Copy link
Author

  • run: rackup middlewares.ru
  • test: curl -v http://127.0.0.1:9292

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment