Errors with a Golang web app hosted in a Google Ap

2019-07-30 05:11发布

I built a Golang web app that front-ends a Google BigQuery project. The app has these imports

import (
      "context"
      "html/template"
      "log"
      "net/http"
      "regexp"
      "strings"
      "strconv"
      "cloud.google.com/go/bigquery"
      "google.golang.org/api/iterator"
)

and a JSON file for the BigQuery security credentials. Locally, it works perfectly at localhost:8080. Then, I tried to host it with Google App Engine and I hit some bugs.

For Google App Engine deployment, I first installed Google Cloud SDK locally, I ran gcloud init, and I installed the

gcloud components install app-engine-go
bq
core
gsutil
gcloud
beta
app-engine-python

packages. I removed the main() function from main.go, and the project directory has a YAML file. I ran

gcloud config set project {correct project ID}

and in a DOS window, I ran

gcloud app deploy

at the project directory. I got this error (formatted for SO and to remove private information):

C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine>gcloud app deploy

ERROR: (gcloud.app.deploy)
Staging command

[C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\goapp-stager.exe    
C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine\app.yaml    
C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine    
c:\-----\-----\appdata\local\temp\--------\--------]

failed with return code [1].

-------------------------------------STDOUT-------------------------------------    
-------------------------------------STDERR-------------------------------------

2017/07/18 18:14:44 failed analyzing C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine:

cannot find package "google.golang.org/appengine/socket" in any of:
        ($GOROOT not set)
        C:\Go Workspace\src\google.golang.org\appengine\socket (from $GOPATH)    
GOPATH: C:\Go Workspace\src\google

I traced this bug down to the imported

"cloud.google.com/go/bigquery"

package; another “test” app without cloud.google.com/go/bigquery works OK using this technique. I tried to import the

google.golang.org/appengine/socket

package in the app and I got another compile error; it looks like this page says don’t even go there. Next, I tried the ideas in this vid using the original application, keeping the original main() function in main.go. I typed

gcloud app deploy

in a Cloud Shell window. I got this

$ ---_---------@---------------X------:~/bigqueryApp
$ gcloud app deploy
ERROR: (gcloud.app.deploy) Staging command [/google/google-cloud-sdk/platform/google_appengine/goroot-1.6/bin/go-app-stager
/home/---_---------/bigqueryApp/app.yaml /tmp/---------/---------]
failed with return code [1].

------------------------------------ STDOUT ------------------------------------    
------------------------------------ STDERR ------------------------------------

2017/07/18 21:30:23 failed analyzing /home/---_---------/bigqueryApp:
cannot find package "google.golang.org/api/iterator" in any of:
        ($GOROOT not set)
        /home/---_---------/gopath/src/google.golang.org/api/iterator (from $GOPATH)
        /google/gopath/src/google.golang.org/api/iterator
GOPATH: /home/---_---------/gopath:/google/gopath

error. The app clearly imports the iterator package. I researched / experimented / etc. to fix the bugs in both techniques but no luck. If someone has ideas re: how to fix these bugs, I’d like to know them and I’d be grateful.

Thank you!

1条回答
Ridiculous、
2楼-- · 2019-07-30 05:56

Solution:

1) Remove the "context" import

2) Import "google.golang.org/appengine"; see

    [https://github.com/golang/appengine/blob/master/README.md][1]

for more details re: the local appengine package installation

3) This function

    http.HandleFunc("/", bqPage)

calls

    bqPage(w http.ResponseWriter, req *http.Request)

as the handler function. Pass that second req parameter down to the code that builds / calls the bigquery client:

    ctx := appengine.NewContext(req)

    // Get the projectID value from the Google Cloud Console:

    projectID := "--------------"

    // Create a client.

    client, err := bigquery.NewClient(ctx, projectID)

Once you have the client object, you're in business.

4) From a DOS window pointed to the directory hosting the main.go file, run

    gcloud app deploy

and then run the app with

    gcloud app browse
查看更多
登录 后发表回答