Ejemplo n.º 1
0
/*
JSONHandlerFunc creates an http.Handler out of a function that takes a JSONContext.

Requests without tokens, or with tokens without one of the provided scopes, will receive a 401 status unless the scopes slice is empty.

For all your JSON API needs.
*/
func JSONHandlerFunc(f func(c JSONContext) (resp jsoncontext.Resp, err error), minAPIVersion, maxAPIVersion int, scopes ...string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gaeCont := appengine.NewContext(r)
		c := NewJSONContext(gaeCont, jsoncontext.NewJSONContext(httpcontext.NewHTTPContext(w, r)))
		jsoncontext.Handle(c, func() (jsoncontext.Resp, error) {
			return f(c)
		}, minAPIVersion, maxAPIVersion, scopes...)
	})
}
Ejemplo n.º 2
0
/*
DocHandle registeres a path/method/api version route in the provided router, handled by the provided f, wrapping it in appstats.NewHandler,
registering the f-function and its route with jsoncontext.Document and then using jsoncontext.Handle to do something that acts like
JSONHandlerFunc.
*/
func DocHandle(router *mux.Router, f interface{}, path string, method string, minAPIVersion, maxAPIVersion int, scopes ...string) {
	doc, fu := jsoncontext.Document(f, path, method, minAPIVersion, maxAPIVersion, scopes...)
	jsoncontext.Remember(doc)
	router.Path(path).Methods(method).Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gaeCont := appengine.NewContext(r)
		c := NewJSONContext(gaeCont, jsoncontext.NewJSONContext(httpcontext.NewHTTPContext(w, r)))
		jsoncontext.Handle(c, func() (resp jsoncontext.Resp, err error) {
			return fu(c)
		}, minAPIVersion, maxAPIVersion, scopes...)
	}))
}