Documentation ¶
Index ¶
- Constants
- Variables
- func DotFileHidingFileSystem(fs http.FileSystem) http.FileSystem
- func FileSystem(fs http.FileSystem) http.FileSystem
- func Hydrate(function interface{}) http.HandlerFunc
- func InterruptContext() context.Context
- func JSON(w http.ResponseWriter, status int, data interface{}) (int, error)
- func ListenWithContext(ctx context.Context, server *http.Server, healthy *int32) error
- func MaxBodySize(next http.Handler, size int64) http.Handler
- func MustQueryParams(h http.Handler, params ...string) http.Handler
- func RequestCounter(duration time.Duration, callback func(uint64, chan struct{})) func(http.Handler) http.Handler
- func RequestThrottler(concurrentRequests int, timeout time.Duration) func(http.Handler) http.Handler
- func SpaFileSystem(fs http.FileSystem) http.FileSystem
- type JSONResponse
- type ParseError
Constants ¶
const ( // TagQuery is the field tag for looking up Query Parameters TagQuery = "query" // TagParam is the field tag for looking up URL Parameters TagParam = "param" )
Variables ¶
var ValidationErrorMessage = "Invalid Request"
ValidationErrorMessage sent to client on validation fail
Functions ¶
func DotFileHidingFileSystem ¶ added in v0.5.0
func DotFileHidingFileSystem(fs http.FileSystem) http.FileSystem
DotFileHidingFileSystem is an http.FileSystem that hides "dot files" from being served.
http.Handle("/", http.FileServer(mid.DotFileHidingFileSystem(http.Dir("/dist"))))
func FileSystem ¶ added in v0.5.0
func FileSystem(fs http.FileSystem) http.FileSystem
FileSystem wrapper to send index.html to all non-existant paths and hide dot files
func Hydrate ¶ added in v0.4.0
func Hydrate(function interface{}) http.HandlerFunc
Hydrate and validate a http.Handler with input from HTTP GET/POST requests
func InterruptContext ¶ added in v0.5.0
InterruptContext listing for os.Signal (i.e. CTRL+C) to cancel a context and end a server/daemon gracefully
func JSON ¶
func JSON(w http.ResponseWriter, status int, data interface{}) (int, error)
JSON safely written to ResponseWriter by using a buffer to prevent partial sends
func ListenWithContext ¶ added in v0.6.0
ListenWithContext starts the server while watching context to trigger graceful shutdown; Use with mid.InterruptContext() to control application lifecycle
func MaxBodySize ¶ added in v0.2.0
MaxBodySize limits the size of the request body to avoid a DOS with a large JSON structure Go does this internally for multipart bodies: https://golang.org/src/net/http/request.go#L1136
func MustQueryParams ¶ added in v0.5.0
MustQueryParams circit breaker middleware only forwards requests which have the specified query params set
func RequestCounter ¶ added in v0.2.0
func RequestCounter(duration time.Duration, callback func(uint64, chan struct{})) func(http.Handler) http.Handler
RequestCounter is useful for counting requests for logging
func RequestThrottler ¶ added in v0.4.0
func RequestThrottler(concurrentRequests int, timeout time.Duration) func(http.Handler) http.Handler
RequestThrottler creates a re-usable limiter for multiple http.Handlers If the server is too busy to handle the request within the timeout, then a "503 Service Unavailable" status will be sent and the connection closed.
func SpaFileSystem ¶ added in v0.5.0
func SpaFileSystem(fs http.FileSystem) http.FileSystem
SpaFileSystem wraps a http.FileSystem and returns index.html for all missing paths while blocking directory browsing. Look into expanding or replacing this with: https://github.com/aaronellington/gospa https://golang.org/src/net/http/fs.go?s=20651:20691#L705
http.Handle("/", http.FileServer(mid.SpaFileSystem(http.Dir("/dist"))))
Types ¶
type JSONResponse ¶ added in v0.4.0
type JSONResponse struct { Data interface{} `json:"data,omitempty"` Error string `json:"error,omitempty"` Fields map[string]string `json:"fields,omitempty"` }
JSONResponse for validation errors or service responses
type ParseError ¶ added in v0.4.0
ParseError occurs whenever the field cannot be parsed, i.e. type mismatch
func (ParseError) Error ¶ added in v0.4.0
func (p ParseError) Error() string