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?

  • 写回答

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().
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line