Documentation ¶
Overview ¶
Package fhttp provides instrumentation for net/http.
Index ¶
- func AnnotateOpenAPI(s *openapi.Collector, setup ...func(op *openapi3.Operation) error) func(fchi.Handler) fchi.Handler
- func AnnotateOperation(annotations ...func(operation *openapi3.Operation) error) func(h *Handler)
- func HTTPBasicSecurityMiddleware(c *openapi.Collector, name, description string, ...) func(fchi.Handler) fchi.Handler
- func HTTPBearerSecurityMiddleware(c *openapi.Collector, name, description, bearerFormat string, ...) func(fchi.Handler) fchi.Handler
- func HandlerAs(handler fchi.Handler, target interface{}) bool
- func HandlerWithRouteMiddleware(method, pathPattern string) func(fchi.Handler) fchi.Handler
- func OpenAPIMiddleware(s *openapi.Collector) func(fchi.Handler) fchi.Handler
- func OptionsMiddleware(options ...func(h *Handler)) func(h fchi.Handler) fchi.Handler
- func RequestMapping(v interface{}) func(h *Handler)
- func ResponseHeaderMapping(v interface{}) func(h *Handler)
- func SecurityMiddleware(c *openapi.Collector, name string, scheme openapi3.SecurityScheme, ...) func(fchi.Handler) fchi.Handler
- func SecurityResponse(structure interface{}, httpStatus int) func(config *MiddlewareConfig)
- func SuccessStatus(status int) func(h *Handler)
- func SuccessfulResponseContentType(contentType string) func(h *Handler)
- func UseCaseMiddlewares(mw ...usecase.Middleware) func(fchi.Handler) fchi.Handler
- func WrapHandler(h fchi.Handler, mw ...func(fchi.Handler) fchi.Handler) fchi.Handler
- type Handler
- func (h *Handler) ServeHTTP(ctx context.Context, rc *fasthttp.RequestCtx)
- func (h *Handler) SetRequestDecoder(requestDecoder RequestDecoder)
- func (h *Handler) SetResponseEncoder(responseEncoder ResponseEncoder)
- func (h *Handler) SetUseCase(useCase usecase.Interactor)
- func (h *Handler) UseCase() usecase.Interactor
- type MiddlewareConfig
- type RequestDecoder
- type ResponseEncoder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnnotateOpenAPI ¶
func AnnotateOpenAPI( s *openapi.Collector, setup ...func(op *openapi3.Operation) error, ) func(fchi.Handler) fchi.Handler
AnnotateOpenAPI applies OpenAPI annotation to relevant handlers.
func AnnotateOperation ¶
AnnotateOperation allows customizations of prepared operations.
func HTTPBasicSecurityMiddleware ¶
func HTTPBasicSecurityMiddleware( c *openapi.Collector, name, description string, options ...func(*MiddlewareConfig), ) func(fchi.Handler) fchi.Handler
HTTPBasicSecurityMiddleware creates middleware to expose Basic Security schema.
func HTTPBearerSecurityMiddleware ¶
func HTTPBearerSecurityMiddleware( c *openapi.Collector, name, description, bearerFormat string, options ...func(*MiddlewareConfig), ) func(fchi.Handler) fchi.Handler
HTTPBearerSecurityMiddleware creates middleware to expose HTTP Bearer security schema.
func HandlerAs ¶
HandlerAs finds the first fchi.Handler in fchi.Handler's chain that matches target, and if so, sets target to that fchi.Handler value and returns true.
An fchi.Handler matches target if the fchi.Handler's concrete value is assignable to the value pointed to by target.
HandlerAs will panic if target is not a non-nil pointer to either a type that implements fchi.Handler, or to any interface type.
func HandlerWithRouteMiddleware ¶
HandlerWithRouteMiddleware wraps handler with routing information.
func OpenAPIMiddleware ¶
OpenAPIMiddleware reads info and adds validation to handler.
func OptionsMiddleware ¶
OptionsMiddleware applies options to encountered fhttp.Handler.
func RequestMapping ¶
func RequestMapping(v interface{}) func(h *Handler)
RequestMapping creates rest.RequestMapping from struct tags.
This can be used to decouple mapping from usecase input with additional struct.
func ResponseHeaderMapping ¶
func ResponseHeaderMapping(v interface{}) func(h *Handler)
ResponseHeaderMapping creates headers mapping from struct tags.
This can be used to decouple mapping from usecase input with additional struct.
func SecurityMiddleware ¶
func SecurityMiddleware( c *openapi.Collector, name string, scheme openapi3.SecurityScheme, options ...func(*MiddlewareConfig), ) func(fchi.Handler) fchi.Handler
SecurityMiddleware creates middleware to expose security scheme.
Example ¶
package main import ( "bytes" "context" "net/http" "github.com/swaggest/fchi" "github.com/swaggest/openapi-go/openapi3" "github.com/swaggest/rest-fasthttp/chirouter" "github.com/swaggest/rest-fasthttp/fhttp" "github.com/swaggest/rest/openapi" "github.com/swaggest/usecase" "github.com/valyala/fasthttp" ) func main() { // Create router. r := chirouter.NewWrapper(fchi.NewRouter()) // Init API documentation schema. apiSchema := &openapi.Collector{} // Setup middlewares (non-documentary middlewares omitted for brevity). r.Wrap( fhttp.OpenAPIMiddleware(apiSchema), // Documentation collector. ) // Configure an actual security middleware. serviceTokenAuth := func(h fchi.Handler) fchi.Handler { return fchi.HandlerFunc(func(ctx context.Context, rc *fasthttp.RequestCtx) { if !bytes.Equal(rc.Request.Header.Peek("Authorization"), []byte("<secret>")) { fchi.Error(rc, "Authentication failed.", http.StatusUnauthorized) return } h.ServeHTTP(ctx, rc) }) } // Configure documentation middleware to describe actual security middleware. serviceTokenDoc := fhttp.SecurityMiddleware(apiSchema, "serviceToken", openapi3.SecurityScheme{ APIKeySecurityScheme: &openapi3.APIKeySecurityScheme{ Name: "Authorization", In: openapi3.APIKeySecuritySchemeInHeader, }, }) u := usecase.NewIOI(nil, nil, func(ctx context.Context, input, output interface{}) error { // Do something. return nil }) // Add use case handler to router with security middleware. r. With(serviceTokenAuth, serviceTokenDoc). // Apply a pair of middlewares: actual security and documentation. Method(http.MethodGet, "/foo", fhttp.NewHandler(u)) }
Output:
func SecurityResponse ¶
func SecurityResponse(structure interface{}, httpStatus int) func(config *MiddlewareConfig)
SecurityResponse is a security middleware option to customize response structure and status.
func SuccessStatus ¶
SuccessStatus sets status code of successful response.
func SuccessfulResponseContentType ¶
SuccessfulResponseContentType sets Content-Type of successful response.
func UseCaseMiddlewares ¶
UseCaseMiddlewares applies use case middlewares to Handler.
func WrapHandler ¶
WrapHandler wraps fchi.Handler with an unwrappable middleware.
Wrapping order is reversed, e.g. if you call WrapHandler(h, mw1, mw2, mw3) middlewares will be invoked in order of mw1(mw2(mw3(h))), mw3 first and mw1 last. So that request processing is first affected by mw1.
Types ¶
type Handler ¶
type Handler struct { rest.HandlerTrait // HandleErrResponse allows control of error response processing. HandleErrResponse func(ctx context.Context, rc *fasthttp.RequestCtx, err error) // contains filtered or unexported fields }
Handler is a use case http handler with documentation and inputPort validation.
Please use NewHandler to create instance.
func NewHandler ¶
func NewHandler(useCase usecase.Interactor, options ...func(h *Handler)) *Handler
NewHandler creates use case http handler.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(ctx context.Context, rc *fasthttp.RequestCtx)
ServeHTTP serves http inputPort with use case interactor.
func (*Handler) SetRequestDecoder ¶
func (h *Handler) SetRequestDecoder(requestDecoder RequestDecoder)
SetRequestDecoder sets request decoder.
func (*Handler) SetResponseEncoder ¶
func (h *Handler) SetResponseEncoder(responseEncoder ResponseEncoder)
SetResponseEncoder sets response encoder.
func (*Handler) SetUseCase ¶
func (h *Handler) SetUseCase(useCase usecase.Interactor)
SetUseCase prepares handler for a use case.
func (*Handler) UseCase ¶
func (h *Handler) UseCase() usecase.Interactor
UseCase returns use case interactor.
type MiddlewareConfig ¶
type MiddlewareConfig struct { // ResponseStructure declares structure that is used for unauthorized message, default rest.ErrResponse{}. ResponseStructure interface{} // ResponseStatus declares HTTP status code that is used for unauthorized message, default http.StatusUnauthorized. ResponseStatus int }
MiddlewareConfig defines security middleware options.
type RequestDecoder ¶
type RequestDecoder interface {
Decode(rc *fasthttp.RequestCtx, input interface{}, validator rest.Validator) error
}
RequestDecoder maps data from http.Request into structured Go input value.
type ResponseEncoder ¶
type ResponseEncoder interface { WriteErrResponse(rc *fasthttp.RequestCtx, statusCode int, response interface{}) WriteSuccessfulResponse( rc *fasthttp.RequestCtx, output interface{}, ht rest.HandlerTrait, ) SetupOutput(output interface{}, ht *rest.HandlerTrait) MakeOutput(rc *fasthttp.RequestCtx, ht rest.HandlerTrait) interface{} }
ResponseEncoder writes data from use case output/error into http.ResponseWriter.