0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rackアプリケーションの実行方法いろいろメモ

Posted at

ほとんど参考サイトの再構成によるメモ

rackup コマンドを使う

  • rackup コマンドで実行する
  • コード内で run [RackApp] を実行する
  • config.ru に設定やコードを書く(ただし rackup コマンド実行時に別のファイルを指定も可能)

インスタンスメソッドを使う

実行コマンド
> bundle exec rackup -o 0.0.0.0
config.ru
require './app.rb'
run SimpleApp.new
app.rb
class SimpleApp
  def call(env)
    [200,{ 'Content-Type' => 'text/html' },["Hello"]]
  end
end

もちろん以下のように config.ru にまとめても可。

config.ru
class SimpleApp
  def call(env)
    [200,{ 'Content-Type' => 'text/plain' },["Hello"]]
  end
end

run SimpleApp.new

クラスメソッドを使う

実行コマンド
> bundle exec rackup -o 0.0.0.0
config.ru
class SimpleApp
  def self.call(env)
    [200, {'Content-Type' => 'text/plain'}, ['Hello']]
  end
end

run SimpleApp

Proc.new を使う

生成されるオブジェクトは call メソッドを持つ。

実行コマンド
> bundle exec rackup -o 0.0.0.0
config.ru
run Proc.new { |env| ['200', {'Content-Type' => 'text/html'}, ['Hello']] }

以下でもおk。

config.ru
run lambda {|env| [200, {'Content-Type' => 'text/plain'}, ['Hello']] }

rackup コマンドを使わない(ハンドラから直接実行する)

実行コマンド
> bundle exec ruby my_rack_app.rb
app.rb
require 'rack'

app = Proc.new do |env|
    ['200', {'Content-Type' => 'text/html'}, ['Hello']]
end

Rack::Handler::WEBrick.run app

参考サイト

Rack解説 - Rackの構造とRack DSL - Qiita https://qiita.com/higuma/items/838f4f58bc4a0645950a#rack%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E4%BB%95%E6%A7%98

Rack: a Ruby Webserver Interface http://rack.github.io/

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?