LoginSignup
3
6

More than 5 years have passed since last update.

RailsアプリにオレオレRackアプリをマウントする

Last updated at Posted at 2015-11-13

なにをしたいか

/exampleみたいなパス以下のリクエストを根こそぎ処理する。

簡単にマウントしてみる

適当なRailsアプリを作る

$ rails new mount_rack_example

lib以下をロードするようにする

config/application.rb
module MountRackExample
  class Application < Rails::Application
    config.autoload_paths += %W(#{config.root}/lib)
  end
end

Rackアプリを適当に作る

lib/rack_example.rb
module RackExample
  def self.call(env)
    p env
    [200, {}, ["#{env}"]]
  end
end

callメソッドはクラスメソッドで定義しないといけない。

  
マウントする

config/routes.rb
Rails.application.routes.draw do
  mount RackExample, at: '/example'
end

試しに見てみる

$ bundle exec rails s
$ open http://localhost:3000/example

知見

あわせて読みたい

Ruby - Rack解説 - Rackの構造とRack DSL - Qiita

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6