Google App Engine for Go (GAE/Go) ã§åä½ãã¦ãã Echo ã®ç°å¢ã§ã»ãã·ã§ã³ã使ç¨ããæé ã¡ã¢
æ¤è¨¼ç°å¢
Windows10 Home Edition VirtualBox 5.1.22 Docker version 17.05.0-ce, build 89658be docker-compose version 1.6.2, build 4d72027
GAE/Go, Echo éçºç°å¢ã«ã¤ãã¦
ä»åã¯ä¸è¨ãµã¤ãã®æé ã§æ§ç¯ãããã®ã使ç¨ãã¾ã
takaya030.hatenablog.com
session ããã±ã¼ã¸ã«ã¤ãã¦
gorilla/sessions ã GAE 対å¿ãããä¸è¨ããã±ã¼ã¸ã使ç¨ãã¾ã
github.com
ãã£ã¬ã¯ããªæ§æ
+---gaego | | docker-compose.yml | | | +---config | | | +---data | | Dockerfile | | | +---glide | | | +---sdk | Dockerfile | +---logs | +---www | +---app | app.yaml | main.go | +---src | +---hello app.go app-engine.go app-standalone.go glide.yaml hello.go
å種ãã¡ã¤ã«ã®è©³ç´°
www/app/app.yaml
runtime: go api_version: go1 handlers: - url: /.* script: _go_app
www/app/main.go
package main import ( _ "hello" ) func init() { }
www/src/hello/app.go
package hello var e = createMux()
www/src/hello/app-engine.go
// +build appengine package hello import ( "github.com/labstack/echo" "github.com/labstack/echo/engine/standard" "net/http" ) func createMux() *echo.Echo { e := echo.New() // note: we don't need to provide the middleware or static handlers, that's taken care of by the platform // app engine has it's own "main" wrapper - we just need to hook echo into the default handler s := standard.New("") s.SetHandler(e) http.Handle("/", s) return e } // main()ã¯æ¸ããªãã¦è¯ã
www/src/hello/app-standalone.go
// +build !appengine,!appenginevm package hello import ( "github.com/labstack/echo" "github.com/labstack/echo/engine/standard" "github.com/labstack/echo/middleware" ) // ãã°ã®è¨å®ãéçãã¡ã¤ã«ã®å ´ææå®ãªã©ããã¦ãã¾ã func createMux() *echo.Echo { e := echo.New() e.Use(middleware.Recover()) e.Use(middleware.Logger()) e.Use(middleware.Gzip()) e.Use(middleware.Static("public")) return e } func main() { e.Run(standard.New(":8080")) }
www/src/hello/glide.yaml
package: hello import: - package: github.com/labstack/echo version: v2.2.0 subpackages: - engine/standard - middleware - package: github.com/rs/cors - package: github.com/gorilla/sessions - package: github.com/gorilla/context - package: github.com/dsoprea/goappenginesessioncascade - package: github.com/dsoprea/go-logging
www/src/hello/hello.go
package hello import ( "net/http" "strconv" "github.com/labstack/echo" "github.com/labstack/echo/engine/standard" "github.com/rs/cors" "github.com/dsoprea/goappenginesessioncascade" ) const ( sessionName = "MainSession" ) var ( sessionSecret = []byte("SessionSecret") sessionStore = cascadestore.NewCascadeStore(cascadestore.DistributedBackends, sessionSecret) ) func init() { g := e.Group("/hello") g.Use(standard.WrapMiddleware(cors.Default().Handler)) g.GET("", getWorld) } // hello world func getWorld(c echo.Context) error { w := c.Response().(*standard.Response).ResponseWriter r := c.Request().(*standard.Request).Request var foo int if sess, err := sessionStore.Get(r, sessionName); err != nil { panic(err) } else { if val, found := sess.Values["foo"]; found == false { foo = 0 } else { foo = val.(int) + 1 } sess.Values["foo"] = foo if err := sess.Save(r, w); err != nil { panic(err) } } return c.String(http.StatusOK, "Hello, World! foo is " + strconv.Itoa(foo)) }
glide ã«ããããã±ã¼ã¸ã¤ã³ã¹ãã¼ã«
$ cd gaego $ docker-compose run --rm sdk /bin/bash -c "cd src/hello;glide up"
åä½ç¢ºèª
以ä¸ã®ã³ãã³ãã§éçºç¨ãµã¼ãã¼ãèµ·åãã¾ã
$ docker-compose up -d
web ãã©ã¦ã¶ã§ http://192.168.99.100:8080/hello ã«ã¢ã¯ã»ã¹ãã¦ä»¥ä¸ã®ãããªæååã表示ãããã°æåã§ã
Hello, World! foo is 0
"foo is" ã®æ°å¤ã¯ã»ãã·ã§ã³ã«ä¿åããã¦ãã¦ãã¢ã¯ã»ã¹ã®åº¦ã«ã¤ã³ã¯ãªã¡ã³ããã¾ã
ãã©ã¦ã¶ããªãã¼ãããã¨æ°å¤ãã¤ã³ã¯ãªã¡ã³ãããã®ã確èªã§ãã¾ã