我试图将我的 Go 应用程序部署到 App Engine。我有以下构建错误:
Starting Step #1 - "builder"
Step #1 - "builder": Pulling image: gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00
Step #1 - "builder": go111_1_11_2_20181111_RC00: Pulling from gae-runtimes/go111_app_builder
Step #1 - "builder": Digest: sha256:51fb36bfa16e7013356867c3a3972986084df93e56258fc258579a5799f0436e
Step #1 - "builder": Status: Downloaded newer image for gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00
Step #1 - "builder": 2018/11/24 18:13:29 Your app is not on your GOPATH, this build may fail.
Step #1 - "builder": 2018/11/24 18:13:29 Building from Go source in /tmp/staging477638319/srv, with main package at ./...
Step #1 - "builder": 2018/11/24 18:13:29 Building /tmp/staging477638319/srv, saving to /tmp/staging477638319/usr/local/bin/start
Step #1 - "builder": 2018/11/24 18:13:30 Wrote build output to /builder/outputs/output
Step #1 - "builder": 2018/11/24 18:13:30 Failed to build app: Your app is not on your GOPATH, please move it there and try again.
Step #1 - "builder": building app with command '[go build -o /tmp/staging477638319/usr/local/bin/start ./...]', env '[PATH=/go/bin:/usr/local/go/bin:/builder/google-cloud-sdk/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=d253e517b16c HOME=/builder/home BUILDER_OUTPUT=/builder/outputs DEBIAN_FRONTEND=noninteractive GOROOT=/usr/local/go/ GOPATH=/go GOPATH=/tmp/staging477638319/srv/gopath]': err=exit status 1, out=srv/main.go:7:2: cannot find package "cloud.google.com/go/firestore" in any of:
Step #1 - "builder": /usr/local/go/src/cloud.google.com/go/firestore (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/cloud.google.com/go/firestore (from $GOPATH)
Step #1 - "builder": srv/main.go:8:2: cannot find package "github.com/gin-gonic/gin" in any of:
Step #1 - "builder": /usr/local/go/src/github.com/gin-gonic/gin (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/github.com/gin-gonic/gin (from $GOPATH)
Step #1 - "builder": srv/main.go:9:2: cannot find package "google.golang.org/api/option" in any of:
Step #1 - "builder": /usr/local/go/src/google.golang.org/api/option (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/google.golang.org/api/option (from $GOPATH)
Finished Step #1 - "builder"
ERROR
ERROR: build step 1 "gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00" failed: exit status 1
我的 app.yaml 文件如下所示:
runtime: go111
handlers:
- url: /api/user
script: auto
- url: /favicon.ico
static_files: build/favicon.ico
upload: build/favicon.ico
- url: /
static_files: build/index.html
upload: build/index.html
- url: /
static_dir: build
我在根目录下的 main.go 文件如下所示:
package main
import (
"context"
"net/http"
"cloud.google.com/go/firestore"
"github.com/gin-gonic/gin"
"google.golang.org/api/option"
)
const firestoreAccountFile = "firebase.json"
const firestoreProjectID = "golang-gae-firestore-template"
type formData struct {
Name string `json:"name" binding:"required"`
Email string `json:"email" binding:"required"`
}
func main() {
// Gin init
r := gin.Default()
// Serve from static build directory
r.StaticFS("/", http.Dir("./build"))
// routes
r.POST("/api/user", userHandler)
// run application on port 8080
r.Run(":8080")
}
func writeLogIfError(c *gin.Context, err error) {
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}
func getNewFirestoreClient(ctx context.Context) (*firestore.Client, error) {
return firestore.NewClient(ctx, firestoreProjectID, option.WithServiceAccountFile(firestoreAccountFile))
}
func userHandler(c *gin.Context) {
ctx := context.Background()
client, err := getNewFirestoreClient(ctx)
writeLogIfError(c, err)
defer client.Close()
// Get form data
var form formData
c.BindJSON(&form)
// [START add user to firestore]
_, _, err = client.Collection("users").Add(ctx, map[string]interface{}{
"name": form.Name,
"email": form.Email,
})
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// [END add user to firestore]
c.JSON(http.StatusOK, gin.H{"status": "user added to db"})
}
如果没有这些构建错误,我将无法成功构建。我试图完全按照 Go on App Engine 的文档进行操作,如果我的应用程序结构与建议的不同,我会感到困惑。关于如何解决此 GOPATH 错误的任何想法?