swagger

package
v0.25.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CDN 资源文件路径
	CDNPathCSS       = "/swagger-ui.css"
	CDNPathBundleJS  = "/swagger-ui-bundle.js"
	CDNPathPresetJS  = "/swagger-ui-standalone-preset.js"
	CDNPathFavicon32 = "/favicon-32x32.png"
	CDNPathFavicon16 = "/favicon-16x16.png"

	// CDN 基础 URL 模板
	CDNBaseURLUnpkg    = "https://unpkg.com/swagger-ui-dist@"
	CDNBaseURLJsDelivr = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@"
)

CDN 资源路径常量

Variables

This section is empty.

Functions

This section is empty.

Types

type AggregateConfig

type AggregateConfig struct {
	Enabled                  bool            `mapstructure:"enabled" yaml:"enabled" json:"enabled"`                                                        // 是否启用聚合
	Mode                     string          `mapstructure:"mode" yaml:"mode" json:"mode"`                                                                 // 聚合模式: merge|selector
	Services                 []*ServiceSpec  `mapstructure:"services" yaml:"services" json:"services"`                                                     // 微服务列表
	Documents                []*DocumentSpec `mapstructure:"documents" yaml:"documents" json:"documents"`                                                  // 独立文档列表
	UILayout                 string          `mapstructure:"ui-layout" yaml:"ui-layout" json:"uiLayout"`                                                   // UI布局: tabs|dropdown|list
	SharedDefinitionPrefixes []string        `mapstructure:"shared-definition-prefixes" yaml:"shared-definition-prefixes" json:"sharedDefinitionPrefixes"` // 共享定义前缀列表(不添加服务名前缀)
}

AggregateConfig 聚合Swagger配置

type AuthConfig

type AuthConfig struct {
	Enabled     bool     `mapstructure:"enabled" yaml:"enabled" json:"enabled"`               // 是否启用认证
	Type        AuthType `mapstructure:"type" yaml:"type" json:"type"`                        // 认证类型
	Username    string   `mapstructure:"username" yaml:"username" json:"username"`            // 用户名(基本认证)
	Password    string   `mapstructure:"password" yaml:"password" json:"password"`            // 密码(基本认证)
	Token       string   `mapstructure:"token" yaml:"token" json:"token"`                     // Token(Bearer认证)
	HeaderName  string   `mapstructure:"header-name" yaml:"header-name" json:"headerName"`    // 自定义header名称
	HeaderValue string   `mapstructure:"header-value" yaml:"header-value" json:"headerValue"` // 自定义header值
}

AuthConfig Swagger认证配置

type AuthType

type AuthType string

AuthType Swagger认证类型

const (
	// AuthNone 无认证
	AuthNone AuthType = "NONE"
	// AuthBasic 基本认证
	AuthBasic AuthType = "BASIC"
	// AuthBearer Bearer Token认证
	AuthBearer AuthType = "BEARER"
	// AuthCustom 自定义认证
	AuthCustom AuthType = "CUSTOM"
)

type Contact

type Contact struct {
	Name  string `mapstructure:"name" yaml:"name" json:"name"`    // 联系人姓名
	URL   string `mapstructure:"url" yaml:"url" json:"url"`       // 联系URL
	Email string `mapstructure:"email" yaml:"email" json:"email"` // 联系邮箱
}

Contact Swagger联系信息

type DocumentPathSelector

type DocumentPathSelector struct {
	Path    string   `mapstructure:"path" yaml:"path" json:"path"`          // Swagger路径,如 /v1/open-platform/apps
	Methods []string `mapstructure:"methods" yaml:"methods" json:"methods"` // 可选的HTTP方法列表,留空表示该路径下全部方法
}

DocumentPathSelector 独立文档中的接口选择器

type DocumentSource

type DocumentSource struct {
	Service string                  `mapstructure:"service" yaml:"service" json:"service"` // 来源服务名,对应 aggregate.services[].name
	Paths   []*DocumentPathSelector `mapstructure:"paths" yaml:"paths" json:"paths"`       // include 的兼容简写,留空表示该服务全部路径
	Include []*DocumentPathSelector `mapstructure:"include" yaml:"include" json:"include"` // 显式包含的路径列表,留空表示该服务全部路径
	Exclude []*DocumentPathSelector `mapstructure:"exclude" yaml:"exclude" json:"exclude"` // 需要排除的路径列表
}

DocumentSource 独立文档的数据源

func NewDocumentSource

func NewDocumentSource(service string) *DocumentSource

NewDocumentSource 创建新的独立文档来源配置

func (*DocumentSource) AddExcludePath

func (d *DocumentSource) AddExcludePath(path string, methods ...string) *DocumentSource

AddExcludePath 添加排除路径选择器

func (*DocumentSource) AddIncludePath

func (d *DocumentSource) AddIncludePath(path string, methods ...string) *DocumentSource

AddIncludePath 添加包含路径选择器

func (*DocumentSource) GetEffectiveInclude

func (d *DocumentSource) GetEffectiveInclude() []*DocumentPathSelector

GetEffectiveInclude 获取生效的 include 选择器,优先使用 include,其次兼容 paths

type DocumentSpec

type DocumentSpec struct {
	Name        string            `mapstructure:"name" yaml:"name" json:"name"`                      // 文档名称,同时用于路由标识
	Title       string            `mapstructure:"title" yaml:"title" json:"title"`                   // 文档标题
	Description string            `mapstructure:"description" yaml:"description" json:"description"` // 文档描述
	Version     string            `mapstructure:"version" yaml:"version" json:"version"`             // 文档版本
	Enabled     bool              `mapstructure:"enabled" yaml:"enabled" json:"enabled"`             // 是否启用
	Sources     []*DocumentSource `mapstructure:"sources" yaml:"sources" json:"sources"`             // 文档来源
}

DocumentSpec 独立Swagger文档配置

func NewDocumentSpec

func NewDocumentSpec(name string) *DocumentSpec

NewDocumentSpec 创建新的独立Swagger文档配置

func (*DocumentSpec) AddSource

func (d *DocumentSpec) AddSource(source *DocumentSource) *DocumentSpec

AddSource 添加文档来源

func (*DocumentSpec) Disable

func (d *DocumentSpec) Disable() *DocumentSpec

Disable 禁用独立文档

func (*DocumentSpec) Enable

func (d *DocumentSpec) Enable() *DocumentSpec

Enable 启用独立文档

func (*DocumentSpec) WithDescription

func (d *DocumentSpec) WithDescription(description string) *DocumentSpec

WithDescription 设置文档描述

func (*DocumentSpec) WithTitle

func (d *DocumentSpec) WithTitle(title string) *DocumentSpec

WithTitle 设置文档标题

func (*DocumentSpec) WithVersion

func (d *DocumentSpec) WithVersion(version string) *DocumentSpec

WithVersion 设置文档版本

type License

type License struct {
	Name string `mapstructure:"name" yaml:"name" json:"name"` // 许可证名称
	URL  string `mapstructure:"url" yaml:"url" json:"url"`    // 许可证URL
}

License Swagger许可证信息

type ServiceSpec

type ServiceSpec struct {
	Name        string   `mapstructure:"name" yaml:"name" json:"name"`                      // 服务名称
	Description string   `mapstructure:"description" yaml:"description" json:"description"` // 服务描述
	SpecPath    string   `mapstructure:"spec-path" yaml:"spec-path" json:"specPath"`        // Swagger规范文件路径
	URL         string   `mapstructure:"url" yaml:"url" json:"url"`                         // 远程Swagger文档URL
	Version     string   `mapstructure:"version" yaml:"version" json:"version"`             // 服务版本
	Enabled     bool     `mapstructure:"enabled" yaml:"enabled" json:"enabled"`             // 是否启用
	BasePath    string   `mapstructure:"base-path" yaml:"base-path" json:"basePath"`        // API基础路径前缀
	Tags        []string `mapstructure:"tags" yaml:"tags" json:"tags"`                      // 服务标签
}

ServiceSpec 单个微服务Swagger规范配置

func NewRemoteServiceSpec

func NewRemoteServiceSpec(name, description, url string) *ServiceSpec

NewRemoteServiceSpec 创建远程服务规范配置

func NewServiceSpec

func NewServiceSpec(name, description, specPath string) *ServiceSpec

NewServiceSpec 创建新的服务规范配置

func (*ServiceSpec) AddTag

func (s *ServiceSpec) AddTag(tag string) *ServiceSpec

AddTag 添加单个标签

func (*ServiceSpec) Disable

func (s *ServiceSpec) Disable() *ServiceSpec

Disable 禁用服务

func (*ServiceSpec) Enable

func (s *ServiceSpec) Enable() *ServiceSpec

Enable 启用服务

func (*ServiceSpec) WithBasePath

func (s *ServiceSpec) WithBasePath(basePath string) *ServiceSpec

WithBasePath 设置API基础路径

func (*ServiceSpec) WithDescription

func (s *ServiceSpec) WithDescription(description string) *ServiceSpec

WithDescription 设置服务描述

func (*ServiceSpec) WithName

func (s *ServiceSpec) WithName(name string) *ServiceSpec

WithName 设置服务名称

func (*ServiceSpec) WithTags

func (s *ServiceSpec) WithTags(tags []string) *ServiceSpec

WithTags 设置服务标签

func (*ServiceSpec) WithVersion

func (s *ServiceSpec) WithVersion(version string) *ServiceSpec

WithVersion 设置服务版本

type Swagger

type Swagger struct {
	ModuleName  string           `mapstructure:"module-name" yaml:"module-name" json:"moduleName"`  // 模块名称
	Enabled     bool             `mapstructure:"enabled" yaml:"enabled" json:"enabled"`             // 是否启用Swagger
	JSONPath    string           `mapstructure:"json-path" yaml:"json-path" json:"jsonPath"`        // Swagger JSON文件路径
	UIPath      string           `mapstructure:"ui-path" yaml:"ui-path" json:"uiPath"`              // Swagger UI路由路径
	YamlPath    string           `mapstructure:"yaml-path" yaml:"yaml-path" json:"yamlPath"`        // Swagger YAML文件路径
	SpecPath    string           `mapstructure:"spec-path" yaml:"spec-path" json:"specPath"`        // Swagger规范文件路径(自动检测格式)
	Title       string           `mapstructure:"title" yaml:"title" json:"title"`                   // 文档标题
	Description string           `mapstructure:"description" yaml:"description" json:"description"` // 文档描述
	Version     string           `mapstructure:"version" yaml:"version" json:"version"`             // Swagger版本
	Contact     *Contact         `mapstructure:"contact" yaml:"contact" json:"contact"`             // 联系信息
	License     *License         `mapstructure:"license" yaml:"license" json:"license"`             // 许可证信息
	Auth        *AuthConfig      `mapstructure:"auth" yaml:"auth" json:"auth"`                      // 认证配置
	Aggregate   *AggregateConfig `mapstructure:"aggregate" yaml:"aggregate" json:"aggregate"`       // 聚合配置
	HotReload   bool             `mapstructure:"hot-reload" yaml:"hot-reload" json:"hotReload"`     // 是否启用文件热重载

	// CDN 资源配置
	CDNVersion     string `mapstructure:"cdn-version" yaml:"cdn-version" json:"cdnVersion"`               // Swagger UI CDN版本
	CDNBaseURL     string `mapstructure:"cdn-base-url" yaml:"cdn-base-url" json:"cdnBaseURL"`             // CDN基础URL
	CDNCSSURL      string `mapstructure:"cdn-css-url" yaml:"cdn-css-url" json:"cdnCSSURL"`                // CSS文件URL
	CDNBundleJS    string `mapstructure:"cdn-bundle-js" yaml:"cdn-bundle-js" json:"cdnBundleJS"`          // Bundle JS URL
	CDNPresetJS    string `mapstructure:"cdn-preset-js" yaml:"cdn-preset-js" json:"cdnPresetJS"`          // Preset JS URL
	CDNFavicon32   string `mapstructure:"cdn-favicon32" yaml:"cdn-favicon32" json:"cdnFavicon32"`         // 32x32 Favicon URL
	CDNFavicon16   string `mapstructure:"cdn-favicon16" yaml:"cdn-favicon16" json:"cdnFavicon16"`         // 16x16 Favicon URL
	UseCDN         bool   `mapstructure:"use-cdn" yaml:"use-cdn" json:"useCDN"`                           // 是否使用CDN资源
	CDNFallbackURL string `mapstructure:"cdn-fallback-url" yaml:"cdn-fallback-url" json:"cdnFallbackURL"` // CDN备用URL
}

Swagger Swagger配置结构

func Default

func Default() *Swagger

Default 创建默认Swagger配置

func (*Swagger) AddAggregateDocument

func (c *Swagger) AddAggregateDocument(document *DocumentSpec) *Swagger

AddAggregateDocument 添加单个独立文档

func (*Swagger) AddAggregateService

func (c *Swagger) AddAggregateService(service *ServiceSpec) *Swagger

AddAggregateService 添加单个聚合服务

func (*Swagger) AddSharedDefinitionPrefix

func (c *Swagger) AddSharedDefinitionPrefix(prefix string) *Swagger

AddSharedDefinitionPrefix 添加单个共享定义前缀

func (*Swagger) Clone

func (c *Swagger) Clone() internal.Configurable

Clone 克隆配置对象

func (*Swagger) Disable

func (c *Swagger) Disable() *Swagger

Disable 禁用Swagger

func (*Swagger) DisableAggregate

func (c *Swagger) DisableAggregate() *Swagger

DisableAggregate 禁用聚合功能

func (*Swagger) DisableCDN

func (c *Swagger) DisableCDN() *Swagger

DisableCDN 禁用CDN资源(使用本地资源)

func (*Swagger) Enable

func (c *Swagger) Enable() *Swagger

Enable 启用Swagger

func (*Swagger) EnableAggregate

func (c *Swagger) EnableAggregate() *Swagger

EnableAggregate 启用聚合功能

func (*Swagger) EnableCDN

func (c *Swagger) EnableCDN() *Swagger

EnableCDN 启用CDN资源

func (*Swagger) Get

func (c *Swagger) Get() interface{}

Get 返回 Swagger 配置的所有字段

func (*Swagger) GetAuthType

func (c *Swagger) GetAuthType() AuthType

GetAuthType 获取认证类型

func (*Swagger) GetCDNBundleJS

func (c *Swagger) GetCDNBundleJS() string

GetCDNBundleJS 获取Bundle JS URL(支持备用)

func (*Swagger) GetCDNCSSURL

func (c *Swagger) GetCDNCSSURL() string

GetCDNCSSURL 获取CSS URL(支持备用)

func (*Swagger) GetCDNFavicon16

func (c *Swagger) GetCDNFavicon16() string

GetCDNFavicon16 获取16x16 Favicon URL(支持备用)

func (*Swagger) GetCDNFavicon32

func (c *Swagger) GetCDNFavicon32() string

GetCDNFavicon32 获取32x32 Favicon URL(支持备用)

func (*Swagger) GetCDNPresetJS

func (c *Swagger) GetCDNPresetJS() string

GetCDNPresetJS 获取Preset JS URL(支持备用)

func (*Swagger) GetFullJSONPath

func (c *Swagger) GetFullJSONPath() string

GetFullJSONPath 获取完整的JSON路径

func (*Swagger) GetFullUIPath

func (c *Swagger) GetFullUIPath() string

GetFullUIPath 获取完整的UI路径

func (*Swagger) GetSharedDefinitionPrefixes

func (c *Swagger) GetSharedDefinitionPrefixes() []string

GetSharedDefinitionPrefixes 获取共享定义前缀列表

func (*Swagger) IsAggregateEnabled

func (c *Swagger) IsAggregateEnabled() bool

IsAggregateEnabled 检查聚合功能是否启用

func (*Swagger) IsAuthEnabled

func (c *Swagger) IsAuthEnabled() bool

IsAuthEnabled 检查是否启用了认证

func (*Swagger) IsEnabled

func (c *Swagger) IsEnabled() bool

IsEnabled 检查Swagger是否启用

func (*Swagger) Reset

func (c *Swagger) Reset() *Swagger

Reset 重置为默认配置

func (*Swagger) Set

func (c *Swagger) Set(data interface{})

Set 更新 Swagger 配置的字段

func (*Swagger) Validate

func (c *Swagger) Validate() error

Validate 检查 Swagger 配置的有效性

func (*Swagger) ValidateAuth

func (c *Swagger) ValidateAuth() error

ValidateAuth 验证认证配置

func (*Swagger) WithAggregate

func (c *Swagger) WithAggregate(aggregate *AggregateConfig) *Swagger

WithAggregate 设置聚合配置

func (*Swagger) WithAggregateDocuments

func (c *Swagger) WithAggregateDocuments(documents []*DocumentSpec) *Swagger

WithAggregateDocuments 设置独立文档列表

func (*Swagger) WithAggregateMode

func (c *Swagger) WithAggregateMode(mode string) *Swagger

WithAggregateMode 设置聚合模式

func (*Swagger) WithAggregateServices

func (c *Swagger) WithAggregateServices(services []*ServiceSpec) *Swagger

WithAggregateServices 设置聚合服务列表

func (*Swagger) WithAggregateUILayout

func (c *Swagger) WithAggregateUILayout(layout string) *Swagger

WithAggregateUILayout 设置UI布局

func (*Swagger) WithAuth

func (c *Swagger) WithAuth(auth *AuthConfig) *Swagger

WithAuth 设置认证配置

func (*Swagger) WithBasePath

func (c *Swagger) WithBasePath(basePath string) *Swagger

WithBasePath 设置基础路径(同时设置JSON和UI路径)

func (*Swagger) WithBasicAuth

func (c *Swagger) WithBasicAuth(username, password string) *Swagger

WithBasicAuth 设置基本认证

func (*Swagger) WithBearerAuth

func (c *Swagger) WithBearerAuth(token string) *Swagger

WithBearerAuth 设置Bearer Token认证

func (*Swagger) WithCDNBaseURL

func (c *Swagger) WithCDNBaseURL(baseURL string) *Swagger

WithCDNBaseURL 设置CDN基础URL

func (*Swagger) WithCDNFallbackURL

func (c *Swagger) WithCDNFallbackURL(fallbackURL string) *Swagger

WithCDNFallbackURL 设置CDN备用URL

func (*Swagger) WithCDNVersion

func (c *Swagger) WithCDNVersion(version string) *Swagger

WithCDNVersion 设置CDN版本

func (*Swagger) WithContact

func (c *Swagger) WithContact(contact *Contact) *Swagger

WithContact 设置联系信息

func (*Swagger) WithCustomCDNURLs

func (c *Swagger) WithCustomCDNURLs(cssURL, bundleJS, presetJS, favicon32, favicon16 string) *Swagger

WithCustomCDNURLs 自定义CDN资源URLs

func (*Swagger) WithCustomPaths

func (c *Swagger) WithCustomPaths(jsonPath, uiPath string) *Swagger

WithCustomPaths 自定义JSON和UI路径

func (*Swagger) WithDefaults

func (c *Swagger) WithDefaults() *Swagger

WithDefaults 使用默认配置填充未设置的字段

func (*Swagger) WithDescription

func (c *Swagger) WithDescription(description string) *Swagger

WithDescription 设置文档描述

func (*Swagger) WithDocumentInfo

func (c *Swagger) WithDocumentInfo(title, description string) *Swagger

WithDocumentInfo 设置文档信息(标题和描述)

func (*Swagger) WithEnabled

func (c *Swagger) WithEnabled(enabled bool) *Swagger

WithEnabled 设置是否启用Swagger

func (*Swagger) WithJSONPath

func (c *Swagger) WithJSONPath(path string) *Swagger

WithJSONPath 设置Swagger JSON文件路径

func (*Swagger) WithLicense

func (c *Swagger) WithLicense(license *License) *Swagger

WithLicense 设置许可证信息

func (*Swagger) WithModuleName

func (c *Swagger) WithModuleName(moduleName string) *Swagger

WithModuleName 设置模块名称

func (*Swagger) WithSharedDefinitionPrefixes

func (c *Swagger) WithSharedDefinitionPrefixes(prefixes []string) *Swagger

WithSharedDefinitionPrefixes 设置共享定义前缀列表

func (*Swagger) WithSpecPath

func (c *Swagger) WithSpecPath(path string) *Swagger

WithSpecPath 设置Swagger规范文件路径

func (*Swagger) WithTitle

func (c *Swagger) WithTitle(title string) *Swagger

WithTitle 设置文档标题

func (*Swagger) WithUIPath

func (c *Swagger) WithUIPath(path string) *Swagger

WithUIPath 设置Swagger UI路由路径

func (*Swagger) WithVersion

func (c *Swagger) WithVersion(version string) *Swagger

WithVersion 设置Swagger版本

func (*Swagger) WithYamlPath

func (c *Swagger) WithYamlPath(path string) *Swagger

WithYamlPath 设置Swagger YAML文件路径

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL