App Engine使用Go库进行部署

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

App Engine deploy with Go libraries

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是Google App Engine的新手。我遇到了一个无法解决的问题。

我有一个非常简单的应用程序(用Go开发),目录结构如下:

main/
|   model/
|   |   user.go
|   main.go
|   app.yaml

这是main.go的导入部分:

import (
    "github.com/julienschmidt/httprouter"
    "log"
    "net/http"
)

当我在本地运行代码时,它可以正常工作。

但是,当我尝试将其发布到我的Google App Engine实例时,我收到以下错误:

$ gcloud app deploy

You are about to deploy the following services:
 - <MY_APP_ENGINE_URL> (from [<MY_LOCAL_YAML_PATH>])
 Deploying to URL: [<MY_APP_ENGINE_URL>]

Do you want to continue (Y/n)?  Y

Beginning deployment of service [default]...
Some files were skipped. Pass `--verbosity=info` to see which ones.
You may also view the gcloud log file, found at
[<LOCAL_APP_ENGINE_LOG>].
File upload done.
Updating service [default]...failed.                                                                                                                                                              ERROR: (gcloud.app.deploy) Error Response: [9] Deployment contains files that cannot be compiled: Compile failed:
2017/05/27 14:48:24 go-app-builder: build timing: 5×compile (301ms total), 0×link (0s total)
2017/05/27 14:48:24 go-app-builder: failed running compile: exit status 2

main.go:4: can't find import: "github.com/julienschmidt/httprouter"

我做错了什么?

编辑:
这是我的app.yaml文件的内容:

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app
英文:

I'm new on Google App Engine. And, I'm getting an issue that I can't solve.

I've a very simple app (developped in Go) like this :

main/
|   model/
|   |   user.go
|   main.go
|   app.yaml

These are the imports of main.go :

import (
    &quot;github.com/julienschmidt/httprouter&quot;
    &quot;log&quot;
    &quot;net/http&quot;
)

My code works well when I run it locally.

But, when I try to publish it on my Google App Engine instance, I receive this error :

$ gcloud app deploy

You are about to deploy the following services:
 - &lt;MY_APP_ENGINE_URL&gt; (from [&lt;MY_LOCAL_YAML_PATH&gt;])
 Deploying to URL: [&lt;MY_APP_ENGINE_URL&gt;]

Do you want to continue (Y/n)?  Y

Beginning deployment of service [default]...
Some files were skipped. Pass `--verbosity=info` to see which ones.
You may also view the gcloud log file, found at
[&lt;LOCAL_APP_ENGINE_LOG&gt;].
File upload done.
Updating service [default]...failed.                                                                                                                                                             
ERROR: (gcloud.app.deploy) Error Response: [9] Deployment contains files that cannot be compiled: Compile failed:
2017/05/27 14:48:24 go-app-builder: build timing: 5&#215;compile (301ms total), 0&#215;link (0s total)
2017/05/27 14:48:24 go-app-builder: failed running compile: exit status 2

main.go:4: can&#39;t find import: &quot;github.com/julienschmidt/httprouter&quot;

What did I do wrong ?

EDIT :
This is the content of my app.yaml file :

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

答案1

得分: 1

App Engine环境不包含您的依赖项,您可以添加一个脚本来为每个依赖项执行"go get ...",但这样做太繁琐了,而且Go语言有一个解决方案,我们可以将依赖项保存在项目根目录下的vendor文件夹中。

快速解决方案

# 安装godep:
go get -v -u github.com/tools/godep

cd your/project/path
godep save

现在尝试再次部署,您将在项目中看到一个vendor文件夹,请不要删除它并将其添加到您的git源代码中,该文件夹包含所有第三方依赖项,例如您喜欢的httprouter。

注意:您可以使用其他工具来保存您的依赖项。

英文:

App Engine environment doesn't contain your dependencies, you can add an script to do a go get ... for each one but it's too hacky and Go has a solution for that, we can save our dependencies in a vendor folder on the root of our project.

Quick solution:

# Instal godep:
go get -v -u github.com/tools/godep

cd your/project/path
godep save

Now try to deploy again, you'll see a vendor folder in your project, don't remove it and add it to your git source, that folder contains all your third party dependencies like your httprouter (it's my favorite App Engine使用Go库进行部署 )

Note You can use other tools to save your dependencies

答案2

得分: 0

我还没有使用过gcloud工具,但是在以前,当goapp是你必须创建github.com/julienschmidt/httprouter(当然,其中包含库的源代码)直接放在你的main目录下,然后进行部署。

据我所知,App Engine的Go版本目前是1.6,这意味着虽然默认情况下启用了vendoring,但可以关闭 - 也许这就是为什么@Yandry Pozo的建议不起作用的原因。

英文:

I haven't used the gcloud tool, but back in the day when goapp was the tool you had to create github.com/julienschmidt/httprouter (with the lib's source in it, of course) directly under you'r main and then deploy.

AFAIK the App Engine's go version is currently 1.6 which means that while the vendoring is on by default, it can be switched off - perhaps thats the case and thats why @Yandry Pozo's suggestion doesn't work.

huangapple
  • 本文由 发表于 2017年5月28日 06:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/44222046.html
匿名

发表评论

匿名网友

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

确定