Melon - Go製のWebフレームワーク
ここ最近Goで様々なソフトウェアが作られるようになっています。最も有名なところとしてはDockerがあるかと思いますが、CLIだけでなくGoはWeb上でも利用ができます。 今回はそんなGoを使ったWebフレームワーク、Melonを紹介します。特にRESTful APIの開発に向いたフレームワークです。
Melonの使い方
Melonの一例です。例えばHello Worldを返すコードは次のようになります。
package main
import (
"net/http"
"os"
"github.com/goburrow/melon"
"github.com/goburrow/melon/core"
)
// resource is the HTTP handler of the application homepage.
type resource struct {
}
func (*resource) Method() string {
return "GET"
}
func (*resource) Path() string {
return "/"
}
func (*resource) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world"))
}
// Build application and run with command:
// ./helloworld server path/to/config.yaml
// Then open these links in browser for application and admin page respectively:
// http://localhost:8080/application/
// http://localhost:8080/admin/
func main() {
app := &melon.Application{
RunFunc: func(conf interface{}, env *core.Environment) error {
env.Server.Register(&resource{})
return nil
},
}
melon.Run(app, os.Args[1:])
}
これを実行してWebブラウザからアクセスすれば普通にHTMLが返ってきます。
さらにRESTfulのデモを実行した場合。
$ go run ./restful.go server ./config.yaml
INFO [2015-11-15T14:09:34.807+09:00] melon/server: starting MyApp
INFO [2015-11-15T14:09:34.807+09:00] melon/assets: registering AssetsBundle for path /static/
DEBUG [2015-11-15T14:09:34.808+09:00] melon/server: resources = [*rest.XMLProvider,*main.usersResource,*main.userResource]
INFO [2015-11-15T14:09:34.808+09:00] melon/server: endpoints =
GET /users (*main.usersResource)
POST /users (*main.usersResource)
GET /user/:name (*main.userResource)
POST /user/:name (*main.userResource)
DELETE /user/:name (*main.userResource)
INFO [2015-11-15T14:09:34.808+09:00] melon/admin: tasks =
POST /tasks/gc (*core.gcTask)
POST /tasks/log (*logging.logTask)
POST /tasks/rmusers (*main.usersTask)
RESTfulのインタフェースも表示されるのが面白いです。Goはコンパイルして使えるので、実行速度はかなり速いと予想されます。MelonはJavaのフレームワーク、Dropwizardにインスパイアされて作られているそうです。
MelonはGo製、MIT Licenseのオープンソース・ソフトウェアです。