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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)