Documentation
¶
Overview ¶
Package fiberopenapi provides an OpenAPI documentation generator for Fiber applications.
This package wraps the Fiber router and provides methods to define OpenAPI specifications, security schemes, and serve the Swagger UI.
Index ¶
- type ConfigOption
- func WithBaseURL(baseURL string) ConfigOption
- func WithDescription(description string) ConfigOption
- func WithDocsPath(path string) ConfigOption
- func WithOpenAPI(enable ...bool) ConfigOption
- func WithSecurity(name string, opts ...SecurityOption) ConfigOption
- func WithServer(url string, description ...string) ConfigOption
- func WithSwaggerConfig(cfg ...*SwaggerConfig) ConfigOption
- func WithTitle(title string) ConfigOption
- func WithVersion(version string) ConfigOption
- type Route
- type Router
- type SecurityOption
- type SwaggerConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConfigOption ¶
type ConfigOption func(*config)
ConfigOption defines a function that applies configuration to the OpenAPI router.
func WithBaseURL ¶
func WithBaseURL(baseURL string) ConfigOption
WithBaseURL sets the base URL for the OpenAPI documentation.
func WithDescription ¶
func WithDescription(description string) ConfigOption
WithDescription sets the description for the OpenAPI documentation.
func WithDocsPath ¶
func WithDocsPath(path string) ConfigOption
WithDocsPath sets the path for the OpenAPI documentation.
func WithOpenAPI ¶
func WithOpenAPI(enable ...bool) ConfigOption
WithOpenAPI enables or disables the OpenAPI functionality.
If true, OpenAPI functionality is enabled; if false, it is disabled. By default, OpenAPI is enabled.
It can be used to conditionally disable OpenAPI at production environment.
func WithSecurity ¶
func WithSecurity(name string, opts ...SecurityOption) ConfigOption
WithSecurity adds a security scheme to the OpenAPI documentation.
It can be used to define API key or HTTP Bearer authentication schemes.
func WithServer ¶
func WithServer(url string, description ...string) ConfigOption
WithServer adds a server to the OpenAPI documentation.
func WithSwaggerConfig ¶
func WithSwaggerConfig(cfg ...*SwaggerConfig) ConfigOption
WithSwagger sets the configuration for Swagger UI.
func WithTitle ¶
func WithTitle(title string) ConfigOption
WithTitle sets the title for the OpenAPI documentation.
func WithVersion ¶
func WithVersion(version string) ConfigOption
WithVersion sets the version for the OpenAPI documentation.
type Route ¶
type Route interface {
// Name sets the name for the route.
Name(name string) Route
// With applies the given options to the route.
With(opts ...option.OperationOption) Route
}
Route represents a single route in the OpenAPI specification.
type Router ¶
type Router interface {
// Use applies middleware to the router.
Use(args ...any) Router
// Get registers a GET route.
Get(path string, handler ...fiber.Handler) Route
// Head registers a HEAD route.
Head(path string, handler ...fiber.Handler) Route
// Post registers a POST route.
Post(path string, handler ...fiber.Handler) Route
// Put registers a PUT route.
Put(path string, handler ...fiber.Handler) Route
// Patch registers a PATCH route.
Patch(path string, handler ...fiber.Handler) Route
// Delete registers a DELETE route.
Delete(path string, handler ...fiber.Handler) Route
// Connect registers a CONNECT route.
Connect(path string, handler ...fiber.Handler) Route
// Options registers an OPTIONS route.
Options(path string, handler ...fiber.Handler) Route
// Trace registers a TRACE route.
Trace(path string, handler ...fiber.Handler) Route
// Add registers a route with the specified method and path.
Add(method, path string, handler ...fiber.Handler) Route
// Static serves static files from the specified root directory.
Static(prefix, root string, config ...fiber.Static) Router
// Group creates a new sub-router with the specified prefix and handlers.
// The prefix is prepended to all routes in the sub-router.
Group(prefix string, handlers ...fiber.Handler) Router
// Route creates a new sub-router with the specified prefix and applies options.
Route(prefix string, fn func(router Router), opts ...option.RouteOption) Router
// Validate checks for errors at OpenAPI router initialization.
//
// It returns an error if there are issues with the OpenAPI configuration.
Validate() error
// GenerateOpenAPISchema generates the OpenAPI schema in the specified format.
// Supported formats are "json" and "yaml".
// If no format is specified, "yaml" is used by default.
GenerateOpenAPISchema(format ...string) ([]byte, error)
WriteSchemaTo(filePath string) error
}
Router defines the interface for an OpenAPI router.
func NewRouter ¶
func NewRouter(r fiber.Router, opts ...ConfigOption) Router
NewRouter creates a new OpenAPI router with the specified Fiber router and options. It initializes the OpenAPI reflector and sets up the documentation routes. If OpenAPI is disabled, it returns a router without OpenAPI functionality.
type SecurityOption ¶
type SecurityOption func(*securityConfig)
SecurityOption is a function that applies configuration to a securityConfig.
func SecurityAPIKey ¶
func SecurityAPIKey(name string, in openapi31.SecuritySchemeAPIKeyIn) SecurityOption
SecurityAPIKey creates a security scheme for API key authentication.
func SecurityDescription ¶
func SecurityDescription(description string) SecurityOption
SecurityDescription sets the description for the security scheme.
func SecurityHTTPBearer ¶
func SecurityHTTPBearer(scheme ...string) SecurityOption
SecurityHTTPBearer creates a security scheme for HTTP Bearer authentication.
func SecurityOauth2 ¶
func SecurityOauth2(flows openapi31.OauthFlows) SecurityOption
SecurityOauth2 creates a security scheme for OAuth 2.0 authentication.
type SwaggerConfig ¶
type SwaggerConfig struct {
ShowTopBar bool
HideCurl bool
JsonEditor bool
PreAuthorizeApiKey map[string]string
// SettingsUI contains keys and plain javascript values of SwaggerUIBundle configuration.
// Overrides default values.
// See https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ for available options.
SettingsUI map[string]string
// Proxy enables proxying requests through swgui handler.
// Can be useful if API is not directly available due to CORS policy.
Proxy bool
}
SwaggerConfig holds the configuration for Swagger UI.