Skip to content

Instantly share code, notes, and snippets.

View IndianGuru's full-sized avatar

IndianGuru IndianGuru

View GitHub Profile
@IndianGuru
IndianGuru / fetch.go
Created September 7, 2015 09:43
The fetch function
func fetch(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// 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.
q := datastore.NewQuery("Quote").Ancestor(quoteKey(c)).Limit(10)
quotes := make([]Quote, 0, 10)
_, err := q.GetAll(c, &quotes)
@IndianGuru
IndianGuru / add.go
Last active September 20, 2015 07:26
The Add service
// 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)
}
// 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
@IndianGuru
IndianGuru / list.go
Last active September 20, 2015 07:26
The List service
// 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
}
@IndianGuru
IndianGuru / init.go
Last active September 20, 2015 07:29
Register the services
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"
@IndianGuru
IndianGuru / goendpoint.go
Last active September 20, 2015 07:31
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
@IndianGuru
IndianGuru / app.yaml
Last active September 21, 2015 03:17
Cloud endpoints file app.yaml
application: quotesnew-2983
module: default
version: v1
runtime: go
api_version: go1
# Important! Even though there's a catch all routing above,
# without these two lines it's not going to work.
# Make sure you have this:
handlers:
@IndianGuru
IndianGuru / data.go
Last active September 20, 2015 07:24
Declaring struts
// 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:"-"`
@IndianGuru
IndianGuru / geowebgap1.go
Last active September 21, 2015 08:28
geowebgap.go
package geowebgap
import (
"fmt"
"net/http"
)
func init() {
http.HandleFunc("/", handler)
}
@IndianGuru
IndianGuru / app.yaml
Created September 21, 2015 08:38
app.yaml for geowebgap.go
# application is mandatory
application: geowebgap
# version is mandatory
version: 1-0
# runtime is mandatory
runtime: go
# api_version is mandatory
@IndianGuru
IndianGuru / response.go
Created September 21, 2015 09:28
Response struct
type Response struct {
Results []struct {
Geometry struct {
Location struct {
Lat float64
Lng float64
}
}
}
}