refactor(response): 重构API响应结构体并优化配置管理
- 将Api结构体重命名为api并改为小写首字母 - 为Success、Error函数及SetMsg、SetData、SetCode、End方法添加详细注释 - 更新JSON字段名称从Json到JSON以符合Go命名规范 - 为配置相关函数SetDefault、LoadConfigFromFile、SetConfigValue等添加注释 - 在IDEA配置文件中更新结构体映射关系和最后修改时间戳 - 为配置结构体BaseConfig、ServerConfig等添加字段说明注释 - 将验证函数中的Bind方法替换为ShouldBind方法提升性能 - 为中间件ErrorHandler和CORSMiddleware函数添加功能注释
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
|
||||
Reference in New Issue
Block a user