Documentation
¶
Index ¶
- func CompactJSON(raw []byte) []byte
- func DefaultMiddleware(env BasicEnv, log zerolog.Logger, router *chi.Mux)
- func DevCORS() *cors.Cors
- func IDParam(r *http.Request, name string) uint
- func ListenForInterrupt(log zerolog.Logger, cancel context.CancelFunc)
- func NewLogger(service string) zerolog.Logger
- func ReadBody(r *http.Request) []byte
- func ReadJSONBody(r *http.Request, schema ozzo.Validatable)
- func Recoverer(next http.Handler) http.Handler
- func RunServer(port int, log zerolog.Logger, handler http.Handler, close func() error)
- func SecureCORS(origins ...string) *cors.Cors
- 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
- func ZeroMiddleware(log zerolog.Logger, exclude ...string) func(next http.Handler) http.Handler
- type APIError
- type BasicEnv
- type ZeroLogEntry
- type ZeroLogger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 - CORS - RedirectSlashes - Compress(with compression level of 5) - Timeout(with 1 minute)
func ListenForInterrupt ¶ added in v0.2.0
func ListenForInterrupt(log zerolog.Logger, cancel context.CancelFunc)
func ReadBody ¶
ReadBody extracts the bytes in a request body without destroying the contents of the body
func ReadJSONBody ¶
func ReadJSONBody(r *http.Request, schema ozzo.Validatable)
ReadBodyJSON is `ReadBody` except that it decodes
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 SecureCORS ¶
SecureCORS is a lot like DevCORS except with a limited set of origins
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
func StringParam ¶ added in v0.2.0
StringParam basically just ensures the param name is correct. You might not need this method unless you're too lazy to do real tests.
Types ¶
type APIError ¶
type APIError struct {
Code int `json:"-"`
Message string `json:"message"`
Meta interface{} `json:"meta"`
Err error `json:"-"`
}
APIError is a struct describing an error
type ZeroLogEntry ¶
ZeroLogEntry is an info event for request detauis
func (*ZeroLogEntry) Panic ¶
func (e *ZeroLogEntry) Panic(v interface{}, _ []byte)
Panic logs final requests that failed with a panic
type ZeroLogger ¶
ZeroLogger is a wrapper around zero log for chi
func (ZeroLogger) NewLogEntry ¶
func (l ZeroLogger) NewLogEntry(r *http.Request) middleware.LogEntry
NewLogEntry creates a special log for each request and storing it's request info for write logs or panic logs