Reputation: 998
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?
Upvotes: 1
Views: 1793
Reputation: 194
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....
}
Upvotes: 1
Reputation: 998
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 toappengine.Main()
.
Upvotes: 5
Reputation: 39834
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 usingappengine.NewContext
.
Which might be related to your error. But I'm not actually a go user, this is just a theory :)
Upvotes: 1