Created
August 28, 2014 16:43
-
-
Save techslides/692dd91a8fa4fdd3e367 to your computer and use it in GitHub Desktop.
Static Response for API endpoint with Go, Martini, and GAE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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