dpjs2005 2018-09-28 05:21
浏览 54
已采纳

如何使Golang杜松子酒与Google App Engine一起使用?

My GOPATH is $HOME/go, I put my project's source code in $HOME/go/src/myproj

and there are two files:

app.yaml:

application: hello
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

and hello.go

package hello

import (
        "net/http"
        "github.com/gin-gonic/gin"
)

func ping(c *gin.Context) {
        c.JSON(200, gin.H{
                "message": "pong",
        })
}

func init() {
        r := gin.Default()

        api := r.Group("/api")
        {
          api.GET("/ping", ping)
        }

        http.Handle("/", r)
}

Then I run dev_appserver.py .

It works,

curl http://127.0.0.1:8080/api/ping
{"message":"pong"}

Then I decide to split hello.go this way:

new hello.go

package hello

import (
        "net/http"
        "github.com/gin-gonic/gin"
        "./api"
        // "myproj/api" // does not work too
)

func init() {
        r := gin.Default()

        api.addRoute()

        http.Handle("/", r)
}

and a api folder, and api/api.go file

package api

import (
        "github.com/gin-gonic/gin"
)

func ping(c *gin.Context) {
        c.JSON(200, gin.H{
                "message": "pong",
        })
}

func addRoute() {
        api := r.Group("/api")
        {
          api.GET("/ping", ping)
        }
}

Then I run dev_appserver.py . again but got this error:

ERROR 2018-09-28 05:17:47,653 instance_factory.py:229] Failed to build Go application: (Executed command: /Users/gaco/.google-cloud-sdk/platform/google_appengine/goroot-1.9/bin/go-app-builder -app_base /Users/gaco/go/src/myproj -api_version go1 -arch 6 -dynamic -goroot /Users/gaco/.google-cloud-sdk/platform/google_appengine/goroot-1.9 -nobuild_files ^^$ -incremental_rebuild -unsafe -print_extras_hash hello.go api/api.go)

2018/09/28 14:17:47 go-app-builder: Failed parsing input: app file api.go conflicts with same file imported from GOPATH

WARNING 2018-09-28 05:17:47,654 instance.py:297] Could not get PID of instance ERROR 2018-09-28 05:17:47,654 instance.py:300] '_GoBuildFailureRuntimeProxy' object has no attribute '_process'

What is the problem? How can I solve it?

  • 写回答

1条回答 默认 最新

  • drll42469 2018-09-28 06:31
    关注

    I just realized this is a problem I had once in my own App Engine project. The problem is that because your api folder is nested beneath your myproject folder. The SDK doesn't handle the imports well and things end up recursively imported, hence the 2018/09/28 14:17:47 go-app-builder: Failed parsing input: app file api.go conflicts with same file imported from GOPATH error.

    Effectively, you have to make sure you are never importing something down a directory, which is really annoying.

    To solve this, given the info here, you would need to break this out into a more flat directory structure. Something like this:

    myproject/main // or however you want to name it
    myproject/api
    

    With nothing in the bare myproject folder. Then there is no possibility for it to be included recursively.

    For reference, my own project has a website, api, db, and 3rd party api structs, so my structure is as you'd expect given the solution mentioned. e.g.

    project/website
    project/api
    project/db
    project/external
    

    Note that both my website and api are separate services, so they both have their own app.yaml in their respective directories. You would just have the one. Also in my example, both projects import db and external.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载