Errors with a Golang web app hosted in a Google App Engine environment; the app front-ends BigQuery

huangapple go评论116阅读模式
英文:

Errors with a Golang web app hosted in a Google App Engine environment; the app front-ends BigQuery

问题

我构建了一个使用Golang编写的Web应用程序,它作为Google BigQuery项目的前端。该应用程序有以下导入:

  1. import (
  2. "context"
  3. "html/template"
  4. "log"
  5. "net/http"
  6. "regexp"
  7. "strings"
  8. "strconv"
  9. "cloud.google.com/go/bigquery"
  10. "google.golang.org/api/iterator"
  11. )

还有一个用于BigQuery安全凭据的JSON文件。在本地,它在localhost:8080上运行得很完美。然后,我尝试使用Google App Engine进行托管,但遇到了一些问题。

对于Google App Engine部署,我首先在本地安装了Google Cloud SDK,运行了gcloud init,并安装了以下包:

  1. gcloud components install app-engine-go
  2. bq
  3. core
  4. gsutil
  5. gcloud
  6. beta
  7. app-engine-python

我从main.go中删除了main()函数,并且项目目录中有一个YAML文件。我在项目目录中运行了以下命令:

  1. gcloud config set project {正确的项目ID}

然后在DOS窗口中运行了以下命令:

  1. gcloud app deploy

但是我得到了以下错误(格式化为SO并删除了私人信息):

  1. C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine>gcloud app deploy
  2. ERROR: (gcloud.app.deploy)
  3. Staging command
  4. [C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\goapp-stager.exe
  5. C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine\app.yaml
  6. C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine
  7. c:\-----\-----\appdata\local\temp\--------\--------]
  8. failed with return code [1].
  9. -------------------------------------STDOUT-------------------------------------
  10. -------------------------------------STDERR-------------------------------------
  11. 2017/07/18 18:14:44 failed analyzing C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine:
  12. cannot find package "google.golang.org/appengine/socket" in any of:
  13. ($GOROOT not set)
  14. C:\Go Workspace\src\google.golang.org\appengine\socket (from $GOPATH)
  15. GOPATH: C:\Go Workspace\src\google

我追踪到这个错误是由导入的

  1. "cloud.google.com/go/bigquery"

包引起的;另一个没有使用cloud.google.com/go/bigquery的“测试”应用程序可以正常工作。我尝试在应用程序中导入

  1. google.golang.org/appengine/socket

包,但是我得到了另一个编译错误;看起来这个页面说不要去那里。接下来,我尝试了这个视频中的方法,使用原始应用程序,保留了main.go中的原始main()函数。我在Cloud Shell窗口中输入了以下命令:

  1. gcloud app deploy

但是我得到了以下错误:

  1. $ ---_---------@---------------X------:~/bigqueryApp
  2. $ gcloud app deploy
  3. ERROR: (gcloud.app.deploy) Staging command [/google/google-cloud-sdk/platform/google_appengine/goroot-1.6/bin/go-app-stager
  4. /home/---_---------/bigqueryApp/app.yaml /tmp/---------/---------]
  5. failed with return code [1].
  6. ------------------------------------ STDOUT ------------------------------------
  7. ------------------------------------ STDERR ------------------------------------
  8. 2017/07/18 21:30:23 failed analyzing /home/---_---------/bigqueryApp:
  9. cannot find package "google.golang.org/api/iterator" in any of:
  10. ($GOROOT not set)
  11. /home/---_---------/gopath/src/google.golang.org/api/iterator (from $GOPATH)
  12. /google/gopath/src/google.golang.org/api/iterator
  13. GOPATH: /home/---_---------/gopath:/google/gopath

很明显,该应用程序导入了iterator包。我进行了研究/实验等来修复这两种技术中的错误,但没有成功。如果有人对如何修复这些错误有想法,我会很感激。

谢谢!

英文:

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

  1. import (
  2. "context"
  3. "html/template"
  4. "log"
  5. "net/http"
  6. "regexp"
  7. "strings"
  8. "strconv"
  9. "cloud.google.com/go/bigquery"
  10. "google.golang.org/api/iterator"
  11. )

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

  1. gcloud components install app-engine-go
  2. bq
  3. core
  4. gsutil
  5. gcloud
  6. beta
  7. app-engine-python

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

  1. gcloud config set project {correct project ID}

and in a DOS window, I ran

  1. gcloud app deploy

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

  1. C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine>gcloud app deploy
  2. ERROR: (gcloud.app.deploy)
  3. Staging command
  4. [C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\goapp-stager.exe
  5. C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine\app.yaml
  6. C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine
  7. c:\-----\-----\appdata\local\temp\--------\--------]
  8. failed with return code [1].
  9. -------------------------------------STDOUT-------------------------------------
  10. -------------------------------------STDERR-------------------------------------
  11. 2017/07/18 18:14:44 failed analyzing C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine:
  12. cannot find package "google.golang.org/appengine/socket" in any of:
  13. ($GOROOT not set)
  14. C:\Go Workspace\src\google.golang.org\appengine\socket (from $GOPATH)
  15. GOPATH: C:\Go Workspace\src\google

I traced this bug down to the imported

  1. "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

  1. 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

  1. gcloud app deploy

in a Cloud Shell window. I got this

  1. $ ---_---------@---------------X------:~/bigqueryApp
  2. $ gcloud app deploy
  3. ERROR: (gcloud.app.deploy) Staging command [/google/google-cloud-sdk/platform/google_appengine/goroot-1.6/bin/go-app-stager
  4. /home/---_---------/bigqueryApp/app.yaml /tmp/---------/---------]
  5. failed with return code [1].
  6. ------------------------------------ STDOUT ------------------------------------
  7. ------------------------------------ STDERR ------------------------------------
  8. 2017/07/18 21:30:23 failed analyzing /home/---_---------/bigqueryApp:
  9. cannot find package "google.golang.org/api/iterator" in any of:
  10. ($GOROOT not set)
  11. /home/---_---------/gopath/src/google.golang.org/api/iterator (from $GOPATH)
  12. /google/gopath/src/google.golang.org/api/iterator
  13. 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

得分: 0

解决方案:

1)删除“context”导入

2)导入“google.golang.org/appengine”;请参阅https://github.com/golang/appengine/blob/master/README.md了解有关本地appengine包安装的更多详细信息

3)这个函数

http.HandleFunc("/", bqPage)

调用

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

作为处理程序函数。将第二个req参数传递给构建/调用bigquery客户端的代码:

ctx := appengine.NewContext(req)

// 从Google Cloud控制台获取projectID值:

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

// 创建一个客户端。

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

一旦你有了客户端对象,你就可以开始了。

4)从指向包含main.go文件的目录的DOS窗口中运行

gcloud app deploy

然后用以下命令运行应用程序

gcloud app browse

英文:

Solution:

  1. Remove the "context" import

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

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

    for more details re: the local appengine package installation

  3. This function

    1. http.HandleFunc("/", bqPage)

    calls

    1. 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:

    1. ctx := appengine.NewContext(req)
    2. // Get the projectID value from the Google Cloud Console:
    3. projectID := "--------------"
    4. // Create a client.
    5. 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

    1. gcloud app deploy

    and then run the app with

    1. gcloud app browse

huangapple
  • 本文由 发表于 2017年7月21日 12:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/45228766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定