Documentation
¶
Index ¶
- func AttachLogger(log zerolog.Logger) func(http.Handler) http.Handler
- func CORS(router *chi.Mux, appEnv string, origins ...string)
- func CompactJSON(raw []byte) []byte
- func DefaultMiddleware(router *chi.Mux, appEnv string, log zerolog.Logger)
- func DefaultRoutes(router *chi.Mux)
- func IDParam(r *http.Request, name string) uint
- func ListenForInterrupt(log zerolog.Logger, cancel context.CancelFunc)
- func LoadEnv(env interface{}) error
- func NewLogger(service string) zerolog.Logger
- func ReadBody(r *http.Request) []byte
- func ReadJSON(r *http.Request, schema ozzo.Validatable)
- func ReadJSONRaw(r *http.Request, schema interface{})
- func Recoverer(env string) func(http.Handler) http.Handler
- func RequestLogger() func(http.Handler) http.Handler
- func ResponseLogger() func(http.Handler) http.Handler
- func RunServer(port int, log zerolog.Logger, handler http.Handler)
- func SendError(r *http.Request, w http.ResponseWriter, err APIError)
- func SendJSON(r *http.Request, w http.ResponseWriter, code int, v interface{})
- func SendSuccess(r *http.Request, w http.ResponseWriter, v interface{})
- func StringParam(r *http.Request, name string) string
- type APIError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AttachLogger ¶
stolen from https://github.com/rs/zerolog/blob/master/hlog/hlog.go
func CORS ¶ added in v0.4.1
CORS sets CORS for the handler. It enables localhost by default when appEnv is not "dev".
func CompactJSON ¶
CompactJSON removes insignificant space from JSON
func DefaultMiddleware ¶
DefaultMiddleware sets up some middlware for a router as well as liveliness(/) and not found handlers: - RequestID - RealIP - Logger(using zerolog) - Recoverer - RedirectSlashes - Compress(with compression level of 5) - Timeout(with 1 minute)
func DefaultRoutes ¶ added in v0.6.0
DefaultRoutes adds liveness(/) and Not found handlers for the passed router.
func ListenForInterrupt ¶ added in v0.2.0
func ListenForInterrupt(log zerolog.Logger, cancel context.CancelFunc)
func LoadEnv ¶ added in v0.5.0
func LoadEnv(env interface{}) error
LoadEnv loads environment variables into Env
func ReadBody ¶
ReadBody extracts the bytes in a request body without destroying the contents of the body
func ReadJSON ¶ added in v0.8.0
func ReadJSON(r *http.Request, schema ozzo.Validatable)
ReadJSON is `ReadBody` except that it decodes JSON and validates the result.
func ReadJSONRaw ¶
ReadJSONRaw is ReadJSON for arrays/maps. Was too lazy to generalise or name well.
func Recoverer ¶
Recoverer creates a middleware that handles panics from chi controllers. It uses the passed interpreters to try to convert errors to APIErrors where possible otherwise it returns a 500 error. When the panic is an APIError or is interpreted as one, it sends a response with the right error code. TODO: add support for wrapped errors in APIError.
func RequestLogger ¶
RequestLogger updates a future log entry with the request parameters such as request ID and headers.
func SendError ¶
func SendError(r *http.Request, w http.ResponseWriter, err APIError)
SendError sends a JSON error message
func SendJSON ¶
func SendJSON(r *http.Request, w http.ResponseWriter, code int, v interface{})
SendJSON response writes a JSON encoded version of `v` to the writer, making sure what deserves to be logged gets logged
func SendSuccess ¶
func SendSuccess(r *http.Request, w http.ResponseWriter, v interface{})
SendSuccess sends a JSON success message with status code 200