Google App Engine ModuleHostname: not an App Engine context

核能气质少年 提交于 2021-01-05 09:24:24

问题


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?


回答1:


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().



回答2:


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:

  • Use request.Context() or your preferred context instead of using appengine.NewContext.

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




回答3:


From what I understand this error is returned when your AppEngine application has the runtime set to go111 and the application imports any of the "google.golang.org/appengine" packages like log, urlfetch, memcache, etc. but the application doesn’t call the “appengine.Main” method.

Therefore you can choose either implementation below:

import (
    "fmt"
    "net/http"

    "google.golang.org/appengine"
)

func init() {
    // register handlers
    http.HandleFunc(“/“, indexHandler)

    appengine.Main()
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)
.......handle the request.......
}

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
}

Or abandon all appengine packages:

import (
    "fmt"
    “log”
    "net/http"
    “os”
)

func main() {
    port := os.Getenv(“PORT”)
    if port == “” {
        port = “8080”
    }

    http.HandleFunc(“/“, indexHandler)

    log.Fatal(http.ListenAndServe(“:”+port, Nil)
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
.....handle request....
}



来源:https://stackoverflow.com/questions/53801551/google-app-engine-modulehostname-not-an-app-engine-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!