Documentation
¶
Overview ¶
Package autofiber provides a FastAPI-like wrapper for the Fiber web framework. It enables automatic request parsing, validation, and OpenAPI/Swagger documentation generation.
Package autofiber provides OpenAPI 3.0 specification generation for automatic API documentation.
Package autofiber provides OpenAPI/Swagger documentation configuration and serving utilities.
Package autofiber provides route group functionality with automatic request parsing, validation, and documentation generation.
Package autofiber provides handler creation utilities for automatic request parsing, validation, and response handling.
Package autofiber provides map and interface parsing utilities for converting data structures to Go structs.
Package autofiber provides middleware functions for automatic request parsing, validation, and response handling.
Package autofiber provides route configuration options for building APIs with automatic parsing, validation, and documentation.
Package autofiber provides request parsing utilities for extracting and validating data from multiple sources.
Package autofiber provides HTTP route registration methods with automatic request parsing, validation, and documentation generation.
Package autofiber provides core types and configuration for the AutoFiber web framework.
Package autofiber provides response validation utilities for ensuring API responses match expected schemas.
Index ¶
- func AutoParseRequest(schema interface{}, customValidator *validator.Validate) fiber.Handler
- func GetParsedRequest[T any](c *fiber.Ctx) *T
- func GetValidator() *validator.Validate
- func ParseFromInterface(data interface{}, schema interface{}) error
- func ParseFromMap(data map[string]interface{}, schema interface{}) error
- func Simple(handler func(*fiber.Ctx) error) fiber.Handler
- func ValidateAndJSON(c *fiber.Ctx, data interface{}) error
- type AutoFiber
- func (af *AutoFiber) All(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (af *AutoFiber) Delete(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (af *AutoFiber) Get(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (af *AutoFiber) GetOpenAPIJSON() ([]byte, error)
- func (af *AutoFiber) GetOpenAPISpec() *OpenAPISpec
- func (af *AutoFiber) Group(prefix string, handlers ...fiber.Handler) *AutoFiberGroup
- func (af *AutoFiber) Head(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (af *AutoFiber) Listen(addr string) error
- func (af *AutoFiber) Options(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (af *AutoFiber) Patch(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (af *AutoFiber) Post(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (af *AutoFiber) Put(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (af *AutoFiber) ServeDocs(path string)
- func (af *AutoFiber) ServeSwaggerUI(swaggerPath, docsPath string)
- func (af *AutoFiber) Test(req *http.Request, msTimeout ...int) (*http.Response, error)
- func (af *AutoFiber) Use(args ...interface{}) fiber.Router
- type AutoFiberGroup
- func (ag *AutoFiberGroup) All(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (ag *AutoFiberGroup) Delete(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (ag *AutoFiberGroup) Get(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (ag *AutoFiberGroup) Head(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (ag *AutoFiberGroup) Options(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (ag *AutoFiberGroup) Patch(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (ag *AutoFiberGroup) Post(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (ag *AutoFiberGroup) Put(path string, handler interface{}, options ...RouteOption) fiber.Router
- func (ag *AutoFiberGroup) Use(args ...interface{}) fiber.Router
- type AutoFiberOption
- type DocsGenerator
- type FieldInfo
- type HandlerFunc
- type HandlerWithRequest
- type OpenAPIComponents
- type OpenAPIContact
- type OpenAPIInfo
- type OpenAPILicense
- type OpenAPIMediaType
- type OpenAPIOperation
- type OpenAPIParameter
- type OpenAPIPath
- type OpenAPIRequestBody
- type OpenAPIResponse
- type OpenAPISchema
- type OpenAPIServer
- type OpenAPISpec
- type OpenAPITag
- type ParseError
- type ParseSource
- type RouteInfo
- type RouteOption
- type RouteOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AutoParseRequest ¶
AutoParseRequest returns middleware for automatic request parsing and validation. The middleware parses request data from multiple sources (body, query, path, headers, cookies, form) based on struct tags and validates the parsed data using the provided schema. If customValidator is nil, it uses the global validator instance.
func GetParsedRequest ¶
GetParsedRequest retrieves the parsed request from context. This function extracts the parsed request data that was stored by AutoParseRequest middleware. It returns nil if no parsed request is found or if the type assertion fails.
func GetValidator ¶
GetValidator returns the global validator instance for validation.
func ParseFromInterface ¶
func ParseFromInterface(data interface{}, schema interface{}) error
ParseFromInterface parses a struct from any interface{} (map, struct, etc.). It supports map[string]interface{}, map[string]string, and struct types. The schema parameter must be a pointer to the target struct.
func ParseFromMap ¶
ParseFromMap parses a struct from a map[string]interface{}. It uses JSON tags to map keys to struct fields and sets the values accordingly. The schema parameter must be a pointer to the target struct.
func Simple ¶
Simple wraps a simple handler function. This is a utility function for creating Fiber handlers from simple functions.
func ValidateAndJSON ¶
ValidateAndJSON validates response data and returns JSON. If response validation is configured, it validates the data against the response schema before returning the JSON response. If validation fails, it returns an error response. If no validation is configured, it simply returns the JSON response.
Types ¶
type AutoFiber ¶
AutoFiber is the main application struct for building APIs with automatic parsing, validation, and documentation.
func New ¶
func New(config fiber.Config, options ...AutoFiberOption) *AutoFiber
New creates a new AutoFiber application instance with custom options.
func (*AutoFiber) All ¶
func (af *AutoFiber) All(path string, handler interface{}, options ...RouteOption) fiber.Router
All registers a route for all HTTP methods with automatic request parsing, validation, and documentation generation. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiber) Delete ¶
func (af *AutoFiber) Delete(path string, handler interface{}, options ...RouteOption) fiber.Router
Delete registers a DELETE route with automatic request parsing, validation, and documentation generation. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiber) Get ¶
func (af *AutoFiber) Get(path string, handler interface{}, options ...RouteOption) fiber.Router
Get registers a GET route with automatic request parsing, validation, and documentation generation. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiber) GetOpenAPIJSON ¶
GetOpenAPIJSON returns the OpenAPI specification as JSON bytes. This is useful for serving the specification via HTTP or saving to a file.
func (*AutoFiber) GetOpenAPISpec ¶
func (af *AutoFiber) GetOpenAPISpec() *OpenAPISpec
GetOpenAPISpec returns the complete OpenAPI specification as a struct. If no documentation info is set, it uses default values.
func (*AutoFiber) Group ¶
func (af *AutoFiber) Group(prefix string, handlers ...fiber.Handler) *AutoFiberGroup
Group creates a new route group with the given prefix.
func (*AutoFiber) Head ¶
func (af *AutoFiber) Head(path string, handler interface{}, options ...RouteOption) fiber.Router
Head registers a HEAD route with automatic request parsing, validation, and documentation generation. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiber) Options ¶
func (af *AutoFiber) Options(path string, handler interface{}, options ...RouteOption) fiber.Router
Options registers an OPTIONS route with automatic request parsing, validation, and documentation generation. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiber) Patch ¶
func (af *AutoFiber) Patch(path string, handler interface{}, options ...RouteOption) fiber.Router
Patch registers a PATCH route with automatic request parsing, validation, and documentation generation. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiber) Post ¶
func (af *AutoFiber) Post(path string, handler interface{}, options ...RouteOption) fiber.Router
Post registers a POST route with automatic request parsing, validation, and documentation generation. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiber) Put ¶
func (af *AutoFiber) Put(path string, handler interface{}, options ...RouteOption) fiber.Router
Put registers a PUT route with automatic request parsing, validation, and documentation generation. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiber) ServeDocs ¶
ServeDocs serves the OpenAPI specification as JSON at the specified path. This creates a GET route that returns the OpenAPI specification.
func (*AutoFiber) ServeSwaggerUI ¶
ServeSwaggerUI serves Swagger UI for the OpenAPI documentation. This creates a GET route that serves an HTML page with Swagger UI interface. swaggerPath is the URL path where Swagger UI will be served. docsPath is the URL path where the OpenAPI JSON specification is served.
type AutoFiberGroup ¶
type AutoFiberGroup struct {
Group *fiber.Group // Underlying Fiber group
Prefix string // Prefix of the group
// contains filtered or unexported fields
}
AutoFiberGroup represents a group of routes with a common prefix and shared middleware.
func (*AutoFiberGroup) All ¶
func (ag *AutoFiberGroup) All(path string, handler interface{}, options ...RouteOption) fiber.Router
All registers a route for all HTTP methods with automatic request parsing, validation, and documentation generation in the group. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiberGroup) Delete ¶
func (ag *AutoFiberGroup) Delete(path string, handler interface{}, options ...RouteOption) fiber.Router
Delete registers a DELETE route with automatic request parsing, validation, and documentation generation in the group. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiberGroup) Get ¶
func (ag *AutoFiberGroup) Get(path string, handler interface{}, options ...RouteOption) fiber.Router
Get registers a GET route with automatic request parsing, validation, and documentation generation in the group. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiberGroup) Head ¶
func (ag *AutoFiberGroup) Head(path string, handler interface{}, options ...RouteOption) fiber.Router
Head registers a HEAD route with automatic request parsing, validation, and documentation generation in the group. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiberGroup) Options ¶
func (ag *AutoFiberGroup) Options(path string, handler interface{}, options ...RouteOption) fiber.Router
Options registers an OPTIONS route with automatic request parsing, validation, and documentation generation in the group. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiberGroup) Patch ¶
func (ag *AutoFiberGroup) Patch(path string, handler interface{}, options ...RouteOption) fiber.Router
Patch registers a PATCH route with automatic request parsing, validation, and documentation generation in the group. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiberGroup) Post ¶
func (ag *AutoFiberGroup) Post(path string, handler interface{}, options ...RouteOption) fiber.Router
Post registers a POST route with automatic request parsing, validation, and documentation generation in the group. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiberGroup) Put ¶
func (ag *AutoFiberGroup) Put(path string, handler interface{}, options ...RouteOption) fiber.Router
Put registers a PUT route with automatic request parsing, validation, and documentation generation in the group. The handler can be a simple function or a function that accepts a parsed request struct. Options can be provided to configure request/response schemas, middleware, and documentation.
func (*AutoFiberGroup) Use ¶
func (ag *AutoFiberGroup) Use(args ...interface{}) fiber.Router
Use adds middleware to the group. This middleware will be applied to all routes registered in this group.
type AutoFiberOption ¶ added in v0.3.0
type AutoFiberOption func(*AutoFiber)
AutoFiberOption is a function type for configuring AutoFiber options
func WithOpenAPI ¶ added in v0.3.0
func WithOpenAPI(info OpenAPIInfo) AutoFiberOption
WithOpenAPI sets the OpenAPI info for the documentation (no server info).
type DocsGenerator ¶
type DocsGenerator struct {
DocsInfo *OpenAPIInfo
// contains filtered or unexported fields
}
DocsGenerator handles API documentation generation and OpenAPI specification creation.
func NewDocsGenerator ¶
func NewDocsGenerator() *DocsGenerator
NewDocsGenerator creates a new documentation generator with the specified base path.
func (*DocsGenerator) AddRoute ¶
func (dg *DocsGenerator) AddRoute(path, method string, handler interface{}, options *RouteOptions)
AddRoute adds a route to the documentation generator with its metadata and options.
func (*DocsGenerator) GenerateJSON ¶
func (dg *DocsGenerator) GenerateJSON() ([]byte, error)
GenerateJSON generates the OpenAPI specification as JSON bytes. This is useful for serving the specification via HTTP or saving to a file.
func (*DocsGenerator) GenerateOpenAPISpec ¶
func (dg *DocsGenerator) GenerateOpenAPISpec() *OpenAPISpec
GenerateOpenAPISpec generates the complete OpenAPI specification from collected route information.
type FieldInfo ¶
type FieldInfo struct {
Source ParseSource // Source to parse the field from
Key string // Custom key name (e.g., "user_id" for "UserId")
Required bool // Whether the field is required
Default interface{} // Default value if not provided
Description string // Description for documentation
}
FieldInfo contains parsing information for a struct field.
type HandlerWithRequest ¶
HandlerWithRequest is a generic handler function with request parsing and response. T is the type of the parsed request struct.
type OpenAPIComponents ¶
type OpenAPIComponents struct {
Schemas map[string]OpenAPISchema `json:"schemas,omitempty"`
SecuritySchemes map[string]map[string]string `json:"securitySchemes,omitempty"`
}
OpenAPIComponents represents reusable components like schemas and security schemes.
type OpenAPIContact ¶
type OpenAPIContact struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
OpenAPIContact represents contact information for the API.
type OpenAPIInfo ¶
type OpenAPIInfo struct {
Title string `json:"title"`
Description string `json:"description,omitempty"`
Version string `json:"version"`
Contact *OpenAPIContact `json:"contact,omitempty"`
License *OpenAPILicense `json:"license,omitempty"`
}
OpenAPIInfo represents the API information including title, description, version, and contact details.
type OpenAPILicense ¶
OpenAPILicense represents license information for the API.
type OpenAPIMediaType ¶
type OpenAPIMediaType struct {
Schema *OpenAPISchema `json:"schema,omitempty"`
}
OpenAPIMediaType represents media type content (e.g., application/json).
type OpenAPIOperation ¶
type OpenAPIOperation struct {
Tags []string `json:"tags,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
OperationID string `json:"operationId,omitempty"`
Parameters []OpenAPIParameter `json:"parameters,omitempty"`
RequestBody *OpenAPIRequestBody `json:"requestBody,omitempty"`
Responses map[string]OpenAPIResponse `json:"responses"`
Security []map[string][]string `json:"security,omitempty"`
}
OpenAPIOperation represents an API operation with parameters, request body, and responses.
type OpenAPIParameter ¶
type OpenAPIParameter struct {
Name string `json:"name"`
In string `json:"in"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Schema *OpenAPISchema `json:"schema,omitempty"`
}
OpenAPIParameter represents a parameter (query, path, header, cookie) for an API operation.
type OpenAPIPath ¶
type OpenAPIPath struct {
Get *OpenAPIOperation `json:"get,omitempty"`
Post *OpenAPIOperation `json:"post,omitempty"`
Put *OpenAPIOperation `json:"put,omitempty"`
Delete *OpenAPIOperation `json:"delete,omitempty"`
Patch *OpenAPIOperation `json:"patch,omitempty"`
Head *OpenAPIOperation `json:"head,omitempty"`
Options *OpenAPIOperation `json:"options,omitempty"`
}
OpenAPIPath represents a path in the API with all supported HTTP methods.
type OpenAPIRequestBody ¶
type OpenAPIRequestBody struct {
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Content map[string]OpenAPIMediaType `json:"content"`
}
OpenAPIRequestBody represents a request body for an API operation.
type OpenAPIResponse ¶
type OpenAPIResponse struct {
Description string `json:"description"`
Content map[string]OpenAPIMediaType `json:"content,omitempty"`
}
OpenAPIResponse represents a response for an API operation.
type OpenAPISchema ¶
type OpenAPISchema struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Description string `json:"description,omitempty"`
Required []string `json:"required,omitempty"`
Properties map[string]OpenAPISchema `json:"properties,omitempty"`
Items *OpenAPISchema `json:"items,omitempty"`
Ref string `json:"$ref,omitempty"`
Example interface{} `json:"example,omitempty"`
}
OpenAPISchema represents a JSON schema for request/response data structures.
type OpenAPIServer ¶
type OpenAPIServer struct {
URL string `json:"url"`
Description string `json:"description,omitempty"`
}
OpenAPIServer represents server information for the API.
type OpenAPISpec ¶
type OpenAPISpec struct {
OpenAPI string `json:"openapi"`
Info OpenAPIInfo `json:"info"`
Servers []OpenAPIServer `json:"servers,omitempty"`
Paths map[string]OpenAPIPath `json:"paths"`
Components OpenAPIComponents `json:"components,omitempty"`
Tags []OpenAPITag `json:"tags,omitempty"`
}
OpenAPISpec represents the OpenAPI 3.0 specification structure.
type OpenAPITag ¶
type OpenAPITag struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
OpenAPITag represents a tag for grouping API operations.
type ParseError ¶
type ParseError struct {
Field string // Name of the field
Source string // Source of the field (e.g., body, query)
Message string // Error message
}
ParseError represents a parsing error for a specific field and source.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error returns the error message for a ParseError.
type ParseSource ¶
type ParseSource string
ParseSource defines where a field should be parsed from (e.g., body, query, path, header, etc.).
const ( // Body indicates the field should be parsed from the request body. Body ParseSource = "body" // Query indicates the field should be parsed from the query string. Query ParseSource = "query" // Path indicates the field should be parsed from the URL path parameters. Path ParseSource = "path" // Header indicates the field should be parsed from the request headers. Header ParseSource = "header" // Cookie indicates the field should be parsed from cookies. Cookie ParseSource = "cookie" // Form indicates the field should be parsed from form data. Form ParseSource = "form" // Auto enables smart parsing based on HTTP method and struct tags. Auto ParseSource = "auto" )
type RouteInfo ¶
type RouteInfo struct {
Path string
Method string
Handler interface{}
Options *RouteOptions
OperationID string
}
RouteInfo stores information about a route for documentation generation.
type RouteOption ¶
type RouteOption func(*RouteOptions)
RouteOption is a function that configures route options for an endpoint.
func WithDescription ¶
func WithDescription(description string) RouteOption
WithDescription sets the route description for API documentation. This description will appear in the generated OpenAPI/Swagger documentation.
func WithMiddleware ¶
func WithMiddleware(middleware ...fiber.Handler) RouteOption
WithMiddleware adds middleware to the route. Multiple middleware can be added and they will be executed in the order provided.
func WithRequestSchema ¶
func WithRequestSchema(schema interface{}) RouteOption
WithRequestSchema sets the request schema for auto-parsing. The schema should be a struct type that defines the expected request structure.
func WithResponseSchema ¶
func WithResponseSchema(schema interface{}) RouteOption
WithResponseSchema sets the response schema for documentation and validation. The schema should be a struct type that defines the expected response structure.
func WithTags ¶
func WithTags(tags ...string) RouteOption
WithTags sets the route tags for API documentation. Tags help organize and categorize routes in the generated documentation.
type RouteOptions ¶
type RouteOptions struct {
RequestSchema interface{} // Struct for request parsing and validation
ResponseSchema interface{} // Struct for response validation and documentation
Middleware []fiber.Handler // Middleware handlers for the route
Description string // Description for API documentation
Tags []string // Tags for API documentation
}
RouteOptions contains configuration for a route, such as schemas, middleware, and metadata.