LoginSignup
6
7

More than 5 years have passed since last update.

AWS EC2(CentOS)上でGlideを使ってEchoフレームワーク環境を構築する

Last updated at Posted at 2016-09-08

背景

AWS(EC2)上でgolangでのWEB環境を構築したいと思いたちまして、せっかくならパッケージ管理の機構のGlideを使ってみました。

WEBフレームワークは例としてEchoを選択してみました。
Revelでも同様に使えるかと思います。

GoのインストールからGlideのインストールまで

まずは何はともあれgolangのインストール

私のAMIのamzn-mainリポジトリでは1.5がインストールされました。
Glideの要件は1.5以上なのでひとまずはこれで。

1.6以上をインストールしたい場合はEL-7のEPELから入れられそうです。
1.6からvendorディレクトリ配下はオートロードされるとのことなので、可能であれば1.6以上をインストールするほうが良いと思います。

sudo yum install golang

次に、GOPATHを設定します。

今回のインストール方法だと、Glideは$GOPATH/bin配下にインストールされます。
MacでHomebrew経由でインストールした場合はどこに入るかは、試してないのでわかりません...

.bash_profile
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

GOPATH用のディレクトリを作成します。

mkdir -p $GOPATH/bin
mkdir -p $GOPATH/src

Glideは、yumではなくcurl+shでインストールします。

curl https://glide.sh/get | sh

※私の環境だとcurlが古かったのでcurl実行時にエラーが発生しました。
sudo yum update libcurlでcurlを最新化してから実行すると良いかと思います。

これで$GOPATH/bin配下にGlideがインストールされます。

Glideの開始

ここではmyprojectディレクトリで作業を進めることとします。

mkdir $GOPATH/src/myproject
cd $GOPATH/src/myproject

glide create

glide createを実行すると、YAMLファイルが生成されます。

[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]  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)?
y
[INFO]  Looking for dependencies to make suggestions on
[INFO]  --> Scanning for dependencies not using version ranges
[INFO]  --> Scanning for dependencies using commit ids
[INFO]  Gathering information on each dependency
[INFO]  --> This may take a moment. Especially on a codebase with many dependencies
[INFO]  --> Gathering release information for dependencies
[INFO]  --> Looking for dependency imports where versions are commit ids
[INFO]  No proposed changes found. Have a nice day.

glide.yamlを以下のように更新します。

packageには自分のプロジェクトのパッケージ名、import配下に使いたいパッケージを記載します。

package: myproject
import:
    - package: github.com/labstack/echo

Glideでパッケージをインストールします。

glide install
[INFO]  Lock file (glide.lock) does not exist. Performing update.
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching updates for github.com/labstack/echo.
[INFO]  Resolving imports
[INFO]  --> Fetching updates for github.com/labstack/gommon.
[INFO]  --> Fetching updates for golang.org/x/net.
[INFO]  --> Fetching updates for github.com/mattn/go-colorable.
[INFO]  --> Fetching updates for github.com/mattn/go-isatty.
[INFO]  --> Fetching updates for github.com/valyala/fasttemplate.
[INFO]  --> Fetching updates for golang.org/x/sys.
[INFO]  Downloading dependencies. Please wait...
[INFO]  Setting references for remaining imports
[INFO]  Exporting resolved dependencies...
[INFO]  --> Exporting github.com/labstack/echo
[INFO]  --> Exporting github.com/labstack/gommon
[INFO]  --> Exporting github.com/mattn/go-colorable
[INFO]  --> Exporting github.com/mattn/go-isatty
[INFO]  --> Exporting github.com/valyala/fasttemplate
[INFO]  --> Exporting golang.org/x/net
[INFO]  --> Exporting golang.org/x/sys
[INFO]  Replacing existing vendor dependencies
[INFO]  Project relies on 7 dependencies.

以下のディレクトリ構成でEchoがインストールされました。

- myproject
  - glide.lock ロックファイル
  - glide.yaml 先ほど編集したyamlファイル
  - vendor
    - github
      - labstack
        - echo

Echoで開発

EchoのREADMEに書いてあるままコピペします。

myproject/server.go
package main

import (
    "net/http"
    "github.com/labstack/echo"
    "github.com/labstack/echo/engine/standard"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    e.Run(standard.New(":1323"))
}

起動確認します。

1.6からはvendor配下はデフォルトでオートロードされますが、1.5の場合は環境変数を設定する必要があります。GO15VENDOREXPERIMENT=1を設定すればよいそうです。

1.5の場合
GO15VENDOREXPERIMENT=1 go run server.go
1.6以上の場合
go run server.go

これでエラーが表示されなければ、ポート1323でLISTENしている状態になります。

curl http://localhost:1323

を実行してHellor, World!と表示されればOKです。
ひとまずGlideを使ってEchoを導入するところまで辿りつけました。

参考

go1.6とglideでechoフレームワークにHelloWorldする
glide - パッケージ管理のお困りの方へ -

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7