Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultOptionsHandler(writer http.ResponseWriter, _ *http.Request)
- type BadRequestErr
- type Behaviour
- type BoundHandlerFunc
- type ContentType
- type ContentTypes
- type Context
- type Group
- type HTTPDurations
- type Handler
- type HandlerFunc
- type In
- type JSONContentType
- type LogLevel
- type MonitoredHTTP
- type MultiType
- type Nil
- type OctetStreamContentType
- func (t OctetStreamContentType) Decode(data []byte, value any) error
- func (t OctetStreamContentType) Encode(value any) ([]byte, error)
- func (t OctetStreamContentType) Mime() string
- func (t OctetStreamContentType) ValidateTypeSchema(logger utils.Logger, level utils.LogLevel, goType reflect.Type, ...) error
- type OpenAPIRouter
- type OpenAPISpec
- type OperationValidationOptions
- type Options
- type PlainTextContentType
- type RawResponse
- type Request
- type Response
- func Error[R any](err error) (Response[R], error)
- func Send[R any](response R, headers ...http.Header) Response[R]
- func SendBytes[R any](response R, headers ...http.Header) Response[R]
- func SendJSON[R any](response R, headers ...http.Header) Response[R]
- func SendOK[R any](response R, headers ...http.Header) Response[R]
- func SendOKBytes[R any](response R, headers ...http.Header) Response[R]
- func SendOKJSON[R any](response R, headers ...http.Header) Response[R]
- func SendOKText[R any](response R, headers ...http.Header) Response[R]
- func SendText[R any](response R, headers ...http.Header) Response[R]
- type SchemaValidationOptions
- type SpecOperation
Constants ¶
const ( LogLevelError = utils.Error LogLevelWarn = utils.Warn LogLevelInfo = utils.Info LogLevelOff = utils.Off )
const ( // PropagateError propagate validation errors to OpenAPIRouter.AsHandler return values and print them to the log. // This is also the initial Behaviour value if not set to anything else. PropagateError = Behaviour(utils.Error) // PrintWarning print validation errors as warning to the log without propagating the error to OpenAPIRouter.AsHandler return values PrintWarning = Behaviour(utils.Warn) // Ignore do nothing if the validation fails. // A validation failure won't get printed to the logs and an error won't get propagate to OpenAPIRouter.AsHandler return values Ignore = Behaviour(utils.Off) )
Variables ¶
var ( UnsupportedRequestContentTypeErr = errors.New("unsupported request content type for operation") UnsupportedResponseContentTypeErr = errors.New("unsupported response content type for operation") UnsupportedResponseStatusErr = errors.New("unsupported response status for operation") )
Runtime errors causes
var ( // ErrInvalidUUID is returned when the uuid format validation fails. ErrInvalidUUID = errors.New("invalid uuid") )
var ErrSpecValidation = errors.New("spec validation failed")
Functions ¶
func DefaultOptionsHandler ¶ added in v1.6.0
func DefaultOptionsHandler(writer http.ResponseWriter, _ *http.Request)
DefaultOptionsHandler This handler is the default OPTIONS handler provided when using DefaultOptions. It will respond to each OPTIONS request with an "Allow" header that will include all the methods that are defined for the path and respond with 204 status.
Types ¶
type BadRequestErr ¶
BadRequestErr is the error returned when there is an error binding the request. You can handle this request using an ErrorHandler middleware to return a custom HTTP response.
func (BadRequestErr) Error ¶
func (e BadRequestErr) Error() string
func (BadRequestErr) Is ¶
func (e BadRequestErr) Is(err error) bool
func (BadRequestErr) Unwrap ¶
func (e BadRequestErr) Unwrap() error
type Behaviour ¶
Behaviour defines a possible behaviour for a validation error. Possible values are PropagateError, PrintWarning and Ignore.
- PropagateError - Cause call to OpenAPIRouter.AsHandler to return an error if the validation fails and failure reason is logged with error level.
- PrintWarning - if the validation fails the failure reason will get logged with warning level. The error won't propagate to OpenAPIRouter.AsHandler return values
- Ignore - do nothing if the validation fails. don't print anything to the logs and don't propagate the error to OpenAPIRouter.AsHandler return values
By default, Behaviour are initialized to PropagateError as it is essentially Behaviour zero value.
func (Behaviour) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Behaviour) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type BoundHandlerFunc ¶
type BoundHandlerFunc func(*Context) (RawResponse, error)
BoundHandlerFunc is an untyped wrapper to HandlerFunc that bound internally calls to request binding, response binding and propagate next handler in the Context.
type ContentType ¶
type ContentTypes ¶
type ContentTypes map[string]ContentType
func DefaultContentTypes ¶
func DefaultContentTypes() ContentTypes
type Context ¶
type Context struct { Operation SpecOperation Writer http.ResponseWriter Request *http.Request Params *httprouter.Params RawResponse *RawResponse NextFunc BoundHandlerFunc Durations HTTPDurations }
Context carries the original http.Request and http.ResponseWriter and additional important parameters through the handlers chain.
func (*Context) Next ¶
func (c *Context) Next() (RawResponse, error)
type Group ¶
type Group interface { // Use middlewares on the root handler Use(...Handler) Group // WithGroup defines a virtual group that allow defining middlewares for group of routes more easily WithGroup(Group) Group // WithOperation attaches a MiddlewareHandler for an operation ID string on the OpenAPISpec WithOperation(string, Handler, ...Handler) Group // contains filtered or unexported methods }
type HTTPDurations ¶ added in v1.7.1
type Handler ¶
type Handler interface {
// contains filtered or unexported methods
}
Handler described the HandlerFunc in a non parametrized way. The router "magic" of being a collection of many HandlerFunc with each having a different generic parameter is possible by describing each HandlerFunc with this unified Handler interface. Eventually a Handler is anything that can report its requestTypes, handlerResponses, sourcePosition and can become a BoundHandlerFunc by calling the handlerFactory method.
func NewHandler ¶
func NewHandler[B, P, Q, R any](h HandlerFunc[B, P, Q, R]) Handler
func RawHandler ¶
RawHandler adds a handler that doesn't define any type information.
type HandlerFunc ¶
HandlerFunc is the typed handler that declare explicitly all types of request and responses. Check the repo examples for seeing how the HandlerFunc can be used to define Handler for operations and middlewares.
func ErrorHandler ¶
func ErrorHandler[R any](errHandler func(c *Context, err error) (Response[R], error)) HandlerFunc[utils.Nil, utils.Nil, utils.Nil, R]
ErrorHandler allows providing a handler function that can handle errors occurred in the handlers chain. This type of handler is particularly useful for handling BadRequestErr caused by a request binding errors and translate it to an HTTP response.
type JSONContentType ¶
type JSONContentType struct{}
func (JSONContentType) Mime ¶
func (t JSONContentType) Mime() string
type MonitoredHTTP ¶ added in v1.7.1
type MonitoredHTTP interface { io.ReadCloser http.ResponseWriter HTTPDurations }
func NewMonitoredHTTP ¶ added in v1.7.1
func NewMonitoredHTTP(writer http.ResponseWriter, reader io.ReadCloser) MonitoredHTTP
type OctetStreamContentType ¶
type OctetStreamContentType struct{}
func (OctetStreamContentType) Decode ¶
func (t OctetStreamContentType) Decode(data []byte, value any) error
func (OctetStreamContentType) Encode ¶
func (t OctetStreamContentType) Encode(value any) ([]byte, error)
func (OctetStreamContentType) Mime ¶
func (t OctetStreamContentType) Mime() string
type OpenAPIRouter ¶
type OpenAPIRouter interface { // Use middlewares on the root handler Use(...Handler) OpenAPIRouter // WithGroup defines a virtual group that allow defining middlewares for group of routes more easily WithGroup(Group) OpenAPIRouter // WithOperation attaches an MiddlewareHandler for an operation ID string on the OpenAPISpec WithOperation(string, Handler, ...Handler) OpenAPIRouter // WithContentType add to the router implementation for encoding additional content types. // // By default, the OpenAPIRouter supports the ContentTypes defined by DefaultContentTypes. // // An implementation of ContentType add support for additional content types by implementing serialization and // deserialization. WithContentType(ContentType) OpenAPIRouter // AsHandler validates the validity of the specified implementation with the registered OpenAPISpec. // // Returns a http.Handler and nil error if all checks passed correctly // // The returned handler can be used easily with any go http web framework as it is implementing the builtin // http.Handler interface. AsHandler() (http.Handler, error) // Spec returns the OpenAPI spec used by the router. Spec() OpenAPISpec }
func NewOpenAPIRouter ¶
func NewOpenAPIRouter(spec OpenAPISpec) OpenAPIRouter
NewOpenAPIRouter creates new OpenAPIRouter router with the provided OpenAPISpec.
The OpenAPISpec can be obtained by calling any of the OpenAPISpec loading functions:
- NewSpecFromData - Init an OpenAPISpec from bytes represent the spec JSON or YAML.
- NewSpecFromFile - Init an OpenAPISpec from a path to a spec file.
- NewSpec - Create an empty spec object that can be initialized programmatically.
This function also initialize OpenAPIRouter with the DefaultOptions, you can also initialize the router with custom Options using the NewOpenAPIRouterWithOptions function instead.
func NewOpenAPIRouterWithOptions ¶
func NewOpenAPIRouterWithOptions(spec OpenAPISpec, options Options) OpenAPIRouter
NewOpenAPIRouterWithOptions creates new OpenAPIRouter router with the provided OpenAPISpec and custom Options.
OpenAPISpec can be obtained by calling any of the OpenAPISpec loading functions:
- NewSpecFromData - Init an OpenAPISpec from bytes represent the spec JSON or YAML.
- NewSpecFromFile - Init an OpenAPISpec from a path to a spec file.
- NewSpec - Create an empty spec object that can be initialized programmatically.
This function also receives an Options object that allow customizing the default behaviour of the router. Check the Options documentation for more details.
If you want to use the OpenAPIRouter with the DefaultOptions, you can use the shorter NewOpenAPIRouter function instead.
type OpenAPISpec ¶
func NewSpec ¶
func NewSpec() OpenAPISpec
func NewSpecFromData ¶
func NewSpecFromData(data []byte) (OpenAPISpec, error)
func NewSpecFromFile ¶
func NewSpecFromFile(path string) (OpenAPISpec, error)
func (*OpenAPISpec) Operations ¶ added in v1.0.12
func (s *OpenAPISpec) Operations() map[string]SpecOperation
Operations returns all operations declared in the spec as a map of operation ID to SpecOperation.
type OperationValidationOptions ¶
type OperationValidationOptions struct { // ValidatePathParams determines validation of operation request body. ValidateRequestBody Behaviour `json:"validateRequestBody,omitempty"` // ValidatePathParams determines validation of operation path params. ValidatePathParams Behaviour `json:"validatePathParams,omitempty"` // HandleAllPathParams describes the behaviour when not every path params defined in the spec is handled at least once in the handlers chain HandleAllPathParams Behaviour `json:"handleAllPathParams,omitempty"` // ValidatePathParams determines validation of operation query params. ValidateQueryParams Behaviour `json:"validateQueryParams,omitempty"` // HandleAllQueryParams describes the behaviour when not every query params defined in the spec is handled at least once in the handlers chain HandleAllQueryParams Behaviour `json:"handleAllQueryParams,omitempty"` // ValidatePathParams determines validation of operation responses. ValidateResponses Behaviour `json:"validateResponses,omitempty"` // HandleAllOperationResponses describes the behaviour when not every response defined in the spec is handled at least once in the handlers chain HandleAllOperationResponses Behaviour `json:"handleAllOperationResponses,omitempty"` // ContentTypesToSkipRuntimeValidation defines a list of content types that are skipped when validating operation request body at runtime. ContentTypesToSkipRuntimeValidation []string `json:"contentTypesToSkipRuntimeValidation,omitempty"` // RuntimeValidateResponses defines the behaviour when validating operation response body at runtime. Printing a warning to // the log by default. It is recommended to turn this option to Ignore in production as it can impact performance for large // responses, and to be used in development and testing environments. RuntimeValidateResponses Behaviour `json:"runtimeValidateResponses,omitempty"` }
OperationValidationOptions defines options to control operation validations
type Options ¶
type Options struct { // When RecoverOnPanic is set to true the handlers chain provide a default recover behaviour that return status 500. // This feature is available and turned on by default to prevent the server from crashing when an internal panic // occurs in the handler chain. // This is a fundamental requirement of a web server to support availability. // While being fundamental, the default behaviour can't conform to your spec out of the box. // To be able to better control the behaviour during panic and the structure of the response returned to be // compatible with the spec it's recommended to implement it as a handler and in this case the default recover // behaviour can be turned off. RecoverOnPanic bool `json:"recoverOnPanic,omitempty"` // LogLevel defines what log levels should be printed to LogOutput. // By default, LogLevel is set to utils.Info to print all info to the log. // The router prints to the log only during initialization to show validation errors, warnings and useful info. // No printing is done after initialization. LogLevel LogLevel `json:"logLevel,omitempty"` // LogOutput defines where to write the outputs too. // By default, it is set to write to os.Stderr. // You can provide your own writer to be able to read the validation errors programmatically or to write them to // different destination. // The router prints to the log only during initialization to show validation errors, warnings and useful info. // No printing is done after initialization. LogOutput io.Writer `json:"-"` // OperationValidations allow defining validation for specific operations using a map of operation id to an // operationValidationOptions structure. // This option is used only to override the default operation validations options defined ny the // DefaultOperationValidation option. OperationValidations map[string]OperationValidationOptions `json:"operationValidations,omitempty"` // DefaultOperationValidation defines the default validations run for every operation. // By default, if not set the default behaviour is strict and consider any validation error to fail the entire // router validations. DefaultOperationValidation OperationValidationOptions `json:"defaultOperationValidation,omitempty"` // The router validates that every operation defined in the spec has an implementation. // MustHandleAllOperations defines the behaviour when this validation fails. // By default, it is set to utils.Error to propagate the error. // If your implementation is still in progress, and you would like to ignore unimplemented operations you can set // this option to utils.Warn or turn it off with utils.Off. MustHandleAllOperations Behaviour `json:"mustHandleAllOperations,omitempty"` // The router validates that every content type define in the spec has an implementation. // HandleAllContentTypes defines the behaviour when this validation fails. // By default, it is set to utils.Error to propagate the error. // Content types defined in the spec in operation request body and responses. // If your spec defines some content types that are not supported by the default router content types you can add // your own implementation. // Content type implementation is basically an implementation of ContentType interface add with // OpenAPIRouter.WithContentType to define serialization and deserialization behaviour. HandleAllContentTypes Behaviour `json:"handleAllContentTypes,omitempty"` // ExcludeOperations defined an array of operations that are defined in the spec but are excluded from the implementation. // One use for this option is when your spec defines the entire API of your app but the implementation is spread to multiple microservices. // With this option you can define list of operations that are to be implemented by other microservices. ExcludeOperations []string // OptionsHandler defines a handler that is called for every OPTIONS request of any path that is defined in the spec. // This handler allows you to define a response for OPTIONS preflight requests and control the allowed methods or set CORS headers. // The handler will receive the allowed methods in the `Allow` header based on the spec. // Set to nil to disable the automatic handling of OPTIONS requests. OptionsHandler http.Handler `json:"-"` }
Options defines the behaviour of the OpenAPI router
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns the default OpenAPI Router Options. For loading different options it is recommended to start from the default Options as the baseline and modify the specific options needed. To init the router with custom Options you can use NewOpenAPIRouterWithOptions.
type PlainTextContentType ¶
type PlainTextContentType struct{}
func (PlainTextContentType) Decode ¶
func (t PlainTextContentType) Decode(data []byte, value any) error
func (PlainTextContentType) Encode ¶
func (t PlainTextContentType) Encode(value any) ([]byte, error)
func (PlainTextContentType) Mime ¶
func (t PlainTextContentType) Mime() string
type RawResponse ¶
type Response ¶
type Response[R any] struct { // contains filtered or unexported fields }
func SendBytes ¶
SendBytes constructs a new Response with an "application/octet-stream" content-type.
func SendOKBytes ¶
SendOKBytes constructs a new Response with an OK 200 status code and with an "application/octet-stream" content-type.
func SendOKJSON ¶
SendOKJSON constructs a new Response with an OK 200 status code and with an "application/json" content-type.
func SendOKText ¶
SendOKText constructs a new Response with an OK 200 status code and with a "plain/text" content-type.
func (Response[R]) AddHeader ¶
AddHeader add a response header. It appends to any existing values associated with key.
func (Response[R]) ContentType ¶
ContentType set the response content type.
type SchemaValidationOptions ¶
type SchemaValidationOptions struct { // NoEmptyInterface defines the behaviour when a schema is validated with an empty interface (any) type. NoEmptyInterface Behaviour `json:"noEmptyInterface,omitempty"` // NoStringAnyMapForObjectsSchema defines the behaviour when an object schema is validated with a string to empty interface map type (map[string]any). NoStringAnyMapForObjectsSchema Behaviour `json:"noStringAnyMapForObjectsSchema,omitempty"` }
SchemaValidationOptions defines options to control schema validations