GoのWAF、Gondolaを試す


個人開発用に、GoのWAFで最近のって何かないかな〜と探していたところに見つけたので、試してみました。

Webサイトをより速く書くためのフレームワーク、と書かれてます。

公式サイト

環境

今回試した環境は以下の通りです。

  • centos7
  • Go1.6

準備

mercurialが必須らしいので、インストールしておきます。

$ sudo yum install git mercurial

インストール

go getでインストールします。

$ go get -v gnd.la/cmd/gondola

完了確認のために、helpを表示してみます。

$ gondola help
dev               Start the Gondola development server
new               Create a new Gondola project
profile           Show profiling information for a remote server running a Gondola app
bake              Converts all assets in <dir> into Go code and generates a VFS named with <name>
random-string     Generates a random string suitable for use as the app secret
rm-gen            Remove Gondola generated files (identified by *.gen.*)
make-messages     Generate strings files from the current package (including its non-package subdirectories, like templates)
compile-messages  Compiles all po files from the current directory and its subdirectories
gae-dev           Start the Gondola App Engine development server
gae-test          Start serving your app on localhost and run gnd.la/app/tester tests against it
gae-deploy        Deploy your application to App Engine
help              Print this help

To view additional help for each command use help <command_name>

これが表示されれば大丈夫みたいです。

試す

hallo worldしてみます。

アプリの新規作成

gondolaコマンドを使って、アプリケーションディレクトリを生成します。

``bash
$ gondola new -template=blank hello
[D] 2016/12/21 02:33:55 writing file hello/app.conf
[D] 2016/12/21 02:33:55 creating directory hello/assets
[D] 2016/12/21 02:33:55 writing file hello/dev.conf
[D] 2016/12/21 02:33:55 creating directory hello/tmpl

$ cd hello

$ ls -ltr
total 16
drwxr-xr-x 2 h-tko h-tko 4096 Dec 21 02:34 tmpl
-rw-rw-r-- 1 h-tko h-tko 86 Dec 21 02:34 dev.conf
drwxr-xr-x 2 h-tko h-tko 4096 Dec 21 02:34 assets
-rw-rw-r-- 1 h-tko h-tko 87 Dec 21 02:34 app.conf
```

こんな感じで展開されてます。

-templateオプションはhelloとかやると、halloテンプレートを用意してくれるようですが、
肝心のテンプレートファイルの呼び出し方とか、変数の埋め込みあたりのチュートリアルはTBDになってます(´・ω・`)

ハンドラを作る

次にハンドラを作ります。
Gondolaのチュートリアルでは、handlers.goとなっているので、その名前で作っておきます。

handlers.go
package main

import (
    "gnd.la/app"
)

func MainHandler(ctx *app.Context) {
    ctx.WriteString("Hello World!")
}

こんな感じのシンプルなやつです。

app.goを作る

アプリの起点になる処理を書きます。

app.go
package main

import (
    "gnd.la/app"
    "gnd.la/config"
)

var (
    App *app.App
)

func init() {
    config.MustParse()

    App = app.New()

    // ここでrouting
    App.Handle("^/$", MainHandler)

    // チュートリアルだと以下のように書かれていますが、現在の最新ではこれだと動きません
    //App.HandleNamed("^/$", MainHandler, "main")
}

main.goを作る

最後に、main.goを作ります。
こんな感じで

main.go
package main

func main() {
    App.MustListenAndServe()
}

これで最低限の処理はできたので、一度サーバを起動してみます。

動かしてみる

gondolaコマンドで起動できます。
引数でdevにすることでdevelopmentで動くっぽいのですが、dev.confのport変えても待ち受けport変わらない??

$ gondola dev
[I] 2016/12/21 03:03:52 Using config file dev.conf
[I] 2016/12/21 03:03:52 Starting Gondola development server on port 8888 (press Control+C to exit)
[I] 2016/12/21 03:03:53 Building hello (go build -gcflags -e)
[E] 2016/12/21 03:03:53 error opening browser: open http://localhost:8888 manually (error was exec: "xdg-open": executable file not found in $PATH)
[I] 2016/12/21 03:03:54 Starting hello (./hello -config dev.conf -port=49740 -debug -template-debug -log-debug)

xdg-openがないと怒ってますが、問題ないので一旦そのまま。

localhost:8888にアクセスすると、こんな感じで表示されます!

比較的さくさく作れる感じはするので、
テンプレート周りとかできてくれば、選択肢としてはありえるかもしれませんね。