Files

99 lines
2.7 KiB
Go
Raw Permalink Normal View History

package middleware
import (
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
2025-10-27 23:12:15 +08:00
// ============================================================================
// 结构体定义
// ============================================================================
// DevModeConfig 开发模式配置
type DevModeConfig struct {
// 是否跳过验证码验证
SkipCaptcha bool
// 是否显示详细错误信息
ShowDetailedErrors bool
// 是否启用调试日志
EnableDebugLog bool
}
2025-10-27 23:12:15 +08:00
// ============================================================================
// 中间件函数
// ============================================================================
// DevModeMiddleware 开发模式中间件
// 统一管理所有开发模式相关的功能
2026-03-28 23:30:02 +08:00
func DevModeMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// 检查是否为开发模式
if IsDevMode() {
// 设置开发模式标识到上下文
c.Set("dev_mode", true)
c.Set("dev_config", GetDevModeConfig())
// 设置开发模式相关的响应头
c.Header("X-Dev-Mode", "true")
} else {
c.Set("dev_mode", false)
}
c.Next()
}
}
2025-10-27 23:12:15 +08:00
// ============================================================================
// 公共函数
// ============================================================================
// IsDevMode 检查是否为开发模式
func IsDevMode() bool {
return viper.GetBool("server.dev_mode")
}
// GetDevModeConfig 获取开发模式配置
func GetDevModeConfig() DevModeConfig {
if !IsDevMode() {
return DevModeConfig{}
}
return DevModeConfig{
2026-03-28 23:30:02 +08:00
SkipCaptcha: true, // 开发模式下默认跳过验证码
ShowDetailedErrors: true, // 开发模式下显示详细错误
EnableDebugLog: true, // 开发模式下启用调试日志
}
}
// IsDevModeFromContext 从上下文中检查是否为开发模式
func IsDevModeFromContext(c *gin.Context) bool {
if devMode, exists := c.Get("dev_mode"); exists {
if isDevMode, ok := devMode.(bool); ok {
return isDevMode
}
}
// 回退到配置检查
return IsDevMode()
}
// GetDevModeConfigFromContext 从上下文中获取开发模式配置
func GetDevModeConfigFromContext(c *gin.Context) DevModeConfig {
if config, exists := c.Get("dev_config"); exists {
if devConfig, ok := config.(DevModeConfig); ok {
return devConfig
}
}
// 回退到默认配置
return GetDevModeConfig()
}
// ShouldSkipCaptcha 检查是否应该跳过验证码验证
func ShouldSkipCaptcha(c *gin.Context) bool {
config := GetDevModeConfigFromContext(c)
return config.SkipCaptcha
}
2025-10-27 23:12:15 +08:00
// ============================================================================
// 私有函数
// ============================================================================