duanbei1709
2018-12-16 11:03
浏览 46
已采纳

Google App Engine模块主机名:不是App Engine上下文

I am trying to discover other deployed services on the App Engine. Something like this article suggests.

This is how my code looks like:

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
}

I am calling this function in a regular http handler. The error I've got is: not an App Engine context.

The only difference in between my code and the referenced article is in app engine go version. I am using the new go111 where he's using go1 runtime.

Do you know how to overcome the issue?

图片转代码服务由CSDN问答提供 功能建议

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

这是我的代码的样子:

  import(
“ fmt” 
“ net / http” 
 \  n“ google.golang.org/appengine"
)

func ServiceHostname(服务名字符串,r * http.Request)(字符串,错误){
 ctx:= appengine.NewContext(r)
主机名,错误 := appengine.ModuleHostname(ctx,serviceName,“”,“”)
 if err!= nil {
 return“”,fmt.Errorf(“无法找到服务%s:%v”,serviceName,err)  
} 
返回主机名,nil 
} 
   
 
 

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

我的代码和所引用文章之间的唯一区别是App Engine Go版本。 我正在使用新的 go111 ,其中他正在使用 go1 运行时。

您知道如何解决该问题吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutou7961 2018-12-17 15:35
    已采纳

    I found the solution. You need to call appengine.Main() in your main file even though it shouldn't be necessary to do in the new go111 runtime.

    So the code in question stays the same, you need to register your handlers same as in go1.9 runtime.

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

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

    It's alo mentioned in Writing a main package:

    • Or, if your service is using the google.golang.org/appengine package, include a call to appengine.Main().
    点赞 打赏 评论
  • dongshan7708 2018-12-16 15:08

    The article you referenced was written with the 1st generation standard environment in mind, the 2nd generation (go111) wasn't released at that time:

    October 10, 2018

    Go runtime notes

    The Go 1.11 runtime for the App Engine standard environment is now in beta. A migration guide for moving apps from Go 1.9 to Go 1.11 is available.

    The differences between the two generations are significant (for all languages, not only for go). In the Migrating from the App Engine Go SDK (Optional) section of the migration guide I noticed:

    Which might be related to your error. But I'm not actually a go user, this is just a theory :)

    点赞 打赏 评论

相关推荐 更多相似问题