requests

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotJSON = errors.New("body is not JSON")
)

Functions

func AddModifier

func AddModifier(tag string, fn mold.Func)

func AttachLogger

func AttachLogger(log zerolog.Logger) func(http.Handler) http.Handler

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

func CORS(appEnv string, origins ...string) func(http.Handler) http.Handler

CORS sets CORS for the handler based on the app environment, making the rules lax in dev environment.

func IDParam

func IDParam(r *http.Request, name string) (uint, error)

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

func Log(next http.Handler) http.Handler

Log updates a future log entry with the request parameters such as request ID and headers.

func ReadBody

func ReadBody(r *http.Request) ([]byte, error)

ReadBody extracts the bytes in a request body without destroying the contents of the body. Returns an error if reading body fails.

func ReadJSON

func ReadJSON(r *http.Request, v interface{}) error

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

func StringParam(r *http.Request, name string) string

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

func Timeout(timeout time.Duration) func(next http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL