Documentation
¶
Index ¶
- Variables
- func AddModifier(tag string, fn mold.Func)
- func AttachLogger(log zerolog.Logger) func(http.Handler) http.Handler
- func CORS(appEnv string, origins ...string) func(http.Handler) http.Handler
- func FormData(r *http.Request, v interface{}) error
- func IDParam(r *http.Request, name string) (uint, error)
- func Log(next http.Handler) http.Handler
- func MultipartFormData(r *http.Request, size int64, v interface{}) error
- func QueryParams(r *http.Request, v interface{}) error
- func ReadBody(r *http.Request) ([]byte, error)
- func ReadJSON(r *http.Request, v interface{}) error
- func StringParam(r *http.Request, name string) string
- func Timeout(timeout time.Duration) func(next http.Handler) http.Handler
Constants ¶
This section is empty.
Variables ¶
var (
ErrNotJSON = errors.New("body is not JSON")
)
Functions ¶
func AddModifier ¶
func AddModifier(tag string, fn mold.Func)
func AttachLogger ¶
AttachLogger attaches a new zerolog.Logger to each new HTTP request. Stolen from https://github.com/rs/zerolog/blob/master/hlog/hlog.go
func CORS ¶
CORS sets CORS for the handler based on the app environment, making the rules lax in dev environment.
func IDParam ¶
IDParam extracts a uint URL parameter from the given request. panics if there's no such path on the route, otherwise it returns an error if the param is not an int.
func Log ¶
Log updates a future log entry with the request parameters such as request ID and headers.
func MultipartFormData ¶ added in v0.18.0
MultipartFormData converts the non-files in the form data of the request into a struct using the "json" tag to map the keys. It supports transformations using modl and validation provided by ozzo.
func QueryParams ¶ added in v0.17.0
QueryParams converts the query values of the request into a struct using the "json" tag to map the keys. It supports transformations using modl and validation provided by ozzo.
func ReadBody ¶
ReadBody extracts the bytes in a request body without destroying the contents of the body. Returns an error if reading body fails.
func ReadJSON ¶
ReadJSON decodes the JSON body of the request and destroys it to prevent possible issues with writing a response. Returns ErrNotJSON if the content-type of the request is not JSON, else it returns validation.Errors if the resultant value fails validation defined using ozzo. Otherwise the it returns an error when json decoding fails
func StringParam ¶
StringParam basically just ensures the param name is correct. You might not need this method unless you're too lazy to do real tests.
func Timeout ¶
Timeout is a middleware that cancels ctx after a given timeout and return a 504 Gateway Timeout error to the client. P.S this was copied directly from go-chi, only removed writing to the response. Also note that this middleware can only be used once in the entire stack. Using it again has not effect on requests(i.e. the first use is the preferred).
It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.
ie. a route/handler may look like:
r.Get("/long", func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() processTime := time.Duration(rand.Intn(4)+1) * time.Second select { case <-ctx.Done(): return case <-time.After(processTime): // The above channel simulates some hard work. } w.Write([]byte("done")) })
Types ¶
This section is empty.