takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

glide で GAE/Go プロジェクトのパッケージ管理

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!" の文字が表示されれば成功です
f:id:takaya030:20170805155642p:plain

http://192.168.99.100:8080/hello/takaya030 にアクセスすると "Hello, takaya030!" と表示されます
f:id:takaya030:20170805155700p:plain