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 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 铜与钢双金属板叠加在一起每种材料300mm长,18mm宽,4mm厚一端固定并加热至80℃,当加热端温度保持不变时另一端的稳态温度。ansys
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开