I'm trying to test the Datastore functionality using Google App Engine and my code works as expected in the local development server:
// code based on the following guide: https://cloud.google.com/datastore/docs/reference/libraries#client-libraries-install-go
package datastoretest
import (
"fmt"
"log"
"net/http"
"cloud.google.com/go/datastore"
"google.golang.org/appengine"
)
type Task struct {
Description string
}
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
// Set Google Cloud Platform project ID.
projectID := "myProjectID" //note: actual ID is different
// Creates a client.
client, err := datastore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Sets the kind for the new entity.
kind := "Task"
// Sets the name/ID for the new entity.
name := "sampletask1"
// Creates a Key instance.
taskKey := datastore.NameKey(kind, name, nil)
// Creates a Task instance.
task := Task{
Description: "Buy milk",
}
// Saves the new entity.
if _, err := client.Put(ctx, taskKey, &task); err != nil {
log.Fatalf("Failed to save task: %v", err)
}
fmt.Fprint(w, "Saved ", taskKey, ":", task.Description)
}
However, after being deployed to a GAE project, it's returning the following message to the visitor:
Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.