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 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊