Documentation ¶
Index ¶
- Constants
- Variables
- func CommonHeaders(serverName string) httpserver.HandlerFunc
- func Error(r *httpserver.Response, status int, errmsg string)
- func Forbidden(r *httpserver.Response)
- func LogAccess(c *httpserver.Context)
- func LogError(c *httpserver.Context)
- func LogHeaders(headerKeys ...string) func(c *httpserver.Context)
- func LogRequest(c *httpserver.Context)
- func MethodNotSupported(r *httpserver.Response)
- func MustEncryptCookie(c *httpserver.Context, maxAge int, name string, value interface{})
- func ProtectCookies(key []byte) httpserver.HandlerFunc
- func Recover(c *httpserver.Context)
- func RouteNotFound(r *httpserver.Response)
- func StaticError(r *httpserver.Response, status int, header string, body string)
- func Unauthorized(r *httpserver.Response)
- type RequestErrors
- type RequestLog
- type StackLogger
Constants ¶
const ( CookieRealToken = "SIGNED-TOKEN" CookieXSRFToken = "XSRF-TOKEN" HeaderXSRFToken = "X-XSRF-Token" LocalCookies = "Cookies" )
const ( HeaderCacheControl = "Cache-Control" HeaderXssProtection = "X-Xss-Protection" HeaderFrameOptions = "X-Frame-Options" HeaderContentTypeOptions = "X-Content-Type-Options" HeaderServer = "Server" CacheControlNeverCache = "max-age=0, private, must-revalidate" )
const ( AnsiBlack = "\x1b[30m" AnsiRed = "\x1b[31m" AnsiGreen = "\x1b[32m" AnsiYellow = "\x1b[33m" AnsiBlue = "\x1b[34m" AnsiMagenta = "\x1b[35m" AnsiCyan = "\x1b[36m" AnsiWhite = "\x1b[37m" AnsiReset = "\x1b[0m" AnsiBold = "\x1b[1m" LogTimeFormat = "2006/01/02 15:04:05" )
Variables ¶
var HeaderRequestErrors = "Request-Errors"
If HeaderRequestErrors is set, errors will additionally be sent in that header
var Logger = NewStackLogger(os.Stdout)
Functions ¶
func CommonHeaders ¶
func CommonHeaders(serverName string) httpserver.HandlerFunc
CommonHeaders sets our Server-side headers like Cache, Security, etc
func Error ¶
func Error(r *httpserver.Response, status int, errmsg string)
Error sets the header to the value of HeaderRequestErrors (eg. "Request-Errors") and renders the errors in a json body
func Forbidden ¶
func Forbidden(r *httpserver.Response)
func LogAccess ¶
func LogAccess(c *httpserver.Context)
LogAccess logs a request immediately when a request is received (before it is processed). It includes the HTTP method, request path, client IP (w/ X-Forward-For), and time received.
Redundant when used with LogRequest. Use LogAccess middleware when either:
- Trying to log access to only a subset of requests
- Want to log a request without allocating a buffer from the Logger pool
func LogError ¶
func LogError(c *httpserver.Context)
LogError logs a request only if its HTTP status code is greater than 400. It includes the HTTP method, request path, client IP (w/ X-Forward-For), the error status code and text, the reply time, and the processing latency.
Use the LogError middleware when you don't want the verbosity of LogRequest, but still want to log any errors that may occur.
func LogHeaders ¶
func LogHeaders(headerKeys ...string) func(c *httpserver.Context)
LogHeaders returns a middleware which logs any header values for headers in headerKeys. Header values longer than 60 characters are truncated with an ellipsis in the log output.
ex. LogHeaders("Accept", "User-Agent")
func LogRequest ¶
func LogRequest(c *httpserver.Context)
LogRequest logs a multiline message with information about each received request. One log line is emitted immediately when the request is received (in case of server crash), the remaining log lines are aggregated in a buffer allocated from a pool and only emitted after the request has been processed.
Another middleware, like LogHeaders can access the request-specific logger from the *httpserver.Context with `c.GetLocal("Log")` or can use the `Logf("fmt", ..args)`, `LogValue(c, "Key", value)`, and `LogResponse(c, "Status", value)` helpers.
func MethodNotSupported ¶
func MethodNotSupported(r *httpserver.Response)
func MustEncryptCookie ¶
func MustEncryptCookie(c *httpserver.Context, maxAge int, name string, value interface{})
MustEncryptCookie sets a cookie value for a secure session
func ProtectCookies ¶
func ProtectCookies(key []byte) httpserver.HandlerFunc
ProtectCookies provides cookie storage with XSRF Protection
func Recover ¶
func Recover(c *httpserver.Context)
Recover is a middlerware that recovers from any panics and writes a 500 if there was one. Logs to the specified writter buffer. If nil is provided, it will still recover, but won't log. Example: os.Stdout, a file opened in write mode, a socket...
func RouteNotFound ¶
func RouteNotFound(r *httpserver.Response)
func StaticError ¶
func StaticError(r *httpserver.Response, status int, header string, body string)
func Unauthorized ¶
func Unauthorized(r *httpserver.Response)
Types ¶
type RequestErrors ¶
type RequestErrors struct {
Errors []string `json:"errors"`
}
type StackLogger ¶
StackLogger stores log output in memory for a given request context so that log output for the given request is sequential in the final log. This makes it easier to gobble up all the information for a single request with Logstash.
func NewStackLogger ¶
func NewStackLogger(out io.Writer) *StackLogger
func (*StackLogger) LogResponse ¶
func (l *StackLogger) LogResponse(c *httpserver.Context, status string, value interface{})
func (*StackLogger) LogValue ¶
func (l *StackLogger) LogValue(c *httpserver.Context, name string, value interface{})
func (*StackLogger) Logf ¶
func (l *StackLogger) Logf(c *httpserver.Context, format string, args ...interface{})