示例#1
0
/*
HTTPHandlerFunc creates an http.Handler out of a function that takes a HTTPContext.

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 basic HTTP needs.
*/
func HTTPHandlerFunc(f func(c HTTPContext) error, scopes ...string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gaeCont := appengine.NewContext(r)
		c := NewHTTPContext(gaeCont, httpcontext.NewHTTPContext(w, r))
		httpcontext.Handle(c, func() error {
			return f(c)
		}, scopes...)
	})
}
示例#2
0
func Handle(c JSONContextLogger, f func() (Resp, error), minAPIVersion, maxAPIVersion int, scopes ...string) {
	httpcontext.Handle(c, func() (err error) {
		if minAPIVersion != 0 && c.APIVersion() < minAPIVersion {
			err = NewError(417, fmt.Sprintf("X-API-Version header has to request API version greater than %v", minAPIVersion), fmt.Sprintf("Headers: %+v", c.Req().Header), nil)
			return
		}
		if maxAPIVersion != 0 && c.APIVersion() > maxAPIVersion {
			err = NewError(417, fmt.Sprintf("X-API-Version header has to request API version less than %v", maxAPIVersion), fmt.Sprintf("Headers: %+v", c.Req().Header), nil)
			return
		}
		resp, err := f()
		if err == nil {
			err = resp.Respond(c)
		}
		return
	}, scopes...)
}