panic: not an App Engine context
datastore.go
package main
import (
"log"
"net/http"
"os"
"encoding/json"
"cloud.google.com/go/datastore"
"golang.org/x/net/context"
"google.golang.org/appengine"
)
var datastoreClient *datastore.Client
func main() {
ctx := context.Background()
// Set this in app.yaml when running in production.
projectID := os.Getenv("GCLOUD_DATASET_ID")
var err error
datastoreClient, err = datastore.NewClient(ctx, projectID)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", articlesList)
appengine.Main()
}
type Article struct {
ID int64
Title string
Content string
}
func articlesList(res http.ResponseWriter, req *http.Request) {
ctx := context.Background()
query := datastore.NewQuery("Article").
Order("Title").
Limit(10)
articles := make([]*Article, 0)
_, err := datastoreClient.GetAll(ctx, query, &articles)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(res).Encode(articles)
}
app.yaml
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
#env: flex
#automatic_scaling:
# min_num_instances: 1
#[START env_variables]
env_variables:
GCLOUD_DATASET_ID: <ProjectId>
#[END env_variables]
You have to use newcontext(r) from app engine package
You can't use context.Background()
You have to use newcontext(r) from app engine package
--
You received this message because you are subscribed to a topic in the Google Groups "google-appengine-go" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-appengine-go/i07tIM3ACAk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-appengine-go+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine-go/390438a6-96f6-407b-a3d9-8453c356e23d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.