Created
July 7, 2015 05:02
-
-
Save kazu69/bc66cdbdaf8c1c7550ae to your computer and use it in GitHub Desktop.
simple rack mimddlewear
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
class MyApp < Sinatra::Base | |
get '/hoge' do | |
'Hello world!' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './app' | |
require File.expand_path 'lib/my_rack_middleware', File.dirname(__FILE__) | |
use MyRackMiddleware | |
run MyApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem 'sinatra' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree -L 2 | |
. | |
├── Gemfile | |
├── Gemfile.lock | |
├── app.rb | |
├── config.ru | |
├── lib | |
│ └── my_rack_middleware.rb | |
└── vendor | |
└── bundle | |
bundle install | |
bundle exec rackup -p 4567 | |
open http://localhost:4567 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /lib/my_rack_middlewear.rb | |
class MyRackMiddleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
if env["PATH_INFO"] == '/hoge' | |
status, headers, response = @app.call(env) | |
status = 200 | |
response = ["hogehoge"] | |
content_length = response.inject(0) do |sum, content| | |
sum + content.bytesize | |
end | |
headers = { "Content-Type" => "text/html;charset=utf-8", | |
"Content-Length" => content_length.to_s, | |
"X-XSS-Protection" => "1; mode=block", | |
"X-Content-Type-Options" => "nosniff", | |
"X-Frame-Options" => "SAMEORIGIN"} | |
else | |
res = @app.call(env) | |
end | |
[status, headers, response] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment