Documentation ¶
Index ¶
- Variables
- func Logger(ctx context.Context, w http.ResponseWriter, req *http.Request, ...) bool
- func New(h http.Handler) seed.MiddlewareFunc
- func PrintPrettyStack(rvr interface{})
- func RealIP(ctx context.Context, w http.ResponseWriter, req *http.Request, ...) bool
- func Recoverer(ctx context.Context, w http.ResponseWriter, req *http.Request, ...) bool
- func RequestLogger(f LogFormatter) seed.MiddlewareFunc
- func Timeout(timeout time.Duration) seed.MiddlewareFunc
- func WithLogEntry(r *http.Request, entry LogEntry) *http.Request
- type DefaultLogFormatter
- type LogEntry
- type LogFormatter
- type LoggerInterface
- type WrapResponseWriter
Constants ¶
This section is empty.
Variables ¶
var ( // LogEntryCtxKey is the context.Context key to store the request log entry. LogEntryCtxKey = &seed.ContextKey{Name: "LogEntry"} MLogFormatter = &DefaultLogFormatter{} )
var IsTTY bool
var RecovererErrorWriter io.Writer = os.Stderr
RecovererErrorWriter for ability to test the PrintPrettyStack function
Functions ¶
func Logger ¶
func Logger(ctx context.Context, w http.ResponseWriter, req *http.Request, next seed.MiddleWareQueue) bool
Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return. When standard output is a TTY, Logger will print in color, otherwise it will print in black and white. Logger prints a request ID if one is provided.
IMPORTANT NOTE: Logger should go before any other middleware that may change the response, such as middleware.Recoverer. Example:
r := seed.NewRouter() r.Use(middleware.Logger) // <--<< Logger should come before Recoverer r.Use(middleware.Recoverer)
func New ¶
func New(h http.Handler) seed.MiddlewareFunc
New will create a new middleware from a http.Handler.
func PrintPrettyStack ¶
func PrintPrettyStack(rvr interface{})
func RealIP ¶
func RealIP(ctx context.Context, w http.ResponseWriter, req *http.Request, next seed.MiddleWareQueue) bool
RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the True-Client-IP, X-Real-IP or the X-Forwarded-For headers (in that order).
This middleware should be inserted fairly early in the middleware stack to ensure that subsequent layers (e.g., request loggers) which examine the RemoteAddr will see the intended value.
You should only use this middleware if you can trust the headers passed to you (in particular, the two headers this middleware uses), for example because you have placed a reverse proxy like HAProxy or nginx in front of clover. If your reverse proxies are configured to pass along arbitrary header values from the client, or if you use this middleware without a reverse proxy, malicious clients will be able to make you very sad (or, depending on how you're using RemoteAddr, vulnerable to an attack of some sort).
func Recoverer ¶
func Recoverer(ctx context.Context, w http.ResponseWriter, req *http.Request, next seed.MiddleWareQueue) bool
Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.
func RequestLogger ¶
func RequestLogger(f LogFormatter) seed.MiddlewareFunc
RequestLogger returns a logger handler using a custom LogFormatter.
func Timeout ¶
func Timeout(timeout time.Duration) seed.MiddlewareFunc
Timeout is a middleware that cancels ctx after a given timeout and return a 504 Gateway Timeout error to the client.
It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.
Types ¶
type DefaultLogFormatter ¶
type DefaultLogFormatter struct { Logger LoggerInterface NoColor bool }
DefaultLogFormatter is a simple logger that implements a LogFormatter.
func (*DefaultLogFormatter) NewLogEntry ¶
func (l *DefaultLogFormatter) NewLogEntry(r *http.Request) LogEntry
NewLogEntry creates a new LogEntry for the request.
type LogEntry ¶
type LogEntry interface { Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) Panic(v interface{}, stack []byte) }
LogEntry records the final log when a request completes. See defaultLogEntry for an example implementation.
func GetLogEntry ¶
GetLogEntry returns the in-context LogEntry for a request.
type LogFormatter ¶
LogFormatter initiates the beginning of a new LogEntry per request. See DefaultLogFormatter for an example implementation.
type LoggerInterface ¶
type LoggerInterface interface {
Print(v ...interface{})
}
LoggerInterface accepts printing to stdlib logger or compatible logger.
type WrapResponseWriter ¶
type WrapResponseWriter interface { http.ResponseWriter // Status returns the HTTP status of the request, or 0 if one has not // yet been sent. Status() int // BytesWritten returns the total number of bytes sent to the client. BytesWritten() int // Tee causes the response body to be written to the given io.Writer in // addition to proxying the writes through. Only one io.Writer can be // tee'd to at once: setting a second one will overwrite the first. // Writes will be sent to the proxy before being written to this // io.Writer. It is illegal for the tee'd writer to be modified // concurrently with writes. Tee(io.Writer) // Unwrap returns the original proxied target. Unwrap() http.ResponseWriter }
WrapResponseWriter is a proxy around an http.ResponseWriter that allows you to hook into various parts of the response process.
func NewWrapResponseWriter ¶
func NewWrapResponseWriter(w http.ResponseWriter, protoMajor int) WrapResponseWriter
NewWrapResponseWriter wraps an http.ResponseWriter, returning a proxy that allows you to hook into various parts of the response process.