echoのセットアップ(作業メモ)
echo Goで書かれたWebサーバー。
これを使ってAPIサーバー作りたい。
glideのインストール
Goのパッケージ管理ツールであるglideをインストール。
Homebrewで一発。
$brew install glide
作業ディレクトリ作成
$ mkdir -p goworks/src/echo_sample
$ cd goworks
作業ディレクトリ配下にGOPATHを設定
cf. direnv参考サイト
$ vim .envrc
export GOPATH=$(pwd) ←追記
$ direnv allow
direnv: loading .envrc
direnv: export +GOPATH
echo_sample配下にgoファイルを作成。
$ vim server.go
公式のGetStartページ からコピペ。
package main
import (
"net/http"
"github.com/labstack/echo"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}
glideでechoをインストール
$ glide create
[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 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] --> Detected semantic version. Setting version for github.com/labstack/echo to v3.2.1.
[INFO] Resolving imports
[INFO] --> Fetching updates for github.com/labstack/gommon.
[INFO] --> Fetching updates for golang.org/x/crypto.
[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] --> Fetching updates for github.com/valyala/bytebufferpool.
[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-isatty
[INFO] --> Exporting github.com/valyala/fasttemplate
[INFO] --> Exporting github.com/mattn/go-colorable
[INFO] --> Exporting github.com/valyala/bytebufferpool
[INFO] --> Exporting golang.org/x/crypto
[INFO] --> Exporting golang.org/x/sys
[INFO] Replacing existing vendor dependencies
[INFO] Project relies on 8 dependencies.
goworks/src配下に、こんな感じの構成でechoがインストールされる。
インストール済みパッケージの情報はglide.lockに記録されてる。
echo_smaple/
├── glide.lock
├── glide.yaml
└── vendor
└── github.com
└── labstack
└── echo
echoを動かしてみる
$ go run server.go
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v3.2.1
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:1323
動いた。