#GAE
とりあえず導入するだけなので、公式のサンプル通りにやる。
$ go get -u github.com/gin-gonic/gin
go.mod
が更新された。
module goae
go 1.14
require (
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
package main
import (
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
r := gin.New()
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
})
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
log.Printf("Listening on port %s", port)
r.Run(":" + port)
}
$ go run main.go
2020/06/08 14:16:17 Defaulting to port 8080
[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 / --> main.main.func1 (1 handlers)
[GIN-debug] GET /ping --> main.main.func2 (1 handlers)
2020/06/08 14:16:17 Listening on port 8080
[GIN-debug] Listening and serving HTTP on :8080
$ curl localhost:8080
Hello World!%
$ curl localhost:8080/ping
pong%
問題なさそう。
$ gcloud app deploy
ブラウザで確認。
$ gcloud app browse
問題なさそう。