robfig/revel · GitHub https://github.com/robfig/revel
Revel は下記のような説明がしてあります.
A high productivity web framework for the Go language, modeled on the Play! web framework (similar to Rails).
要するに Scala の Play Framework をモデルとした Web フレームワークのようです.
細かい機能や導入の詳しい内容ははここに書かれています.
さて,今回は簡単なアプリケーションを作ってみます.
最初に revel を使えるようにします. revel のソースを取得してきてパスに revel をビルドしたものを設置しましょう.
$ cd $GOPATH $ go get github.com/robfig/revel $ go build -o bin/revel github.com/robfig/revel/cmd
これで revel コマンドが使える様になったはずです. 試しになにか新しく Revel アプリケーションを作ってみましょう. アプリケーション名は hello-revel としておきます.
$ revel new github.com/nise-nabe/hello-revel ~ ~ revel! http://robfig.github.com/revel ~ Your application is ready: <hogehoge>/src/github.com/nise-nabe/hello-revel You can run it with: revel run github.com/nise-nabe/hello-revel
PlayFramework ではカレントディレクトリにアプリケーションができますが Revel では GOPATH/src 以下にできます. それでは実際に走らせてみましょう. デフォルトではポートは 9000 で起動します.
$ revel run github.com/nise-nabe/revel ~ ~ revel! http://robfig.github.com/revel ~ 2013/03/20 16:29:43 revel.go:240: Loaded module static 2013/03/20 16:29:43 revel.go:240: Loaded module testrunner 2013/03/20 16:29:43 run.go:57: Running hello-revel (github.com/nise-nabe/hello-revel) in dev mode 2013/03/20 16:29:43 harness.go:143: Listening on :9000
ページを開いてみましょう. とりあえず開いてみるだけなので w3m で標準出力に書き出します.今のところ cookie も使っておらず表示上邪魔なので消してます.
$ w3m http://localhost:9000 -no-cookie -dump Your Application Is Ready
「Your Application Is Ready」」という表示がされました. ここからは作成したアプリケーションのルートにいる前提にしておきます. ポートを変更したい場合はアプリケーションのルートから conf/app.conf の中に http.port という項目があるのでここを変更してください.
ここで Revel の構成を見てみます.
$ tree . ├── app │ ├── controllers │ │ └── app.go │ ├── tmp │ │ └── main.go │ └── views │ ├── Application │ │ └── Index.html │ ├── errors │ │ ├── 404.html │ │ └── 500.html │ ├── footer.html │ └── header.html ├── conf │ ├── app.conf │ └── routes ├── messages │ └── sample.en ├── public │ ├── css │ │ └── bootstrap.css │ ├── images │ │ └── favicon.png │ └── js │ └── jquery-1.9.1.min.js └── tests └── apptest.go 13 directories, 14 files
ちなみにこちら PlayFramework (2.1) の Scala の場合の構成です.
. ├── app │ ├── controllers │ │ └── Application.scala │ └── views │ ├── index.scala.html │ └── main.scala.html ├── conf │ ├── application.conf │ └── routes ├── project │ ├── build.properties │ ├── Build.scala │ └── plugins.sbt ├── public │ ├── images │ │ └── favicon.png │ ├── javascripts │ │ └── jquery-1.9.0.min.js │ └── stylesheets │ └── main.css ├── README └── test ├── ApplicationSpec.scala └── IntegrationSpec.scala 10 directories, 14 files
Revel で扱うファイルは PlayFramework と同様に,MVC の内容は app へ,設定は conf へ,アセット類は public へ入れます.
ここで簡単に Twitter の home_timeline を取得するアプリを書いてみます. Twitter の認証や表示を行うコードは Revel の exmaple ディレクトリにあるため,これを流用します. コードはすでにあるので簡単に流れだけ.
新しいアクション Authentication を追加して認証処理を追加し,ルーティングに /auth を追加して Authentication アクション に紐付けます.ConsumerKey や ConsumerKeySecret などはなんとかして取得してコードに書き込んでください.
add twitter auth · 717213c · nise-nabe/hello-revel https://github.com/nise-nabe/hello-revel/commit/717213c0c3b9c3dc3e7a5a3cae38380567e5d868
そして認証が成功している場合ににタイムラインを表示する処理を追加します.
add feature to get home_timeline · d8cbe98 · nise-nabe/hello-revel https://github.com/nise-nabe/hello-revel/commit/d8cbe98b6a08688871b63fe5cc7c784f5fd52553
これで表示されるはずようになったはずです. ここではアクセストークンの永続化を行なっていないため,コードを修正するたびに認証し直す作業が発生します.
ただ,このままだと twitter のレスポンス時点で html のエスケープが行われており, ビューに渡す際にさらにエスケープされるため表示が二重エスケープされた状態になってしまうという問題が残ってしまいます. また,revel の example の例だと投稿時に CSRF 脆弱性が残ったままっぽいでこのまま例に出すのはどうかという感じでここでは実装してません.
とりあえずここまでで簡単に twitter のタイムラインを表示するアプリケーションを作成してみました. 他にも https://github.com/robfig/revel/tree/master/samples には例が幾つか書かれているのでここを参考に Go 言語で Web アプリを作ってみるのもいいですね.