Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Last active September 20, 2015 07:31
Show Gist options
  • Select an option

  • Save IndianGuru/288953ce317333f9dff2 to your computer and use it in GitHub Desktop.

Select an option

Save IndianGuru/288953ce317333f9dff2 to your computer and use it in GitHub Desktop.
Complete program
// Package goendpoint exposes a REST API to manage quotes stored in the Google
// Cloud Datastore using the Cloud Endpoints feature of App Engine.
package goendpoint
import (
// Package endpoints will let you write Cloud Endpoints backend in Go.
// http://godoc.org/github.com/GoogleCloudPlatform/go-endpoints/endpoints
"github.com/GoogleCloudPlatform/go-endpoints/endpoints"
// Package context defines the Context type
// Do not store Contexts inside a struct type; instead, pass a
// Context explicitly to each function that needs it. The Context
// should be the first parameter, typically named ctx
// https://godoc.org/golang.org/x/net/context
"golang.org/x/net/context"
// Package datastore provides a client for App Engine's datastore service
// https://godoc.org/google.golang.org/appengine/datastore
"google.golang.org/appengine/datastore"
// Package log provides the means of querying an application's
// logs from within an App Engine application.
// https://godoc.org/google.golang.org/appengine/log
// though not used here
// "google.golang.org/appengine/log"
)
// Declare structs which describe your data
// Quote is a datastore entity that represents a single quote.
// It also serves as (a part of) a response of QuotesAPI.
// Each attribute has its own json tag.
// All the attributes in the struct start with a capital letter
// so that the attribute is accessible outside the package.
// A "-" tag name means that the datastore will ignore that attribute.
type Quote struct {
UID *datastore.Key `json:"uid" datastore:"-"`
Author string `json:"author"`
Message string `json:"message"`
}
// A QuotesAPI struct defines all the endpoints of the quotes API.
// It will have functions for CRUD like Add, List etc.
type QuotesAPI struct {
}
// Quotes contains a slice of quotes. This type is needed because go-endpoints
// only supports pointers to structs as input and output types.
type Quotes struct {
Quotes []Quote `json:"quotes"`
}
// AddRequest contains all the fields needed to create a new Quote.
type AddRequest struct {
Author string
Message string
}
// quoteKey returns the key used for all quote entries.
func quoteKey(c context.Context) *datastore.Key {
return datastore.NewKey(c, "Quote", "default_quote", 0, nil)
}
// List returns a list of all the existing quotes.
func (QuotesAPI) List(c context.Context) (*Quotes, error) {
quotes := []Quote{}
// If we omitted the .Ancestor from this query there would be
// a slight chance that Quote that had just been written would not
// show up in a query.
keys, err := datastore.NewQuery("Quote").Ancestor(quoteKey(c)).GetAll(c, &quotes)
if err != nil {
return nil, err
}
for i, k := range keys {
quotes[i].UID = k
}
return &Quotes{quotes}, nil
}
// Add creates a new quote given the fields in AddRequest, stores it in the
// datastore, and returns it.
func (QuotesAPI) Add(c context.Context, r *AddRequest) (*Quote, error) {
// We set the same parent key on every Quote entity to ensure each Quote
// is in the same entity group. Queries across the single entity group
// will be consistent.
k := datastore.NewIncompleteKey(c, "Quote", quoteKey(c))
t := &Quote{Author: r.Author, Message: r.Message}
k, err := datastore.Put(c, k, t)
if err != nil {
return nil, err
}
t.UID = k
return t, nil
}
func init() {
// register the quotes API with cloud endpoints.
api, err := endpoints.RegisterService(QuotesAPI{}, "quotesService", "v1", "Quotes API", true)
if err != nil {
panic(err)
}
// adapt the name, method, and path for each method.
info := api.MethodByName("List").Info()
info.Name, info.HTTPMethod, info.Path = "getQuotes", "GET", "quotesService"
info = api.MethodByName("Add").Info()
info.Name, info.HTTPMethod, info.Path = "addQuote", "POST", "quotesService"
// start handling cloud endpoint requests.
endpoints.HandleHTTP()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment