Documentation
¶
Index ¶
- func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next httpserver.Handler) httpserver.Handler
- func DelResponseHeaderHandler(headerName string) func(next httpserver.Handler) httpserver.Handler
- func DumpRequestHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
- func DumpResponseHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
- func FromRequest(r *http.Request) *zerolog.Logger
- func HeaderHandler(headerName string) func(next httpserver.Handler) httpserver.Handler
- func IDFromRequest(r *http.Request, headerName string) (id xid.ID, err error)
- func MethodHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
- func NewHandler(log zerolog.Logger) func(httpserver.Handler) httpserver.Handler
- func RefererHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
- func RemoteAddrHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
- func RequestHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
- func RequestIDHandler(fieldKey, headerName string) func(next httpserver.Handler) httpserver.Handler
- func ResponseHeaderHandler(headerName, valType string) func(next httpserver.Handler) httpserver.Handler
- func URLHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
- func UserAgentHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
- func WithLog(h httpserver.Handler, dir, splitBy string, once sync.Once) httpserver.Handler
- type Chain
- type Config
- type Constructor
- type ResponseLog
- type ResponseProxyWriter
- type ZLog
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessHandler ¶
func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next httpserver.Handler) httpserver.Handler
AccessHandler returns a handler that call f after each request.
func DelResponseHeaderHandler ¶
func DelResponseHeaderHandler(headerName string) func(next httpserver.Handler) httpserver.Handler
func DumpRequestHandler ¶
func DumpRequestHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
func DumpResponseHandler ¶
func DumpResponseHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
func FromRequest ¶
FromRequest gets the logger in the request's context. This is a shortcut for log.Ctx(r.Context())
func HeaderHandler ¶
func HeaderHandler(headerName string) func(next httpserver.Handler) httpserver.Handler
HeaderHandler adds the request's headerName from Header as a field to the context's logger using headerName as field key.
func IDFromRequest ¶
IDFromRequest returns the unique id associated to the request if any.
func MethodHandler ¶
func MethodHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
MethodHandler adds the request method as a field to the context's logger using fieldKey as field key.
func NewHandler ¶
func NewHandler(log zerolog.Logger) func(httpserver.Handler) httpserver.Handler
NewHandler injects log into requests context.
func RefererHandler ¶
func RefererHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
RefererHandler adds the request's referer as a field to the context's logger using fieldKey as field key.
func RemoteAddrHandler ¶
func RemoteAddrHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
RemoteAddrHandler adds the request's remote address as a field to the context's logger using fieldKey as field key.
func RequestHandler ¶
func RequestHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
RequestHandler adds the request method and URL as a field to the context's logger using fieldKey as field key.
func RequestIDHandler ¶
func RequestIDHandler(fieldKey, headerName string) func(next httpserver.Handler) httpserver.Handler
RequestIDHandler returns a handler setting a unique id to the request which can be gathered using IDFromRequest(req). This generated id is added as a field to the logger using the passed fieldKey as field name. The id is also added as a response header if the headerName is not empty.
The generated id is a URL safe base64 encoded mongo object-id-like unique id. Mongo unique id generation algorithm has been selected as a trade-off between size and ease of use: UUID is less space efficient and snowflake requires machine configuration.
func ResponseHeaderHandler ¶
func ResponseHeaderHandler(headerName, valType string) func(next httpserver.Handler) httpserver.Handler
func URLHandler ¶
func URLHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
URLHandler adds the requested URL as a field to the context's logger using fieldKey as field key.
func UserAgentHandler ¶
func UserAgentHandler(fieldKey string) func(next httpserver.Handler) httpserver.Handler
UserAgentHandler adds the request's user-agent as a field to the context's logger using fieldKey as field key.
func WithLog ¶
func WithLog(h httpserver.Handler, dir, splitBy string, once sync.Once) httpserver.Handler
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain acts as a list of httpserver.Handler constructors. Chain is effectively immutable: once created, it will always hold the same set of constructors in the same order.
func NewChain ¶
func NewChain(constructors ...Constructor) Chain
New creates a new chain, memorizing the given list of middleware constructors. New serves no other function, constructors are only called upon a call to Then().
func (Chain) Append ¶
func (c Chain) Append(constructors ...Constructor) Chain
Append extends a chain, adding the specified constructors as the last ones in the request flow.
Append returns a new chain, leaving the original one untouched.
stdChain := NewChain(m1, m2) extChain := stdChain.Append(m3, m4) // requests in stdChain go m1 -> m2 // requests in extChain go m1 -> m2 -> m3 -> m4
func (Chain) Extend ¶
Extend extends a chain by adding the specified chain as the last one in the request flow.
Extend returns a new chain, leaving the original one untouched.
stdChain := NewChain(m1, m2) ext1Chain := NewChain(m3, m4) ext2Chain := stdChain.Extend(ext1Chain) // requests in stdChain go m1 -> m2 // requests in ext1Chain go m3 -> m4 // requests in ext2Chain go m1 -> m2 -> m3 -> m4
Another example:
aHtmlAfterNosurf := NewChain(m2)
aHtml := NewChain(m1, func(h httpserver.Handler) httpserver.Handler {
csrf := nosurf.New(h)
csrf.SetFailureHandler(aHtmlAfterNosurf.ThenFunc(csrfFail))
return csrf
}).Extend(aHtmlAfterNosurf)
// requests to aHtml hitting nosurfs success handler go m1 -> nosurf -> m2 -> target-handler
// requests to aHtml hitting nosurfs failure handler go m1 -> nosurf -> m2 -> csrfFail
func (Chain) Then ¶
func (c Chain) Then(h httpserver.Handler) httpserver.Handler
Then chains the middleware and returns the final httpserver.Handler.
New(m1, m2, m3).Then(h)
is equivalent to:
m1(m2(m3(h)))
When the request comes in, it will be passed to m1, then m2, then m3 and finally, the given handler (assuming every middleware calls the following one).
A chain can be safely reused by calling Then() several times.
stdStack := NewChain(ratelimitHandler, csrfHandler) indexPipe = stdStack.Then(indexHandler) authPipe = stdStack.Then(authHandler)
Note that constructors are called on every call to Then() and thus several instances of the same middleware will be created when a chain is reused in this way. For proper middleware, this should cause no problems.
func (Chain) ThenFunc ¶
func (c Chain) ThenFunc(fn httpserver.HandlerFunc) httpserver.Handler
ThenFunc works identically to Then, but takes a HandlerFunc instead of a Handler.
The following two statements are equivalent:
c.Then(httpserver.HandlerFunc(fn)) c.ThenFunc(fn)
ThenFunc provides all the guarantees of Then.
type Constructor ¶
type Constructor func(httpserver.Handler) httpserver.Handler
A constructor for a piece of middleware. Some middleware use this constructor out of the box, so in most cases you can just pass somepackage.New
type ResponseLog ¶
type ResponseLog struct {
Request *http.Request
StatusCode int `json:"staus_code"`
Body string `json:"body"`
Header string `json:"header"`
}
func (ResponseLog) DumpResponse ¶
func (rl ResponseLog) DumpResponse() string
type ResponseProxyWriter ¶
type ResponseProxyWriter struct {
Body []byte
Code int
SourceHeader http.Header
// contains filtered or unexported fields
}
func NewRespProxyWriter ¶
func NewRespProxyWriter(w http.ResponseWriter) *ResponseProxyWriter
func (*ResponseProxyWriter) Header ¶
func (w *ResponseProxyWriter) Header() http.Header
func (*ResponseProxyWriter) WriteHeader ¶
func (w *ResponseProxyWriter) WriteHeader(i int)