66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
// Package openapi ...
|
|
//
|
|
// Description : openapi ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2026-01-06 22:48
|
|
package openapi
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"github.com/getkin/kin-openapi/openapi3"
|
|
)
|
|
|
|
// OptionFunc 设置文档选项
|
|
type OptionFunc func(t *openapi3.T)
|
|
|
|
// WithServers 设置文档服务器
|
|
func WithServers(serverList openapi3.Servers) OptionFunc {
|
|
return func(t *openapi3.T) {
|
|
if len(serverList) == 0 {
|
|
return
|
|
}
|
|
t.Servers = serverList
|
|
}
|
|
}
|
|
|
|
// WithInfo 文档基础信息
|
|
func WithInfo(info *openapi3.Info) OptionFunc {
|
|
return func(t *openapi3.T) {
|
|
if nil == info {
|
|
return
|
|
}
|
|
t.Info = info
|
|
}
|
|
}
|
|
|
|
// WithSecurity 设置安全策略
|
|
func WithSecurity(securityTable *openapi3.SecuritySchemes) OptionFunc {
|
|
return func(t *openapi3.T) {
|
|
if nil == securityTable {
|
|
return
|
|
}
|
|
if nil == t.Components {
|
|
t.Components = &openapi3.Components{}
|
|
}
|
|
if nil == t.Components.SecuritySchemes {
|
|
t.Components.SecuritySchemes = make(map[string]*openapi3.SecuritySchemeRef)
|
|
}
|
|
if nil == t.Security {
|
|
t.Security = make([]openapi3.SecurityRequirement, 0)
|
|
}
|
|
keyList := make([]string, 0)
|
|
for k, v := range *securityTable {
|
|
keyList = append(keyList, k)
|
|
t.Components.SecuritySchemes[k] = v
|
|
}
|
|
// 保证生成结果有序
|
|
sort.Strings(keyList)
|
|
for _, k := range keyList {
|
|
t.Security = append(t.Security, map[string][]string{k: {}})
|
|
}
|
|
}
|
|
}
|