公式サイト Getting Started
を見ながら。
$ go get github.com/revel/revel
go: missing Mercurial command. See http://golang.org/s/gogetcmd
package code.google.com/p/go.net/websocket: exec: "hg": executable file not found in $PATH
早速怒られましてたorz
hg
を入れる。
$ brew install hg
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mercurial-3.2.3.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mercurial-3.2.3.yosemite.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
zsh completion has been installed to:
/usr/local/share/zsh/site-functions
==> Summary
? /usr/local/Cellar/mercurial/3.2.3: 364 files, 4.8M
もっかい。
$ go get github.com/revel/revel
$ go get github.com/revel/cmd/revel
$ revel help
~
~ revel! http://revel.github.io
~
usage: revel command [arguments]
The commands are:
new create a skeleton Revel application
run run a Revel application
build build a Revel application (e.g. for deployment)
package package a Revel application (e.g. for deployment)
clean clean a Revel application's temp files
test run all tests from the command-line
Use "revel help [command]" for more information.
はいった。
Creating a new Revel application
公式サイト Creating a new Revel application
を見ながら。
$ cd $GOPATH
$ revel new myapp
~
~ revel! http://revel.github.io
~
Your application is ready:
/Users/XXX/XXXX/XXXXX/src/myapp
You can run it with:
revel run myapp
src/myapp
にできたみたい。
プロジェクト構成は以下のようになっていました。
tree myapp
myapp
├── README.md
├── app
│ ├── controllers
│ │ └── app.go
│ ├── init.go
│ ├── routes
│ │ └── routes.go
│ ├── tmp
│ │ └── main.go
│ └── views
│ ├── App
│ │ └── Index.html
│ ├── debug.html
│ ├── errors
│ │ ├── 404.html
│ │ └── 500.html
│ ├── flash.html
│ ├── footer.html
│ └── header.html
├── conf
│ ├── app.conf
│ └── routes
├── messages
│ └── sample.en
├── public
│ ├── css
│ │ └── bootstrap.css
│ ├── img
│ │ ├── favicon.png
│ │ ├── glyphicons-halflings-white.png
│ │ └── glyphicons-halflings.png
│ └── js
│ └── jquery-1.9.1.min.js
└── tests
└── apptest.go
14 directories, 21 files
実行してみる。
$ revel run myapp
~
~ revel! http://revel.github.io
~
INFO 2015/01/08 21:46:41 revel.go:326: Loaded module testrunner
INFO 2015/01/08 21:46:41 revel.go:326: Loaded module static
INFO 2015/01/08 21:46:41 run.go:57: Running myapp (myapp) in dev mode
INFO 2015/01/08 21:46:41 harness.go:165: Listening on :9000
INFO 2015/01/08 21:46:49 revel.go:326: Loaded module static
INFO 2015/01/08 21:46:49 revel.go:326: Loaded module testrunner
INFO 2015/01/08 21:46:49 main.go:29: Running revel server
Go to /@tests to run the tests.
Listening on :53355...
http://localhost:9000
にアクセスしてみる。
おお、アクセスできた。やっぽい。
The Request Flow
公式サイト The Request Flow
見ながら。
まずはルーティングの確認。
src/myapp/conf/routes
にルーティングの設定が書いてあるみたい。
1 # Routes
2 # This file defines all application routes (Higher priority routes first)
3 # ~~~~
4
5 module:testrunner
6
7 GET / App.Index
8
9 # Ignore favicon requests
10 GET /favicon.ico 404
11
12 # Map static resources from the /app/public folder to the /public path
13 GET /public/*filepath Static.Serve("public")
14
15 # Catch all
16 * /:controller/:action :controller.:action
GET
で/
にアクセスした場合は、App.Index
関数が実行されるということでしょう。きっと。
で、App.Index
がどこかというと、
myapp/controllers/app.go
にあります。
package controllers
import "github.com/revel/revel"
type App struct {
*revel.Controller
}
func (c App) Index() revel.Result {
return c.Render()
}
のIndexメソッド
が実行されています。
今回revelを使って想定しているサーバは、htmlをはかずに、
フロントからのAPIアクセスで、jsonを返すサーバにするつもりなので、
JSONを返すように書き換えてみます。
まず、User構造体を定義します。
type User struct {
Name string
}
次にindexメソッドを書き換えます。
jsonを返すには、c.RenderJson
を使えばいいようです。
func (c App) Index() revel.Result {
//return c.Render()
return c.RenderJson(User{"go tarou"})
}
で、再度アクセス。きちんとJSONが返ってきました。
レスポンスのName
プロパティが大文字になっていますね。小文字にしたい。
これは、次のようにすると自動で変換してくれるようです。
type User struct {
Name string `json:"name"`
}
小文字になりました。やっぽい。
導入までのまとめ
さくっとrevelを試しました。
意外にすんなりいきました。
次は、構造体とjsonのマッピングやルーティングの追加や、DB接続を試してみたいと思います。
構造体とjsonのマッピング
あとで書く。
mysqlと接続してみる
Go言語 + webFramework revelで、MYSQLを使用する
redisと接続してみる
あとで書く。