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 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示