Documentation
¶
Overview ¶
The package swagger provides capabilities for generating and displaying Swagger/OpenAPI documentation Based on swaggo/swag for documentation generation driven by comments
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSwaggerDisabled Swagger is not enabled ErrSwaggerDisabled = errcode.Register(errcode.New( moduleCodeSwagger, 1, "swagger", "swagger.disabled", "Swagger is disabled", http.StatusServiceUnavailable, )) // ErrSwaggerInitFailed Swagger initialization failed ErrSwaggerInitFailed = errcode.Register(errcode.New( moduleCodeSwagger, 2, "swagger", "swagger.init_failed", "Swagger initialization failed", http.StatusInternalServerError, )) // ErrSwaggerDocNotFound Swagger document not found ErrSwaggerDocNotFound = errcode.Register(errcode.New( moduleCodeSwagger, 3, "swagger", "swagger.doc_not_found", "Swagger documentation not found", http.StatusNotFound, )) )
Error code definitions
var BuildTime = time.Now().Format(time.RFC3339)
BuildTime records the generation time of swag (for debugging)
Functions ¶
func Setup ¶
Setup quick method: Obtain Manager from DI container and register routes The application layer can call this method in the OnReady callback
Using examples:
import _ "your-app/docs" // import the docs package generated by swag init
func (a *App) onReady(core *application.Application) error {
swagger.Setup(core.GetInjector(), core.GetHTTPServer().GetEngine())
return nil
}
func SetupWithInfo ¶
SetupWithInfo shortcut method: Set up SwaggerInfo and register routes For scenarios requiring dynamic setting of API information
Usage example:
import (
_ "your-app/docs"
"your-app/docs"
)
func (a *App) onReady(core *application.Application) error {
// Dynamically set API information
docs.SwaggerInfo.Title = a.GetConfig().App.Name + " API"
docs.SwaggerInfo.Version = a.GetVersion()
docs.SwaggerInfo.Host = a.GetConfig().ApiServer.Host
docs.SwaggerInfo.BasePath = "/api/v1"
swagger.SetupWithInfo(core.GetInjector(), core.GetHTTPServer().GetEngine())
return nil
}
Types ¶
type Config ¶
type Config struct {
// Enabled whether to enable Swagger (default false)
Enabled bool `mapstructure:"enabled"`
// UIPath Swagger UI route path (default "/swagger/*any")
UIPath string `mapstructure:"ui_path"`
// SpecPath OpenAPI Spec route path (default "/openapi.json")
SpecPath string `mapstructure:"spec_path"`
// DocJSONPath path for the doc.json generated by Swag (defaults to embedded)
// If empty, use swag generated docs.SwaggerInfo
DocJSONPath string `mapstructure:"doc_json_path"`
// Whether deep linking is enabled (default true)
DeepLinking bool `mapstructure:"deep_linking"`
// Whether to persist authorization information (default true)
PersistAuthorization bool `mapstructure:"persist_authorization"`
// ValidatorURL validator URL (empty string disables validation)
ValidatorURL string `mapstructure:"validator_url"`
// OAuth2RedirectURL OAuth 2.0 redirect URL
OAuth2RedirectURL string `mapstructure:"oauth2_redirect_url"`
}
Configure Swagger settings
type ContactInfo ¶
type ContactInfo struct {
Name string `mapstructure:"name"`
URL string `mapstructure:"url"`
Email string `mapstructure:"email"`
}
ContactInfo Contact information
type LicenseInfo ¶
LicenseInfo license information
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager Swagger Manager Encapsulate swaggo initialization and Gin middleware mounting
func NewManager ¶
func NewManager(cfg Config, info SwaggerInfo, log *logger.CtxZapLogger) *Manager
Create Swagger Manager
func ProvideManager ¶
ProvideManager creates the Swagger Manager's samber/do provider
func (*Manager) GetInfo ¶
func (m *Manager) GetInfo() SwaggerInfo
GetInfo returns API documentation metadata
func (*Manager) RegisterRoutes ¶
RegisterRoutes registers Swagger routes to the Gin Engine This method is called by the application layer to mount Swagger UI and Spec routes
Usage example:
import _ "your-app/docs" // import the docs package generated by swag init
swaggerManager.RegisterRoutes(engine)
type SecurityDefinition ¶
type SecurityDefinition struct {
Type string `mapstructure:"type"` // "apiKey", "oauth2", "basic"
Description string `mapstructure:"description"`
Name string `mapstructure:"name"` // Header/Query Parameter Names
In string `mapstructure:"in"` // "header", "query"
Flow string `mapstructure:"flow"` // OAuth2 flow
AuthorizationURL string `mapstructure:"authorization_url"`
TokenURL string `mapstructure:"token_url"`
Scopes map[string]string `mapstructure:"scopes"`
}
SecurityDefinition Security definition
type SwaggerInfo ¶
type SwaggerInfo struct {
// Title API title (corresponds to @title)
Title string `mapstructure:"title"`
// Description API description (corresponding to @description)
Description string `mapstructure:"description"`
// Version API version (corresponds to @version)
Version string `mapstructure:"version"`
// Host host address (corresponds to @host)
// Leave blank for automatic detection
Host string `mapstructure:"host"`
// BasePath API base path (corresponds to @BasePath)
BasePath string `mapstructure:"base_path"`
// Schemes supported protocols (corresponding to @schemes)
// For example: ["http", "https"]
Schemes []string `mapstructure:"schemes"`
// Contact information
Contact *ContactInfo `mapstructure:"contact"`
// License information
License *LicenseInfo `mapstructure:"license"`
// Security Definitions
SecurityDefinitions map[string]*SecurityDefinition `mapstructure:"security_definitions"`
}
SwaggerInfo API metadata Corresponds to swag's @title, @version annotations etc.
func DefaultSwaggerInfo ¶
func DefaultSwaggerInfo() SwaggerInfo
DefaultSwaggerInfo returns the default API documentation metadata