LoginSignup
42
41

More than 5 years have passed since last update.

go言語のweb framework のrevelを試してみる

Last updated at Posted at 2015-01-08

公式サイト 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にアクセスしてみる。

スクリーンショット 2015-01-08 21.47.33.png

おお、アクセスできた。やっぽい。

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が返ってきました。

スクリーンショット 2015-01-08 22.18.14.png

レスポンスのNameプロパティが大文字になっていますね。小文字にしたい。
これは、次のようにすると自動で変換してくれるようです。

type User struct {
    Name string `json:"name"`
}

スクリーンショット 2015-01-08 22.21.08.png

小文字になりました。やっぽい。

導入までのまとめ

さくっとrevelを試しました。
意外にすんなりいきました。
次は、構造体とjsonのマッピングやルーティングの追加や、DB接続を試してみたいと思います。

構造体とjsonのマッピング

あとで書く。

mysqlと接続してみる

Go言語 + webFramework revelで、MYSQLを使用する

redisと接続してみる

あとで書く。

42
41
1

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
42
41