google-app-engine - Google App Engine 模块主机名 : not an App Engine context

标签 google-app-engine go google-cloud-platform

我正在尝试发现 App Engine 上的其他已部署服务。类似于 this文章建议。

我的代码是这样的:

import (
    "fmt"
    "net/http"

    "google.golang.org/appengine"
)

func ServiceHostname(serviceName string, r *http.Request) (string, error) {
    ctx := appengine.NewContext(r)
    hostname, err := appengine.ModuleHostname(ctx, serviceName, "", "")
    if err != nil {
        return "", fmt.Errorf("unable to find service %s: %v", serviceName, err)
    }
    return hostname, nil
}

我在常规的 http 处理程序中调用此函数。我遇到的错误是:不是 App Engine 上下文

我的代码与引用文章之间的唯一区别在于 App Engine Go 版本。我正在使用新的 go111 而他正在使用 go1 运行时。

你知道如何克服这个问题吗?

最佳答案

我找到了解决方案。您需要在主文件中调用 appengine.Main(),即使在新的 go111 运行时中没有必要这样做。

所以有问题的代码保持不变,您需要像在 go1.9 运行时一样注册您的处理程序。

func main() {
    http.HandleFunc("/serveurl", handle)
    appengine.Main()
}

来源:https://groups.google.com/d/msg/google-appengine-go/ZcASFMWJKpE/7iwGirNiBgAJ

Writing a main package 中也提到了它:

  • Or, if your service is using the google.golang.org/appengine package, include a call to appengine.Main().

关于google-app-engine - Google App Engine 模块主机名 : not an App Engine context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53801551/

相关文章:

java - Android 存储数据的策略

java - 如何在 netbeans 中为 GAE 配置 vosao cms

django - Google App Engine - 错误(仅 "python27"运行时支持库条目)

go - 为什么我会根据调用 BindPFlag 的位置收到零指针错误?

python - 删除 Flask Web 应用程序的终端依赖性

java - Appengine 端点 Maven 构建错误与/生成源

go - 为什么 curl 为我的用户提供了错误的 json?

Golang后台处理

google-cloud-platform - 如何从 Google Data Studio 中的第一页跳转到下一页

hadoop - Google Dataflow 的工作流程编排