177 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			177 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Package router ...
 | 
						|
//
 | 
						|
// Description : router ...
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 2025-02-18 17:26
 | 
						|
package router
 | 
						|
 | 
						|
import (
 | 
						|
	"strings"
 | 
						|
 | 
						|
	apiDocDefine "git.zhangdeman.cn/zhangdeman/api-doc/define"
 | 
						|
	"git.zhangdeman.cn/zhangdeman/gin/middleware"
 | 
						|
	"github.com/gin-gonic/gin"
 | 
						|
)
 | 
						|
 | 
						|
type SetServerOptionFunc func(so *serverOption)
 | 
						|
 | 
						|
// serverOption 获取server实例的选项
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 17:28 2025/2/18
 | 
						|
type serverOption struct {
 | 
						|
	swaggerUiTheme       string                     // swagger 主题
 | 
						|
	swaggerBaseUri       string                     // swagger基础path
 | 
						|
	globalMiddlewareList []gin.HandlerFunc          // 全局中间件列表
 | 
						|
	disableSwaggerDoc    bool                       // 禁用swagger文档, 特定环境不想展示文档, 可通过次方式禁用
 | 
						|
	serverInfo           *apiDocDefine.Info         // 服务器信息
 | 
						|
	serverList           []*apiDocDefine.ServerItem // 服务器环境列表
 | 
						|
	enablePprof          bool                       // 启用pprof
 | 
						|
	enableCors           bool                       // 启动跨域支持
 | 
						|
	disableInitRequest   bool                       // 禁用初始化请求
 | 
						|
	loggerCfg            *middleware.AccessConfig   // 日志配置
 | 
						|
	initContextData      gin.HandlerFunc            // 初始化一些请求数据
 | 
						|
}
 | 
						|
 | 
						|
// WithDisableInitRequest 禁用自从初始化请求
 | 
						|
func WithDisableInitRequest(disable bool) SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		so.disableInitRequest = disable
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithInitContextData 初始化一些请求数据
 | 
						|
func WithInitContextData(formatFunc func(ctx *gin.Context) map[string]any) SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		if nil == formatFunc {
 | 
						|
			return
 | 
						|
		}
 | 
						|
		f := func(ctx *gin.Context) {
 | 
						|
			data := formatFunc(ctx)
 | 
						|
			for k, v := range data {
 | 
						|
				ctx.Set(k, v)
 | 
						|
			}
 | 
						|
		}
 | 
						|
		so.initContextData = f
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithSwaggerUITheme 设置swaggerUI主题
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 17:29 2025/2/18
 | 
						|
func WithSwaggerUITheme(uiTheme string) SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		uiTheme = strings.TrimSpace(uiTheme)
 | 
						|
		if len(uiTheme) == 0 {
 | 
						|
			return
 | 
						|
		}
 | 
						|
		so.swaggerUiTheme = uiTheme
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithGlobalMiddlewareList 设置全局中间件
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 17:33 2025/2/18
 | 
						|
func WithGlobalMiddlewareList(middlewareList ...gin.HandlerFunc) SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		so.globalMiddlewareList = middlewareList
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithSwaggerBaseUri ...
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 18:05 2025/2/18
 | 
						|
func WithSwaggerBaseUri(baseUri string) SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		baseUri = strings.TrimSpace(baseUri)
 | 
						|
		if len(baseUri) == 0 {
 | 
						|
			return
 | 
						|
		}
 | 
						|
		baseUri = "/" + strings.TrimLeft(baseUri, "/")
 | 
						|
		so.swaggerBaseUri = baseUri
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithDisableSwaggerDoc 禁用swagger文档
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 17:35 2025/2/18
 | 
						|
func WithDisableSwaggerDoc() SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		so.disableSwaggerDoc = true
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithServerInfo 设置serverInfo
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 17:51 2025/2/18
 | 
						|
func WithServerInfo(serverInfo *apiDocDefine.Info) SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		if nil == serverInfo {
 | 
						|
			return
 | 
						|
		}
 | 
						|
		so.serverInfo = serverInfo
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithServerList 设置服务器列表
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 17:52 2025/2/18
 | 
						|
func WithServerList(serverList []*apiDocDefine.ServerItem) SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		if len(serverList) == 0 {
 | 
						|
			return
 | 
						|
		}
 | 
						|
		so.serverList = serverList
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithPprofEnable 启用pprof
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 15:10 2025/2/21
 | 
						|
func WithPprofEnable() SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		so.enablePprof = true
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithEnableCors 启用全局跨域
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 14:56 2025/2/22
 | 
						|
func WithEnableCors() SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		so.enableCors = true
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// WithLoggerCfg ...
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 15:25 2025/2/22
 | 
						|
func WithLoggerCfg(loggerCfg *middleware.AccessConfig) SetServerOptionFunc {
 | 
						|
	return func(so *serverOption) {
 | 
						|
		if nil != loggerCfg || nil != loggerCfg.Logger {
 | 
						|
			so.loggerCfg = loggerCfg
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |