Documentation
¶
Index ¶
- func CompactJSON(raw []byte) []byte
- func DefaultMiddleware(env BasicEnv, log zerolog.Logger, router *chi.Mux)
- func DevCORS() *cors.Cors
- func GetRequest(t *testing.T, path string) *http.Request
- func GetResponseBody(t *testing.T, rr *httptest.ResponseRecorder) map[string]interface{}
- func IDParam(r *http.Request, name string) uint
- func IsStatus(t *testing.T, expected, actual int)
- func MockRequest(req *http.Request, handler http.Handler) *httptest.ResponseRecorder
- func NewLogger(service string) zerolog.Logger
- func PostRequest(t *testing.T, path string, body interface{}) *http.Request
- func PutRequest(t *testing.T, path string, body interface{}) *http.Request
- 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 SearchRequest(t *testing.T, path string, query map[string]string) *http.Request
- 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 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 GetRequest ¶
GetRequest creates a mock GET request
func GetResponseBody ¶
func GetResponseBody(t *testing.T, rr *httptest.ResponseRecorder) map[string]interface{}
GetResponseBody creates a map of the JSON response body of off a mock request
func MockRequest ¶
MockRequest records the http response to a generated request
func PostRequest ¶
PostRequest creates a mock POST request with a JSON body
func PutRequest ¶
PutRequest creates a mock PUT request with a JSON body
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 SearchRequest ¶
SearchRequest creates a mock GET request with query parameters
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
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