Documentation ¶
Overview ¶
Package goatkeeper is an HTTP middleware for Golang that validates HTTP requests and their responses according to types defined in OpenAPI Specification.
Built using kin-openapi implementation! https://github.com/getkin/kin-openapi
Usage:
spec, err := ioutil.ReadFile("./openapi-spec.yaml") if err != nil { panic("unable to read openapi spec file") } middleware, err := goatkeeper.NewMiddlware( &goatkeeper.MiddlewareOptions{ OpenAPISpec: spec, ValidateResponse: true, }) if err != nil { panic("unable to initialize goatkeeper middleware") }
With Gorilla mux
r := mux.NewRouter() r.Use(middleware) // r.Handle ...
With vanilla Golang
s := &http.Server{ Addr: ":8080", Handler: middlware(myAPIHandler), } log.Fatal(s.ListenAndServe())
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultMiddlewareOptions = MiddlewareOptions{ Logger: logrtesting.NullLogger{}, ValidateResponse: false, InvalidRequestResponse: &MiddlewareResponse{ Headers: http.Header{}, Status: http.StatusBadRequest, Body: []byte{}, }, InvalidResponseResponse: &MiddlewareResponse{ Status: http.StatusInternalServerError, Headers: http.Header{}, Body: []byte{}, }, }
DefaultMiddlewareOptions defines default options used by the middleware. These options are only used if a option isn't specified when initializing the middleware.
Functions ¶
func NewMiddleware ¶
NewMiddleware creates a new HTTP middleware that will use the given OpenAPI Spec to validate all requests and their responses.
Types ¶
type MiddlewareOptions ¶
type MiddlewareOptions struct { Logger logr.Logger OpenAPISpec []byte ValidateResponse bool // should goatkeeper also validate http responses InvalidRequestResponse *MiddlewareResponse // response to write if request is invalid InvalidResponseResponse *MiddlewareResponse // response to write if response is invalid }
MiddlewareOptions defines options for configuring the GoatKeeper middleware.
Click to show internal directories.
Click to hide internal directories.