Google App Engine for Go (GAE/Go) ã®éçºç°å¢ã« glide ã§ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããæé ã¡ã¢
æ¤è¨¼ç°å¢
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 éçºç°å¢ã«ã¤ãã¦
ä»åã¯ä¸è¨ãµã¤ãã®æé ã§æ§ç¯ãããã®ã使ç¨ãã¾ã
takaya030.hatenablog.com
GAE/Go ç°å¢ã§ã® vendoring ã®æ³¨æç¹
- app.yaml ããããã£ã¬ã¯ããªä»¥ä¸ã® go ãã¡ã¤ã«ã¯ãã«ãæã«ãã¹ã¦ãªã³ã¯ããããããå¤é¨ããã±ã¼ã¸ã¯å¥ãã£ã¬ã¯ããªã«ã¤ã³ã¹ãã¼ã«ãã (ããã±ã¼ã¸å 㧠syscall ã unsafe ã使ç¨ãã¦ããã¨ã¨ã©ã¼ã«ãªãããããã¤å¿ è¦ãªãã®ã ãããªã³ã¯ããããã«ãããã)
- vendor ãã£ã¬ã¯ããªã¯ $GOPATH/src 以ä¸ã«é ç½®ããã¦ããªã㨠import ã®å¯¾è±¡ã«ãªããªã
ãã£ã¬ã¯ããªæ§æ
ä¸è¨ãè¸ã¾ããä¸ã§ã®ãã£ã¬ã¯ããªæ§æ
+---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 hello.go
å種ãã¡ã¤ã«ã®è©³ç´°
Echo ã使ã£ã¦ "Hello, World!" ã表示ãããµã³ãã«ããã°ã©ã ã§ã
(ååè¨äºãã追å ãå¤æ´ã®ãããã®ã®ã¿)
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/hello.go
package hello import ( "net/http" "github.com/labstack/echo" "github.com/labstack/echo/engine/standard" "github.com/rs/cors" ) func init() { g := e.Group("/hello") g.Use(standard.WrapMiddleware(cors.Default().Handler)) g.GET("", getWorld) g.GET("/:id", getUser) } func getWorld(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") } func getUser(c echo.Context) error { return c.String(http.StatusOK, "Hello, " + c.P(0) + "!") }
glide ã®ã¤ã³ã¹ãã¼ã«
$ cd gaego $ docker-compose run --rm sdk go get github.com/Masterminds/glide
glide.yaml ã®ä½æ
$ cd gaego $ docker-compose run --rm sdk /bin/bash -c "cd src/hello;glide create" Starting gaego_data_1 [INFO] Generating a YAML configuration file and guessing the dependencies [INFO] Attempting to import from other package managers (use --skip-import to skip) [INFO] Scanning code to look for dependencies [INFO] --> Found reference to github.com/labstack/echo [INFO] --> Adding sub-package engine/standard to github.com/labstack/echo [INFO] --> Adding sub-package middleware to github.com/labstack/echo [INFO] --> Found reference to github.com/rs/cors [INFO] Writing configuration file (glide.yaml) [INFO] Would you like Glide to help you find ways to improve your glide.yaml configuration? [INFO] If you want to revisit this step you can use the config-wizard command at any time. [INFO] Yes (Y) or No (N)? N [INFO] You can now edit the glide.yaml file. Consider: [INFO] --> Using versions and ranges. See https://glide.sh/docs/versions/ [INFO] --> Adding additional metadata. See https://glide.sh/docs/glide.yaml/ [INFO] --> Running the config-wizard command to improve the versions in your configuration
glide ã«ããããã±ã¼ã¸ã¤ã³ã¹ãã¼ã«
ä¸è¨æä½ã§ä½æããã glide.yaml ã§ã¯ Echo ã®ææ°çãã¤ã³ã¹ãã¼ã«ããã¾ãããgo1.6 ã§ã¯åä½ããªãã®ã§æ§ãã¼ã¸ã§ã³ãæå®ããããã«å¤æ´ãã¾ã
(glide.yaml 4è¡ç®ã« "version: v2.2.0" ã追å )
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
ãã®å¾ã以ä¸ã®æä½ã§ã¤ã³ã¹ãã¼ã«
$ cd gaego $ docker-compose run --rm sdk /bin/bash -c "cd src/hello;glide up" Starting gaego_data_1 [INFO] Downloading dependencies. Please wait... [INFO] --> Fetching github.com/labstack/echo [INFO] --> Fetching github.com/rs/cors [INFO] --> Setting version for github.com/labstack/echo to v2.2.0. [INFO] Resolving imports [INFO] --> Fetching github.com/labstack/gommon [INFO] --> Fetching github.com/dgrijalva/jwt-go [INFO] --> Fetching github.com/mattn/go-isatty [INFO] --> Fetching github.com/valyala/fasttemplate [INFO] --> Fetching golang.org/x/net [INFO] --> Fetching github.com/mattn/go-colorable [INFO] --> Fetching golang.org/x/sys [INFO] --> Fetching github.com/valyala/bytebufferpool [INFO] Downloading dependencies. Please wait... [INFO] Setting references for remaining imports [INFO] Exporting resolved dependencies... [INFO] --> Exporting github.com/valyala/bytebufferpool [INFO] --> Exporting github.com/labstack/echo [INFO] --> Exporting github.com/dgrijalva/jwt-go [INFO] --> Exporting github.com/mattn/go-colorable [INFO] --> Exporting github.com/rs/cors [INFO] --> Exporting github.com/mattn/go-isatty [INFO] --> Exporting github.com/labstack/gommon [INFO] --> Exporting github.com/valyala/fasttemplate [INFO] --> Exporting golang.org/x/sys [INFO] --> Exporting golang.org/x/net [INFO] Replacing existing vendor dependencies [INFO] Project relies on 10 dependencies.
以ä¸ã§ www/src/hello/vendor 以ä¸ã« Echo v2 ãã¤ã³ã¹ãã¼ã«ããã¾ãã
åä½ç¢ºèª
以ä¸ã®ã³ãã³ãã§éçºç¨ãµã¼ãã¼ãèµ·åãã¾ã
$ docker-compose up -d
web ãã©ã¦ã¶ã§ http://192.168.99.100:8080/hello ã«ã¢ã¯ã»ã¹ã㦠"Hello, world!" ã®æåã表示ãããã°æåã§ã
http://192.168.99.100:8080/hello/takaya030 ã«ã¢ã¯ã»ã¹ãã㨠"Hello, takaya030!" ã¨è¡¨ç¤ºããã¾ã