#趣旨
HerokuにGolangでRestAPIを作成する。フレームワークはecho。
#環境情報
PC:Mac(CPUはintel製)
Go:1.17.6 ←Goのver大事。versionは1.16以降をインストールしてください。
開発エディタ:Visual Studio Code
#ディレクトリ構成
ディレクトリ構成
~/go/src/go_echo $ tree
.
├── Procfile
├── go.mod
├── go.sum
└── main.go
※プロジェクトの作成ディレクトリはGOPAHT配下。
#環境設定
.git作成
~/go/src/go_echo $ git init
Initialized empty Git repository in /Users/kawamurakouji/go/src/go_echo/.git/
gomodの作成
~/go/src/go_echo $ go mod init
go: creating new go.mod: module go_echo
ライブラリのインストール
~/go/src/go_echo $ go get github.com/labstack/echo/v4
go get: added github.com/labstack/echo/v4 v4.6.3
go get: added github.com/labstack/gommon v0.3.1
go get: added github.com/mattn/go-colorable v0.1.11
go get: added github.com/mattn/go-isatty v0.0.14
go get: added github.com/valyala/bytebufferpool v1.0.0
go get: added github.com/valyala/fasttemplate v1.2.1
go get: added golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
go get: added golang.org/x/net v0.0.0-20210913180222-943fd674d43e
go get: added golang.org/x/sys v0.0.0-20211103235746-7861aae1554b
go get: added golang.org/x/text v0.3.7
~/go/src/go_echo $ go get github.com/labstack/echo/v4/middleware
#プログラムの作成
main.go
package main
import (
"net/http"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
e.GET("/", hello)
// Use 1323 as default port
port := os.Getenv("PORT")
if port == "" {
port = "1323"
}
// Start server
e.Logger.Fatal(e.Start(":" + port))
}
// Handler
func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
#ローカル実行
main.goの実行
~/go/src/go_echo $ go run main.go
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.6.3
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:1323
→下記画像のように「Hello,World」が表示されることを確認。
#デプロイ準備
Procfileの作成
web: bin/go_echo
【重要】go.modの修正
module go_echo
// +heroku goVersion go1.17 ←1行追加。HerokuにbuildするVersionを指定。
go 1.17
require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/labstack/echo/v4 v4.6.3 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210913180222-943fd674d43e // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
)
gitのcommit
~/go/src/go_echo $ git add ./
~/go/src/go_echo $ git commit -m "initial commit"
[main (root-commit) 00aff07] initial commit
4 files changed, 96 insertions(+)
create mode 100644 Procfile
create mode 100644 go.mod
create mode 100644 go.sum
create mode 100644 main.go
Herokuアプリの作成
~/go/src/go_echo $ heroku create
Creating app... done, ⬢ obscure-fjord-75737
https://XXXXX-XXXXX-XXXXX.herokuapp.com/ | https://git.heroku.com/XXXXX-XXXXX-XXXXX.git
#デプロイ実行
Procfileの作成
git push heroku main
Enumerating objects: 521, done.
Counting objects: 100% (521/521), done.
Delta compression using up to 8 threads
Compressing objects: 100% (309/309), done.
Writing objects: 100% (521/521), 226.26 KiB | 45.25 MiB/s, done.
Total 521 (delta 141), reused 501 (delta 134)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Go app detected
remote: -----> Fetching jq... done
remote:
remote: -----> Detected go modules - go.mod
remote:
remote: -----> Installing go1.12.1
remote: -----> Fetching go1.12.1.linux-amd64.tar.gz... done
remote: !! Installing package '.' (default)
remote: !!
remote: !! To install a different package spec add a comment in the following form to your `go.mod` file:
remote: !! // +heroku install ./cmd/...
remote: !!
remote: -----> Running: go install -v -tags heroku -mod=vendor .
remote: gopkg.in/bluesuncorp/validator.v5
remote: github.com/gin-gonic/gin/render
remote: github.com/manucorporat/sse
remote: github.com/mattn/go-colorable
remote: golang.org/x/net/context
remote: github.com/heroku/x/hmetrics
remote: github.com/heroku/x/hmetrics/onload
remote: github.com/gin-gonic/gin/binding
remote: github.com/gin-gonic/gin
remote: github.com/heroku/go-getting-started
remote:
remote: Compiled the following binaries:
remote: ./bin/go-getting-started
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 5.5M
remote: -----> Launching...
remote: Released v3
remote: https://go-on-heroku.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/go-on-heroku.git
* [new branch] main -> main
→表示されたURLにアクセスすると、「Hello,World」が表示されることを確認。