{"id":4891,"date":"2014-12-11T10:00:58","date_gmt":"2014-12-11T01:00:58","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=4891"},"modified":"2018-11-14T16:33:50","modified_gmt":"2018-11-14T07:33:50","slug":"rackmiddleware%e3%81%ae%e5%a7%8b%e3%82%81%e6%96%b9","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2014\/12\/11\/rackmiddleware%e3%81%ae%e5%a7%8b%e3%82%81%e6%96%b9\/","title":{"rendered":"Rack Middleware\u306e\u59cb\u3081\u65b9"},"content":{"rendered":"
\u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002\u3053\u306e\u8a18\u4e8b\u306f TECHSCORE Advent Calendar 2014<\/a> \u306e 11\u65e5\u76ee\u306e\u8a18\u4e8b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u6700\u8fd1Rails\u3067\u4f7f\u7528\u3059\u308bRack Middleware\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u306e\u3067\u3001\u305d\u306e\u8fba\u308a\u306e\u8a71\u3092\u66f8\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n rack\u306b\u3064\u3044\u3066\u8aac\u660e\u3059\u308b\u3068\u9577\u304f\u306a\u308b\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u8a18\u4e8b\u304c\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002 \u7c21\u5358\u306b\u8aac\u660e\u3059\u308b\u3068\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30b5\u30fc\u30d0\u3068\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306e\u3053\u3068\u3067\u3059\u3002 \u3092\u307e\u3068\u3081\u305f\u914d\u5217\u3092\u8fd4\u3059\u3088\u3046\u306b\u3057\u3066\u306d\uff01\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002<\/p>\n \u4eca\u56de\u306f\u3001\u5927\u91cf\u306e\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u306e\u30ed\u30b0\u3067\u30a2\u30d7\u30ea\u306e\u30ed\u30b0\u304c\u898b\u306b\u304f\u304f\u306a\u308b\u3053\u3068\u306b\u60a9\u3093\u3067\u3044\u307e\u3057\u305f\u306e\u3067\u3001\u5c02\u7528\u306e\u30ed\u30b0\u306b\u51fa\u529b\u3059\u308b\u6a5f\u80fd\u3092\u6301\u3063\u305f\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002 \u30ed\u30b0\u3092\u5206\u3051\u308b\u305f\u3081\u306emiddleware\u306a\u306e\u3067\u3001Rails::Rack::Logger\u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u307e\u3059\u3002 initialize\u3067\u5c02\u7528\u306e\u30ed\u30ac\u30fc\u3092\u4f5c\u3063\u3066\u304a\u3044\u3066\u3001\u30a2\u30af\u30bb\u30b9\u6642\u306bPATH_INFO\u3092\u53c2\u7167\u3057\u3001\u30ed\u30ac\u30fc\u3092\u5207\u308a\u66ff\u3048\u308b\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002<\/p>\n Rails\u3067\u306f\u3001config.middlware\u306b<\/p>\n \u306e3\u3064\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n \u4eca\u56de\u306f\u65e2\u5b58\u306e\u30ed\u30ac\u30fc\u3092\u5165\u308c\u66ff\u3048\u308b\u306e\u3067\u3001swap\u3092\u4f7f\u7528\u3057\u307e\u3057\u305f\u3002<\/p>\n config\/application.rb\u306e\u4e2d\u3067\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u304c\u3001config\/initializers\u306e\u4e2d\u3067\u8a2d\u5b9a\u3057\u3066\u3082\u826f\u3044\u307f\u305f\u3044\u3067\u3059\u3002 rake\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n \u4ee5\u4e0b\u306e\u3088\u3046\u306b\u81ea\u5206\u304c\u4f5c\u3063\u305f\u30af\u30e9\u30b9\u304c\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n \u666e\u901a\u306brails\u3092\u8d77\u52d5\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3057\u3066\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n \u4ed6\u306b\u3082\u3044\u304f\u3064\u304b\u4f5c\u3063\u3066\u307f\u3066\u30cf\u30de\u3063\u305f\u3068\u3053\u308d\u304c\u3042\u308b\u306e\u3067\u3001\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n middleware\u306f\u8d77\u52d5\u6642\u306b\u3057\u304b\u8aad\u307f\u8fbc\u307e\u308c\u307e\u305b\u3093\u3002 \u958b\u767a\u4e2d\u3060\u3068\u5730\u5473\u306b\u30e1\u30f3\u30c9\u30af\u30b5\u30a4\u3067\u3059\u3002<\/p>\n middleware\u306e\u4e2d\u3067\u4f8b\u5916\u3092\u3056\u3063\u304f\u308a\u53d7\u3051\u53d6\u3063\u3066\u3001500\u3067\u8fd4\u3059\u3088\u3046\u306a\u4f5c\u308a\u306b\u3057\u3066\u3057\u307e\u3046\u3068\u3001\u4ed6\u306emiddleware\u306b\u4f8b\u5916\u304c\u4f1d\u642c\u3067\u304d\u306a\u3044\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002 \u4ee5\u4e0a\u3001\u7c21\u5358\u306b\u8aac\u660e\u3057\u3066\u307f\u307e\u3057\u305f\u304c\u3001Rack Middleware\u3092\u4f5c\u308c\u308b\u3088\u3046\u306b\u306a\u308b\u3068\u3001ApplicationControler\u3067\u5bfe\u5fdc\u3057\u3066\u3044\u305f\u3088\u3046\u306a\u51e6\u7406\u3092\u5207\u308a\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u305d\u3046\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" \u3053\u3093\u306b\u3061\u306f\u3001\u6cb3\u91ce\u3067\u3059\u3002\u3053\u306e\u8a18\u4e8b\u306f TECHSCORE Advent Calendar 2014 \u306e 11\u65e5\u76ee\u306e\u8a18\u4e8b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u6700\u8fd1Rails\u3067\u4f7f\u7528\u3059\u308bRack Middleware\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u306e\u3067\u3001\u305d\u306e\u8fba\u308a\u306e\u8a71\u3092\u66f8\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u305d\u3082\u305d\u3082rack\u3063\u3066\uff1f<\/h2>\n
\nRuby - Rack\u89e3\u8aac - Rack\u306e\u69cb\u9020\u3068Rack DSL - Qiita<\/a><\/p>\n
\n\u3042\u308b\u30af\u30e9\u30b9\u306bcall\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u3066\u3001<\/p>\n\n
Rack Middleware\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n
\nstackoverflow\u306e\u8a18\u4e8b<\/a>\u3092\u53c2\u8003\u306b\u4f5c\u308a\u307e\u3057\u305f\u3002<\/p>\n
\n\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u30d8\u30eb\u30b9\u30c1\u30a7\u30c3\u30af\u7528\u306e\u30d1\u30b9\u3068\u3001\u30ed\u30b0\u306e\u30bf\u30b0\u3092\u6e21\u305b\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n# lib\/health_logger.rb\r\nclass HealthLogger < Rails::Rack::Logger\r\n def initialize(app, opts={})\r\n @default_log = Rails.logger\r\n @health_log = Logger.new(Rails.root.join('log', 'health.log'))\r\n @app = app\r\n @opts = opts\r\n @opts[:health_path] = Array(@opts[:health_path])\r\n\r\n super app, @opts[:tags]\r\n end\r\n\r\n def call(env)\r\n if @opts[:health_path].include?(env['PATH_INFO'])\r\n logfile = @health_log\r\n else\r\n logfile = @default_log\r\n end\r\n\r\n ActiveRecord::Base.logger = logfile\r\n ActionController::Base.logger = logfile\r\n ActionView::Base.logger = logfile\r\n Rails.logger = logfile\r\n @logger = logfile\r\n\r\n super env\r\n end\r\nend<\/pre>\n
\u4f5c\u3063\u305fmiddleware\u3092\u8ffd\u52a0\u3059\u308b<\/h2>\n
\n
# config\/application.rb\r\nconfig.middleware.swap(\r\n Rails::Rack::Logger, \"HealthLogger\", {tags: config.log_tags, health_path: \"\/check\/health\"}\r\n)<\/pre>\n
\n\u30af\u30e9\u30b9\u540d\u306f\u6587\u5b57\u5217\u3067\u3082\u6e21\u305b\u307e\u3059\u3002<\/p>\nmiddleware\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3059\u308b<\/h2>\n
bundle exec rake middleware<\/pre>\n
use Rack::Runtime\r\nuse Rack::MethodOverride\r\nuse ActionDispatch::RequestId\r\nuse HealthLogger\r\nuse ActionDispatch::ShowExceptions\r\nuse ActionDispatch::DebugExceptions\r\n<\/pre>\n
\u52d5\u4f5c\u78ba\u8a8d\u3059\u308b<\/h2>\n
bundle exec rails s<\/pre>\n
\u30cf\u30de\u3063\u305f\u3053\u3068<\/h2>\n
auto reload \u3055\u308c\u306a\u3044<\/h3>\n
\n\u4fee\u6b63\u3057\u305f\u5185\u5bb9\u3092\u53cd\u6620\u3059\u308b\u305f\u3081\u306b\u306f\u3001rails\u306e\u518d\u8d77\u52d5\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\u4f8b\u5916\u3092\u9069\u5207\u306b\u51e6\u7406\u3059\u308b<\/h3>\n
\n\u30a2\u30d7\u30ea\u672c\u4f53\u3084\u4ed6\u306emiddleware\u3067\u767a\u751f\u3057\u305f\u30a8\u30e9\u30fc\u3082\u62fe\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u5185\u5bb9\u3092\u78ba\u8a8d\u3057\u305f\u4e0a\u3067raise\u3059\u308b\u306a\u3069\u306e\u51e6\u7406\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n
\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[141,22],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4891"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=4891"}],"version-history":[{"count":20,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4891\/revisions"}],"predecessor-version":[{"id":5179,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4891\/revisions\/5179"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=4891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=4891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=4891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}