Rails5でのアプリケーションサーバ

Ruby on Rails チュートリアル - 実例を使ってRailsを学ぼう を見ながら、チュートリアルの続きです。

まだ起動したばかりで、何もしていません…。 ですが、Rails4までの時と違っている点を発見。デフォルトのトップページ (welcome page) も違いますが、起動時の様子もちょっと違う。

$ bundle exec rails s
=> Booting Puma  # ここ
=> Rails 5.0.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.2 (ruby 2.3.2-p217), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

よくよく見ると、見慣れたWebrickではなくて、Pumaという文字になっています。

Pumaってなに?

Puma とは、どうやら並列実行性を持ったruby用のアプリケーションサーバのようです。 (何年も前から出ているんですね…知らなかった)

READMEの冒頭に書かれている文は、こんな感じです。

Puma is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications. Puma is intended for use in both development and production environments. In order to get the best throughput, it is highly recommended that you use a Ruby implementation with real threads like Rubinius or JRuby.

“Pumaは、シンプルで高速で、スレッド処理・同時実行が可能なRuby/Rackアプリケーション向けのHTTP1.1サーバです。”

“Pumaは開発モードでもプロダクションモードでもどちらの場合でも利用できるようになっています。” “最高のスループットを出すためには、Rubinius や JRubyといったスレッド対応したRubyの実装を利用することをお勧めします。”


ちょっと確認してみると、config/ の下に、puma.rb というスクリプトが配置されています。

実際のところは、標準のrubyでの実行なのですが、それでも早いとか? 細かいところは少しづつ学習していきます….

1.3.2 rails server / 演題の確認

ここまでで、”1.3.2 rails server” の章の演題の確認をします。 (rubyのバージョン、railsのバージョン)

$ rails -v
Rails 5.0.2
$ ruby -v
ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-darwin16]

rails sして表示されたWelcome画面にも、この情報が出ていますね!

1.3.4 Hello, world! / 最初のメソッドを調整する

まだモデルやコントローラは追加していませんが、application_controller.rbにメソッドを追加することで、アクションを追加することができました。

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def hello
    render html: "hello, world!"
  end
end

追加したアクションを、アプリケーションのroot (デフォルトのトップ)に関連づけるには、ルーティングをroutes.rb に追加します。

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'application#hello'
end

上記の場合は、http://localhost:3000/ のWelcome画面が、hello, world! だけが表示されたシンプルなものに置き換わりました。

http://localhost:3000/hello に割り当てる場合は、こんな感じ。

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  #root 'application#hello'
  get :hello, to: 'application#hello'
end

参考:Rails Guides - Rails のルーティング


本日のJekyllメモ

Table of Contents を置くには?

{:toc} を置けば良いとのこと。 ということで、このポストには

* TOC
{:toc}

を配置してみました。あとはscssを調整してスッキリ。 目次はSidebar側に配置するのがいいかもしれませんね。