環境
- CentOS7
- go1.x
- gin1.x
- Nginx
gin インストール
$ cd /path/to/go/src/your/project
$ vi main.go
main.go
package main
func main() {}
dep でインストール
$ dep init
$ dep ensure -add github.com/gin-gonic/gin
gin 確認
main.go
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and server on 0.0.0.0:8080
}
起動
$ go run main.go
ctrl + c で終了
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
ビルド
- supervisor とか使う時には必ずビルドする
- 作業が終わったら必ずビルドする
$ go build main.go
Nginx の設定