Skip to content

Instantly share code, notes, and snippets.

@WyattJia
Created December 20, 2017 15:51
Show Gist options
  • Save WyattJia/93c12baad6e2bad0e3a2287620e65a08 to your computer and use it in GitHub Desktop.
Save WyattJia/93c12baad6e2bad0e3a2287620e65a08 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
# encoding: utf-8
require 'rack'
class MyMiddleware
def initialize(app)
@app = app
end
def call(env)
code, headers, body = @app.call(env)
body << 'this is my middleware'
[code, headers, body]
end
end
class MyApp
def call(env)
[200, {'Content-Type' => 'text/plain'}, ['hello world']]
end
end
app = Rack::Builder.app do
map '/' do
run MyApp.new
end
map '/middleware' do
use MyMiddleware
run MyApp.new
end
end
run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment