diff --git a/.idea/GOHCache.xml b/.idea/GOHCache.xml index 7aee180..51544e7 100644 --- a/.idea/GOHCache.xml +++ b/.idea/GOHCache.xml @@ -3,13 +3,6 @@ \ No newline at end of file diff --git a/config/fun.go b/config/fun.go index 4d5015b..c037e68 100644 --- a/config/fun.go +++ b/config/fun.go @@ -54,6 +54,7 @@ func init() { }) } +// SetDefault 设置默认配置信息 func SetDefault() { viper.Set("SERVER.addr", "127.0.0.1:8080") viper.Set("SERVER.mode", "release") @@ -63,6 +64,7 @@ func SetDefault() { viper.Set("JWT.expire", 86400) } +// LoadConfigFromFile 在配置文件中加载配置 func LoadConfigFromFile() error { err := viper.ReadInConfig() if err != nil { @@ -71,6 +73,7 @@ func LoadConfigFromFile() error { return nil } +// SetConfigValue 设置指定配置文件的值 func SetConfigValue(key string, value any) error { viper.SetDefault(key, value) err := viper.WriteConfig() @@ -80,6 +83,7 @@ func SetConfigValue(key string, value any) error { return nil } +// SetConfigMap 使用Map的方式添加配置信息 func SetConfigMap(value map[string]any) error { if len(value) == 0 { log.Error("value is empty") @@ -95,6 +99,7 @@ func SetConfigMap(value map[string]any) error { return nil } +// GetConfigValue 获取指定配置的值 func GetConfigValue(key string, def ...any) *gvar.Var { value := gvar.New(viper.Get(key)) if value.IsEmpty() && len(def) > 0 { @@ -102,12 +107,15 @@ func GetConfigValue(key string, def ...any) *gvar.Var { } return value } + +// Unmarshal 将配置解析成指定的对象 func Unmarshal[T any]() (*T, error) { var s T err := viper.Unmarshal(&s) return &s, err } +// GetAllConfig 获取所有配置信息并返回Map func GetAllConfig() map[string]any { return viper.AllSettings() } diff --git a/config/structs.go b/config/structs.go index 8e61de0..a2906d8 100644 --- a/config/structs.go +++ b/config/structs.go @@ -1,20 +1,25 @@ package config +// BaseConfig 基础配置信息 type BaseConfig struct { Server ServerConfig `mapstructure:"SERVER"` Database DataBaseConfig `mapstructure:"DATABASE"` Jwt JwtConfig `mapstructure:"JWT"` } + +// ServerConfig 服务配置 type ServerConfig struct { Addr string `mapstructure:"addr"` Mode string `mapstructure:"mode"` } +// DataBaseConfig 数据库配置 type DataBaseConfig struct { Dns string `mapstructure:"dns"` Type string `mapstructure:"type"` } +// JwtConfig JWT配置 type JwtConfig struct { Secret string `mapstructure:"secret"` Expire int64 `mapstructure:"expire"` diff --git a/middleware/middleware.go b/middleware/middleware.go index 1cc724d..9455c5d 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" ) +// ErrorHandler 全局异常处理中间件 func ErrorHandler() gin.HandlerFunc { return func(c *gin.Context) { defer func() { @@ -29,6 +30,8 @@ func ErrorHandler() gin.HandlerFunc { c.Next() } } + +// CORSMiddleware 跨域中间件 func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set("Access-Control-Allow-Origin", "*") @@ -40,7 +43,6 @@ func CORSMiddleware() gin.HandlerFunc { c.AbortWithStatus(204) return } - c.Next() } } diff --git a/response/code.go b/response/code.go index e3f8b79..bc403ae 100644 --- a/response/code.go +++ b/response/code.go @@ -13,45 +13,71 @@ type response struct { Timestamp int64 `json:"timestamp"` } -type Api struct { - Json *response +// api 返回结构体 +type api struct { + JSON *response c *gin.Context } -func Success(c *gin.Context) *Api { +// Success 函数用于创建一个包含成功响应的 api 对象。 +// 参数 c 是 Gin 框架的上下文对象,用于处理请求和响应。 +// 返回值是一个指向 API 结构体的指针,其中包含 JSON 响应数据。 +func Success(c *gin.Context) *api { + // 创建响应结构体,包含状态码和时间戳 + json := &response{ + Code: 200, // 成功的状态码 + Timestamp: time.Now().Unix(), // 当前时间的时间戳 + } + // 创建 api 结构体实例,并将响应数据和上下文对象赋值给它 + r := &api{ + JSON: json, // JSON 响应数据 + c: c, // Gin 上下文对象 + } + return r // 返回 api 对象的指针 +} + +// Error 函数用于创建一个包含错误响应的 api 对象。 +// 参数 c 是 Gin 框架的上下文对象,用于处理请求和响应。 +// 返回值是一个指向 api 结构体的指针,其中包含 JSON 响应数据。 +func Error(c *gin.Context) *api { json := &response{ Code: 200, Timestamp: time.Now().Unix(), } - r := &Api{ - Json: json, + r := &api{ + JSON: json, c: c, } return r } -func Error(c *gin.Context) *Api { - json := &response{ - Code: 200, - Timestamp: time.Now().Unix(), - } - r := &Api{ - Json: json, - c: c, - } + +// SetMsg 方法用于设置 api 对象的 JSON 响应数据中的 Msg 字段。 +// 参数 msg 是要设置的消息字符串。 +// 返回值是指向 api 结构体的指针,用于链式调用。 +func (r *api) SetMsg(msg string) *api { + r.JSON.Msg = msg return r } -func (r *Api) SetMsg(msg string) *Api { - r.Json.Msg = msg + +// SetData 方法用于设置 api 对象的 JSON 响应数据中的 Data 字段。 +// 参数 data 是要设置的数据,类型为 any(任意类型)。 +// 返回值是指向 api 结构体的指针,用于链式调用。 +func (r *api) SetData(data any) *api { + r.JSON.Data = data return r } -func (r *Api) SetData(data any) *Api { - r.Json.Data = data + +// SetCode 方法用于设置 api 对象的 JSON 响应数据中的 Code 字段。 +// 参数 code 是要设置的状态码,类型为 int。 +// 返回值是指向 api 结构体的指针,用于链式调用。 +func (r *api) SetCode(code int) *api { + r.JSON.Code = code return r } -func (r *Api) SetCode(code int) *Api { - r.Json.Code = code - return r -} -func (r *Api) End() { - r.c.JSON(r.Json.Code, r.Json) + +// End 方法用于将 api 对象的 JSON 响应数据发送给客户端。 +// 它使用 Gin 上下文对象的 JSON 方法将 JSON 响应数据发送给客户端。 +// 参数 r 是指向 api 结构体的指针,包含 JSON 响应数据和 Gin 上下文对象。 +func (r *api) End() { + r.c.JSON(r.JSON.Code, r.JSON) } diff --git a/valid/valid.go b/valid/valid.go index 1278f49..c180507 100644 --- a/valid/valid.go +++ b/valid/valid.go @@ -10,7 +10,7 @@ import ( // ValidToStruct 验证参数并返回结构体 func ValidToStruct[T any](c *gin.Context) (object *T) { obj := new(T) - if err := c.Bind(obj); err != nil { + if err := c.ShouldBind(obj); err != nil { panic(err) } if err := g.Validator().Data(obj).Run(c); err != nil { @@ -22,7 +22,7 @@ func ValidToStruct[T any](c *gin.Context) (object *T) { // ValidToMap 验证参数并返回结构体 func ValidToMap[T any](c *gin.Context) (object map[string]any) { obj := new(T) - if err := c.Bind(obj); err != nil { + if err := c.ShouldBind(obj); err != nil { panic(err) } if err := g.Validator().Data(obj).Run(c); err != nil { @@ -34,7 +34,7 @@ func ValidToMap[T any](c *gin.Context) (object map[string]any) { // ValidToStructAndMap 验证参数并返回map func ValidToStructAndMap[T any](c *gin.Context) (stru *T, object map[string]any) { obj := new(T) - if err := c.Bind(obj); err != nil { + if err := c.ShouldBind(obj); err != nil { panic(err) } if err := g.Validator().Data(obj).Run(c); err != nil {