Documentation ¶
Index ¶
- Variables
- func AddCommon(mx *web.Mux)
- func AddCommon15(mx *web.Mux, log log15.Logger)
- func ContextLogger(c *web.C, h http.Handler) http.Handler
- func EnvAdd(m map[string]interface{}) web.MiddlewareType
- func ErrorInternal(c web.C, rw http.ResponseWriter, err error)
- func ErrorString(c web.C, rw http.ResponseWriter, code int, str string)
- func Errorf(c web.C, rw http.ResponseWriter, code int, message string, args ...interface{})
- func FormParser(c *web.C, h http.Handler) http.Handler
- func GetJSONBody(c *web.C, h http.Handler) http.Handler
- func Logger15(logger log15.Logger) web.MiddlewareType
- func ParamsLogger(verbose bool) web.MiddlewareType
- func Printf(rw http.ResponseWriter, code int, message string, args ...interface{})
- func Recoverer(c *web.C, h http.Handler) http.Handler
- func RequestID(c *web.C, h http.Handler) http.Handler
- func WriteJSON(c web.C, rw http.ResponseWriter, code int, obj interface{})
- func WriteString(rw http.ResponseWriter, code int, str string)
Constants ¶
This section is empty.
Variables ¶
var ApplicationJSON = "application/json"
var ContextLog string = "log"
ContextLog is the hash key in which ContextLogger places the log15 context logger
var RequestIDHeader = "X-Request-Id"
Functions ¶
func AddCommon15 ¶
Add the following common middlewares: EnvInit, RealIP, RequestID, Logger15, Recoverer, FormParser
func ContextLogger ¶
ContextLogger injects a log15 logger that is initialized to print the request ID. It assumes that c.Env[middleware.RequestIDKey] is set (e.g. by the RequestID middleware). It puts the logger into c.Env[gojiutil.ContextLogger].
func EnvAdd ¶
func EnvAdd(m map[string]interface{}) web.MiddlewareType
Create a simple middleware that merges a map into c.Env
func ErrorInternal ¶
func ErrorInternal(c web.C, rw http.ResponseWriter, err error)
Convenience function to produce an internal error based on the err argument
func ErrorString ¶
Produce a text/plain error response into the responseWriter and also sets the context to reflect the error in a way that the logger groks properly. For 500 errors a generic error is returned and the details are only logged.
func FormParser ¶
FormParser simply calls Request.FormParse to get all params into the request
func GetJSONBody ¶
GetJSONBody is a middleware to read and parse an application/json body and store it in c.Env["json"] as a map[string]interface{}, which can be easily mapped to a proper struct using github.com/mitchellh/mapstructure. This middleware is pretty permissive: it allows for having no content-length and no content-type as long as either there's no body or the body parses as json.
func Logger15 ¶
func Logger15(logger log15.Logger) web.MiddlewareType
Create a logger middleware that logs HTTP requests and results to log15 Assumes that c.Env is allocated, use goji/middleware.EnvInit for that Prints a requestID if one is present, use goji/middleware.RequestID Prints the requestor's IP address, use goji/middleware.RealIP
func ParamsLogger ¶
func ParamsLogger(verbose bool) web.MiddlewareType
ParamsLogger logs all query string / form parameters primarily for debug purposes. It logs at the start of a request using log15.Debug (or c.Env[ContextLog].Debug if defined) unlike the Logger15 middleware, which logs at the end. If verbose is true then the c.URLParams and the c.Env hashes are also logged
func Recoverer ¶
Create a panic-catching middleware for Echo that ensures the server doesn't die if one of the handlers panics. Also puts the call stack into the Echo Context which causes the logger middleware to log it.
func RequestID ¶
RequestID injects a request ID into the context of each request. Retrieve it using goji's GetReqID(). If the incoming request has a header of RequestIDHeader then that value is used, else a random value is generated
func WriteString ¶
func WriteString(rw http.ResponseWriter, code int, str string)
Types ¶
This section is empty.