前回は、Sinatraバージョン1.0の概要を公式ドキュメントを手がかりとして、Sinatraを紹介しました。そして最後に、「Sinatraの先には、まだ地図がない」と言及しました。 今回は、「実世界のSinatra」と題して、実際にSinatraを利用して開発していくうえでの、筆者自身のロードマップを示していきます。 Sinatraとはいったい何か いきなりですが、Sinatraとはいったい何なのでしょう。 これは根本的な問いになりますが、Sinatraで開発を進める前に、ここをしっかり考えることが重要であると筆者は考えます。 素直に考えるならば、Sinatraはもちろん、広義のWebアプリケーションフレームワークの一つである、と答えられるでしょう。アプリケーションフレームワークのそもそもの定義が、「共通部分を再利用可能にし、開発を助けるもの」であるならば、Sinatraもこの例に
はじめに 第7回はRails以外のWebフレームワークの簡単な紹介と、SinatraでHello Worldアプリケーションを動かすところまでを解説しました。今回はSinatraで実際のアプリケーションを作り、SequelとHamlという2つのライブラリを紹介します。 Sinatraの特徴は、CGIスクリプトのようにファイル一つからアプリケーションが書ける気軽さです。CGIスクリプトといえば、代表的なものは何と言っても掲示板(BBS)です。そこで、今回はSinatraで掲示板アプリを作ってみました。ソースコードが少し長めなので、githubにて全文を公開しています。適宜参照しながら読み進めて下さい。ファイル構成は以下のようになっています。 start.rb アプリケーションの本体。 model/comment.rb 掲示板の書き込みを表すモデルの定義。 view/index.haml トッ
最悪だ。 Sinatra を CGI で動かそうとして、原因不明のエラーが発生し、原因を特定するまでに半日くらい要した。 ソース #! /usr/local/bin/ruby require 'rubygems' require 'sinatra' get '/' do 'Hello world!!' end 実行方法 % ruby sinatra.rb エラーの内容 ./sinatra.rb:6: undefined method `get' for main:Object (NoMethodError) from /home/yamamucho/local/lib/rubygems/custom_require.rb:31:in `gem_original_require' from /home/yamamucho/local/lib/rubygems/custom_require.r
SinatraやRailsは開発は楽だけどデプロイ方法がいろいろあって悩む。 最近はpassengerを使う方法が流行ってるけど、あいにく学校に置いたサーバーでサブドメインが使えないので同一ホスト名でapacheと共存させるしかない。 passengerでもRailsBaseURIだったか?を設定すればサブディレクトリでRack対応アプリを動かせるが、 今回は1年前からRails+mongrelのデプロイに使っているapache2のサブディレクトリをmongrelで起動してるrailsにプロキシする – 橋本詳解と同じ方法でSinatra+Thinをデプロイした。 Railsではmongrel_clusterで複数起動させたrailsにapache2のmod_proxy_balancerでアクセスを割り振ったが、同じ設定でsinatra+thinにも割り振れる。 結果、橋本商会 Twitt
個人的にSinatraとLimonadeがマイブームなのですが、Sinatraに関して言えば、CGIで動かす方法は(本家のドキュメントとかでは)あまりフォローされていないようです。国内ではそれなりに記事もあるようなのですが、結局ソースに手を入れてしまう(Sinatraのソースに「Rack::Handler::CGI.run」をベタに書く)か、rackupで実行する方法かが多そう。でも、前者はCGI専用になってしまうのでいまいちだし、後者はシェルが2個実行されるのがどうにも美しくありません(現実的にはあまり問題にならないのかもしれませんが)。 そこでいろいろいじってみたところ、何とか良さげな方法ができました。 まずSinatraのソース。適当です。 require 'rubygems' require 'sinatra' set :run, true get '/?' do "hello"
This page is also available in English. 始めよう SinatraはRubyで下記のような最小労力で手早くウェブアプリケーションを作成するためのDSLです。 # myapp.rb require 'rubygems' require 'sinatra' get '/' do 'Hello world!' end gemをインストールして動かしてみる。 sudo gem install sinatra ruby myapp.rb localhost:4567 を見る。 ルート Sinatraでは、ルートはHTTPメソッドとURLマッチングパターンがペアになっています。 ルートはブロックに結び付けられています。 get '/' do .. 何か見せる .. end post '/' do .. 何か生成する .. end put '/' do .. 何か更
HerokuはRuby on Railsのクラウド環境を提供しているWebサービスだ。最近ではherokuコマンドによってコマンド一つでデプロイが完了する手軽さになっている。これによりサーバのメンテナンスやデータベースのことなど一切考えずに開発だけに専念できる。 Heroku上でSinatraを動かす だが何でもRailsというのは意外と面倒だ。Railsでは重厚過ぎる、そう感じたらSinatraをHeroku上で展開してみよう。 今回紹介するフリーウェアはHeroku Sinatra App、Heroku上でSinatraアプリケーションを展開するサンプルアプリケーションだ。ソースコードは公開されているがライセンスは明記されていないのでご注意いただきたい。 Heroku Sinatra AppはHeroku向けとは言うものの、特に特殊なことはしていない。HerokuがSinatraにも対
はじめに あなたは「Ruby」と聞いて最初に何を思い浮かべますか? オブジェクト指向スクリプト言語であること、プログラミングの「楽しさ」を重視して設計された言語であること、最近新しいバージョンである1.9.1がリリースされたこと……。 Rubyにはいろいろな特徴がありますが、Rubyという単語からすぐに「Ruby on Rails」を思い出す方も多いのではないでしょうか。Ruby on Rails(以下Rails)は2004年に公開されたRuby用のWebアプリケーションフレームワークで、その生産性の高さから注目を浴び、Rubyの名前を広めることにも大きく貢献しました。 ですが、Java、PHP、Perl、Pythonなど、他の言語の世界ではいくつものフレームワークがしのぎを削っているのに対し、Rubyの世界においてはRailsの完成度の高さから、長らくライバル不在の状況が続いてきました。
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く