Exemplo n.º 1
0
// With responds to the client.
func With(w http.ResponseWriter, r *http.Request, status int, data interface{}) {
	var opts *Options
	optsInterface, hasOpts := httpcontext.GetOk(r, OptionsKey)
	if hasOpts {
		opts = optsInterface.(*Options)
	}
	_, multiple := httpcontext.GetOk(r, RespondedKey)
	with(w, r, status, data, opts, multiple)
}
Exemplo n.º 2
0
// Params returns the httprouter.Params for req.
func Params(req *http.Request) httprouter.Params {
	if value, ok := httpcontext.GetOk(req, paramsKey); ok {
		if params, ok := value.(httprouter.Params); ok {
			return params
		}
	}
	return httprouter.Params{}
}
Exemplo n.º 3
0
// WithStatus responds to the client with the specified status.
// Options.StatusData will be called to obtain the data payload, or a default
// payload will be returned:
//     {"status":"I'm a teapot","code":418}
func WithStatus(w http.ResponseWriter, r *http.Request, status int) {
	var opts *Options
	optsInterface, hasOpts := httpcontext.GetOk(r, OptionsKey)
	if hasOpts {
		opts = optsInterface.(*Options)
	}
	_, multiple := httpcontext.GetOk(r, RespondedKey)
	var data interface{}
	if hasOpts && opts.StatusData != nil {
		data = opts.StatusData(w, r, status)
	} else {
		const (
			fieldStatus = "status"
			fieldCode   = "code"
		)
		data = map[string]interface{}{fieldStatus: http.StatusText(status), fieldCode: status}
	}
	with(w, r, status, data, opts, multiple)
}
Exemplo n.º 4
0
// Params returns the httptreemux.Params for req.
func Params(req *http.Request) ParamCol {
	if value, ok := httpcontext.GetOk(req, paramsKey); ok {
		return ParamCol(value.(map[string]string))
	}
	return ParamCol{}
}