Documentation ¶
Index ¶
- type Context
- type ContextRequest
- func (cr ContextRequest) BodyBytes() (body []byte)
- func (cr ContextRequest) BodyStream() (stream io.ReadCloser)
- func (cr ContextRequest) BodyString() (body string)
- func (cr ContextRequest) BodyStruct(result interface{}) (err error)
- func (cr ContextRequest) GetParam(name string) (value string)
- func (cr ContextRequest) Header() (h *http.Header)
- func (cr ContextRequest) Method() (m string)
- func (cr ContextRequest) Params() (p httprouter.Params)
- func (cr ContextRequest) URL() (url *url.URL)
- type ContextResponse
- func (cr ContextResponse) Bytes(statusCode int, body []byte) (err error)
- func (cr ContextResponse) File(statusCode int, filename string) (err error)
- func (cr ContextResponse) GetStatusCode() (statusCode int)
- func (cr ContextResponse) Header() (h http.Header)
- func (cr ContextResponse) Status(statusCode int) (err error)
- func (cr ContextResponse) Stream(statusCode int, stream io.Reader) (err error)
- func (cr ContextResponse) StreamReadSeeker(statusCode int, stream io.ReadSeeker) (err error)
- func (cr ContextResponse) String(statusCode int, body string) (err error)
- func (cr ContextResponse) Struct(statusCode int, s interface{}) (err error)
- type Handler
- type HandlerGroup
- func (ohg *HandlerGroup) AddHandlerGroup(basePath string, registerFunc func(hg *HandlerGroup))
- func (hg *HandlerGroup) AddMiddleware(m ...Middleware)
- func (hg *HandlerGroup) DELETE(path string, h Handler)
- func (hg *HandlerGroup) GET(path string, h Handler)
- func (ohg *HandlerGroup) GetHandlerGroup(basePath string) (hg *HandlerGroup)
- func (hg *HandlerGroup) HEAD(path string, h Handler)
- func (hg *HandlerGroup) OPTIONS(path string, h Handler)
- func (hg *HandlerGroup) PATCH(path string, h Handler)
- func (hg *HandlerGroup) POST(path string, h Handler)
- func (hg *HandlerGroup) PUT(path string, h Handler)
- type Middleware
- type RequestLogger
- func (rl *RequestLogger) Error(msg string)
- func (rl *RequestLogger) Errorf(format string, values ...interface{})
- func (rl *RequestLogger) Info(msg string)
- func (rl *RequestLogger) Infof(format string, values ...interface{})
- func (rl *RequestLogger) WTF(msg string)
- func (rl *RequestLogger) WTFf(format string, values ...interface{})
- func (rl *RequestLogger) Warning(msg string)
- func (rl *RequestLogger) Warningf(format string, values ...interface{})
- type RequestLoggerBase
- type Server
- func (s *Server) AddHandlerGroup(basePath string, registerFunc func(hg *HandlerGroup))
- func (s *Server) GetHandlerGroup(basePath string) (hg *HandlerGroup)
- func (s *Server) Router() (router *httprouter.Router)
- func (s *Server) Start(address string) (err error)
- func (s *Server) StartTest() (testServer *httptest.Server)
- type SimpleCtx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { Context SimpleCtx Log *RequestLogger Response ContextResponse Request ContextRequest // contains filtered or unexported fields }
Context is the object provided to an HTTP handler. It contains sub-objects for reading the request, returning a response, and logging. It also includes an extended context.Context, which can be used for deadlines, cancellation, or storage of arbitrary values.
func (*Context) EscapeHatch ¶
func (c *Context) EscapeHatch() (rw http.ResponseWriter, req *http.Request)
EscapeHatch returns the *Request and ResponseWriter for the request. The use of this function assumes you need lower-level control, and thus disables some built-in functionality.
type ContextRequest ¶
type ContextRequest struct {
// contains filtered or unexported fields
}
ContextRequest contains the methods used to retrieve the request details
func (ContextRequest) BodyBytes ¶
func (cr ContextRequest) BodyBytes() (body []byte)
BodyBytes returns the body of the request as a byte slice.
func (ContextRequest) BodyStream ¶
func (cr ContextRequest) BodyStream() (stream io.ReadCloser)
BodyStream returns the body of the request as a io.ReadCloser.
func (ContextRequest) BodyString ¶
func (cr ContextRequest) BodyString() (body string)
BodyString returns the body of the request as a string.
func (ContextRequest) BodyStruct ¶
func (cr ContextRequest) BodyStruct(result interface{}) (err error)
BodyStruct reads and deserialises the body of the request into the provided struct. The result parameter must be a pointer to a struct.
func (ContextRequest) GetParam ¶
func (cr ContextRequest) GetParam(name string) (value string)
GetParam is shorthand for Params().ByName.
func (ContextRequest) Header ¶
func (cr ContextRequest) Header() (h *http.Header)
func (ContextRequest) Method ¶
func (cr ContextRequest) Method() (m string)
Method returns the HTTP method of the request.
func (ContextRequest) Params ¶
func (cr ContextRequest) Params() (p httprouter.Params)
Params returns the httprouter.Params object for the request.
func (ContextRequest) URL ¶
func (cr ContextRequest) URL() (url *url.URL)
URL returns the HTTP URL of the request.
type ContextResponse ¶
type ContextResponse struct {
// contains filtered or unexported fields
}
ContextResponse contains the methods used to return an HTTP response
func (ContextResponse) Bytes ¶
func (cr ContextResponse) Bytes(statusCode int, body []byte) (err error)
Bytes returns the provided status code and body. If the Content-Type header is not already set, it will be set to application/octet-stream.
func (ContextResponse) File ¶
func (cr ContextResponse) File(statusCode int, filename string) (err error)
File returns the provided status code, then streams the provided file as the body. Go will automatically set the Content-Type based on the first 512 bytes of the file, if the header is not already set. If you don't want Go to infer the Content-Type, you should explicitly set the header BEFORE using this function.
func (ContextResponse) GetStatusCode ¶
func (cr ContextResponse) GetStatusCode() (statusCode int)
GetStatusCode returns the status code that was sent in the response. If a response code has not been sent yet, this will return 0.
func (ContextResponse) Header ¶
func (cr ContextResponse) Header() (h http.Header)
Header returns the header object for the response.
func (ContextResponse) Status ¶
func (cr ContextResponse) Status(statusCode int) (err error)
Status returns the provided status code, with no response body.
func (ContextResponse) Stream ¶
func (cr ContextResponse) Stream(statusCode int, stream io.Reader) (err error)
Stream returns the provided status code, then streams the provided Reader as the body. Go will automatically set the Content-Type based on the first 512 bytes of the stream, if the header is not already set. If you don't want Go to infer the Content-Type, you should explicitly set the header BEFORE using this function. For relatively short streams, Go will automatically buffer the output and set the Content-Length after reading the full stream. For longer streams, Go will use chunked encoding.
func (ContextResponse) StreamReadSeeker ¶
func (cr ContextResponse) StreamReadSeeker(statusCode int, stream io.ReadSeeker) (err error)
StreamReadSeeker returns the provided status code, then streams the provided ReadSeeker as the body. Go will automatically set the Content-Type based on the first 512 bytes of the stream, if the header is not already set. If you don't want Go to infer the Content-Type, you should explicitly set the header BEFORE using this function.
func (ContextResponse) String ¶
func (cr ContextResponse) String(statusCode int, body string) (err error)
String returns the provided status code and body. If the Content-Type header is not already set, it will be set to text/plain.
func (ContextResponse) Struct ¶
func (cr ContextResponse) Struct(statusCode int, s interface{}) (err error)
Struct returns the provided status code, and a serialised struct The struct will be serialised using the server's configured StructEncoder.
type Handler ¶
func ClassicHandlerShim ¶
ClassicHandlerShim converts a classic Go http.Handler into a batteryholder Handler.
type HandlerGroup ¶
type HandlerGroup struct {
// contains filtered or unexported fields
}
HandlerGroup is the object that allows you to add middleware and routes/handlers. Middleware registered within a HandlerGroup is scoped to that group, and will only be used for handlers within that group, or within child groups.
func (*HandlerGroup) AddHandlerGroup ¶
func (ohg *HandlerGroup) AddHandlerGroup(basePath string, registerFunc func(hg *HandlerGroup))
AddHandlerGroup is a more convenient method of retrieving and using a child HandlerGroup to add middleware and functions.
func (*HandlerGroup) AddMiddleware ¶
func (hg *HandlerGroup) AddMiddleware(m ...Middleware)
AddMiddleware registers one or more middleware handlers. Middleware is called in the order that it gets registered, and will only be applied to handlers that are added after it.
func (*HandlerGroup) DELETE ¶
func (hg *HandlerGroup) DELETE(path string, h Handler)
DELETE registers a handler using the DELETE HTTP Method
func (*HandlerGroup) GET ¶
func (hg *HandlerGroup) GET(path string, h Handler)
GET registers a handler using the GET HTTP Method
func (*HandlerGroup) GetHandlerGroup ¶
func (ohg *HandlerGroup) GetHandlerGroup(basePath string) (hg *HandlerGroup)
GetHandlerGroup returns a child HandlerGroup, which can be used to add middleware and handlers in a separate scope.
func (*HandlerGroup) HEAD ¶
func (hg *HandlerGroup) HEAD(path string, h Handler)
HEAD registers a handler using the HEAD HTTP Method
func (*HandlerGroup) OPTIONS ¶
func (hg *HandlerGroup) OPTIONS(path string, h Handler)
OPTIONS registers a handler using the OPTIONS HTTP Method
func (*HandlerGroup) PATCH ¶
func (hg *HandlerGroup) PATCH(path string, h Handler)
PATCH registers a handler using the PATCH HTTP Method
func (*HandlerGroup) POST ¶
func (hg *HandlerGroup) POST(path string, h Handler)
POST registers a handler using the POST HTTP Method
func (*HandlerGroup) PUT ¶
func (hg *HandlerGroup) PUT(path string, h Handler)
PUT registers a handler using the PUT HTTP Method
type Middleware ¶
func ClassicMiddlewareShim ¶
func ClassicMiddlewareShim(classicMiddleware func(next http.Handler) http.Handler) Middleware
ClassicMiddlewareShim is a helper function that allows you to use a classic Go middleware handler as middleware. If your middleware accepts more than one parameter, you'll have to curry the other parameters, as this only allows for simple middleware with a single http.Handler parameter.
type RequestLogger ¶
type RequestLogger struct {
// contains filtered or unexported fields
}
RequestLogger is used to log events that occur within a request handler.
func (*RequestLogger) Error ¶
func (rl *RequestLogger) Error(msg string)
Error should be used to log problems that are bad enough to make the request completely fail.
func (*RequestLogger) Errorf ¶
func (rl *RequestLogger) Errorf(format string, values ...interface{})
Errorf formats your message before logging it as an Error, using the provided FormatLog function.
func (*RequestLogger) Info ¶
func (rl *RequestLogger) Info(msg string)
Info should be used to log things that are useful to know, but not in any way bad.
func (*RequestLogger) Infof ¶
func (rl *RequestLogger) Infof(format string, values ...interface{})
Infof formats your message before logging it as Info, using the provided FormatLog function.
func (*RequestLogger) WTF ¶
func (rl *RequestLogger) WTF(msg string)
WTF should be used to log problems that should absolutely never happen. This also records a stack trace as a second WTF event. This should be indicative of a programming bug, as opposed to an expected runtime error.
func (*RequestLogger) WTFf ¶
func (rl *RequestLogger) WTFf(format string, values ...interface{})
WTFf formats your message before logging it as WTF, using the provided FormatLog function.
func (*RequestLogger) Warning ¶
func (rl *RequestLogger) Warning(msg string)
Warning should be used to log problems that are not bad enough to make the request completely fail.
func (*RequestLogger) Warningf ¶
func (rl *RequestLogger) Warningf(format string, values ...interface{})
Warningf formats your message before logging it as a Warning, using the provided FormatLog function.
type RequestLoggerBase ¶
type RequestLoggerBase interface { // Info will be used to log things that are useful to know, but not in any way bad. Info(msg string) // Warning will be used to log problems that are not bad enough to make the request completely fail. Warning(msg string) // Error will be used to log problems that are bad enough to make the request completely fail. Error(msg string) // WTF will be used to log problems that should absolutely never happen; in development this should panic, in production it should simply be logged. WTF(msg string) // FormatLog will be called to format logs when using the RequestLogger.*f methods. FormatLog(format string, values ...interface{}) (mst string) // WriteLogs is called once a request completes, in order to write all the logs that were created during the lifecycle of the request WriteLogs() }
RequestLoggerBase provides the basic interface required to log at 4 simple levels.
type Server ¶
type Server struct { // EncodeStruct will be used to serialise objects for HTTP responses. // Typically, this would be a JSON or XML encoder. EncodeStruct func(c *Context, input interface{}, output io.Writer) (err error) // DecodeStruct will be used to deserialise HTTP requests into objects. // Typically, this would be a JSON or XML decoder. // "result" must be a pointer to the object that this will deserialise into. DecodeStruct func(c *Context, input io.Reader, result interface{}) (err error) // ValidateStruct will be used to validate objects. ValidateStruct func(c *Context, input interface{}) (err error) // NewRequestLogger will be called at the beginning of every request to get a logger to be used for that request. NewRequestLogger func(c *Context) (rlb RequestLoggerBase) // contains filtered or unexported fields }
func (*Server) AddHandlerGroup ¶
func (s *Server) AddHandlerGroup(basePath string, registerFunc func(hg *HandlerGroup))
AddHandlerGroup is a more convenient method of retrieving and using a HandlerGroup to add middleware and functions.
func (*Server) GetHandlerGroup ¶
func (s *Server) GetHandlerGroup(basePath string) (hg *HandlerGroup)
GetHandlerGroup returns a HandlerGroup, which can be used to add middleware and handlers.
func (*Server) Router ¶
func (s *Server) Router() (router *httprouter.Router)
Router returns the underlying julienschmidt/httprouter Router instance.