Documentation ¶
Index ¶
- func GetRequestID(r *http.Request) (string, bool)
- type Authenticator
- type Logger
- type Middleware
- func (m *Middleware) BasicAuth(handler http.Handler, auth Authenticator) http.Handler
- func (m *Middleware) HTTPError(rw http.ResponseWriter, r *http.Request, msg string, code int, err error)
- func (m *Middleware) Logger(handler http.Handler) http.Handler
- func (m *Middleware) Recovery(handler http.Handler) http.Handler
- func (m *Middleware) RequestID(handler http.Handler) http.Handler
- func (m *Middleware) Timeout(handler http.Handler, fallback time.Duration) http.Handler
- type StdLogger
- type TimeoutResponseWriter
- func (rw *TimeoutResponseWriter) Cancel() bool
- func (rw *TimeoutResponseWriter) Header() http.Header
- func (rw *TimeoutResponseWriter) ResponseWriter() http.ResponseWriter
- func (rw *TimeoutResponseWriter) Status() int
- func (rw *TimeoutResponseWriter) TimedOut() bool
- func (rw *TimeoutResponseWriter) Write(b []byte) (int, error)
- func (rw *TimeoutResponseWriter) WriteHeader(status int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Authenticator ¶ added in v1.2.0
Authenticator is a simple interface to provide HTTP basic authentication of a given username and password. This could support single or multiple users.
func SingleUserBcrypt ¶ added in v1.2.0
func SingleUserBcrypt(username string, passhash []byte) Authenticator
SingleUserBcrypt returns an Authenticator for a single given user credentials with bcrypt hashed password.
func SingleUserBcryptUnsafe ¶ added in v1.2.0
func SingleUserBcryptUnsafe(username, password string) Authenticator
SingleUserBcryptUnsafe returns the same as SingleUserBcrypt but hashing the given password string beforehand, this is unsafe as depending on the means of input to the app you should not be passing in a plain-text password.
type Logger ¶
type Logger interface { // LogRequest will log a handled HTTP request along with the handler's response code and latency. LogRequest(r *http.Request, code int, latency time.Duration) // LogHTTP will log the error response of a handled HTTP request including status, code and the causal error. LogHTTPError(r *http.Request, status string, code int, err error) // LogPanic will log a panic caught when handling an HTTP request including recovered panic info and stack frames. LogPanic(r *http.Request, i interface{}, stack *runtime.Frames) }
Logger is a log interface used by middleware in its various handlers. This allows your log implementation of choice to work with our middleware handlers.
type Middleware ¶ added in v0.2.1
type Middleware struct {
// contains filtered or unexported fields
}
func New ¶ added in v1.0.0
func New(logger Logger) *Middleware
New returns a new instance of Middleware, using log.Default() is Logger is nil.
func (*Middleware) BasicAuth ¶ added in v1.2.0
func (m *Middleware) BasicAuth(handler http.Handler, auth Authenticator) http.Handler
BasicAuth adds HTTP basic authentication to the given handler, passing each user / pass provided in the HTTP authorization header to the given Authenticator{} for authentication.
func (*Middleware) HTTPError ¶ added in v0.2.1
func (m *Middleware) HTTPError(rw http.ResponseWriter, r *http.Request, msg string, code int, err error)
HTTPError handles responding to a client with an HTTP error code and logs the provided error.
func (*Middleware) Logger ¶ added in v0.2.1
func (m *Middleware) Logger(handler http.Handler) http.Handler
Logger adds request logging to the provided HTTP handler.
func (*Middleware) Recovery ¶ added in v0.2.1
func (m *Middleware) Recovery(handler http.Handler) http.Handler
Recovery adds panic recovery (and panic logging) to the provided HTTP handler.
func (*Middleware) RequestID ¶ added in v0.3.1
func (m *Middleware) RequestID(handler http.Handler) http.Handler
RequestID provides setting unique IDs per request and adding a hook to the logger to append these to log entries using the request's context. This should be placed at the TOP of handler stack.
type StdLogger ¶ added in v1.0.0
StdLogger is a typedef to allow the standard library "log" Logger to implement the middleware.Logger interface.
func (*StdLogger) LogHTTPError ¶ added in v1.0.0
type TimeoutResponseWriter ¶ added in v0.3.0
type TimeoutResponseWriter struct {
// contains filtered or unexported fields
}
TimeoutResponseWriter wraps an http.ResponseWriter to add request timeout capabilities.
func (*TimeoutResponseWriter) Cancel ¶ added in v0.3.0
func (rw *TimeoutResponseWriter) Cancel() bool
Cancel will attempt to cancel the timeout counter.
func (*TimeoutResponseWriter) Header ¶ added in v0.3.0
func (rw *TimeoutResponseWriter) Header() http.Header
Header implements http.ResponseWriter's .Header().
func (*TimeoutResponseWriter) ResponseWriter ¶ added in v0.3.0
func (rw *TimeoutResponseWriter) ResponseWriter() http.ResponseWriter
ResponseWriter returns access to the underlying http.ResponseWriter.
func (*TimeoutResponseWriter) Status ¶ added in v0.3.0
func (rw *TimeoutResponseWriter) Status() int
Status returns the set response status code.
func (*TimeoutResponseWriter) TimedOut ¶ added in v0.3.0
func (rw *TimeoutResponseWriter) TimedOut() bool
TimedOut returns whether this TimeoutResponseWriter has timed out.
func (*TimeoutResponseWriter) Write ¶ added in v0.3.0
func (rw *TimeoutResponseWriter) Write(b []byte) (int, error)
Write implements http.ResponseWriter's .Write().
func (*TimeoutResponseWriter) WriteHeader ¶ added in v0.3.0
func (rw *TimeoutResponseWriter) WriteHeader(status int)
WriteHeader implements http.ResponseWriter's .WriteHeader().