Skip to content

Instantly share code, notes, and snippets.

@techslides
Created August 28, 2014 16:43
Show Gist options
  • Save techslides/692dd91a8fa4fdd3e367 to your computer and use it in GitHub Desktop.
Save techslides/692dd91a8fa4fdd3e367 to your computer and use it in GitHub Desktop.
Static Response for API endpoint with Go, Martini, and GAE
package hello
import (
"net/http"
"github.com/go-martini/martini"
"github.com/martini-contrib/cors"
"github.com/martini-contrib/render"
)
type Resources struct {
Name string `json:"name"`
URI string `json:"uri"`
Methods string `json:"methods"`
}
func init() {
m := martini.Classic()
allowCORSHandler := cors.Allow(&cors.Options{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST"},
AllowHeaders: []string{"Origin"},
})
m.Use(render.Renderer(render.Options{
IndentJSON: true, // Output human readable JSON
}))
m.Get("/", allowCORSHandler, func(r render.Render) {
Shuffle(cards)
json := []Resources{{Name: "cards", URI: "/cards/{players}", Methods: "GET"}}
r.JSON(200, json)
})
http.Handle("/", m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment