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函数添加功能注释
main
black1552 2026-02-25 15:25:32 +08:00
parent 60f0d8053d
commit 7304f59b61
6 changed files with 91 additions and 86 deletions

View File

@ -3,13 +3,6 @@
<component name="GoORMHelperCache">
<option name="schemaMapping">
<map>
<entry key="Api">
<value>
<set>
<option value="file://$PROJECT_DIR$/response/code.go" />
</set>
</value>
</entry>
<entry key="BaseConfig">
<value>
<set>
@ -91,7 +84,6 @@
<value>
<set>
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
<option value="file://$PROJECT_DIR$/../gin_test/req/new.go" />
</set>
</value>
</entry>
@ -99,7 +91,6 @@
<value>
<set>
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
<option value="file://$PROJECT_DIR$/../gin_test/req/new.go" />
</set>
</value>
</entry>
@ -145,6 +136,13 @@
</set>
</value>
</entry>
<entry key="api">
<value>
<set>
<option value="file://$PROJECT_DIR$/response/code.go" />
</set>
</value>
</entry>
<entry key="response">
<value>
<set>
@ -159,7 +157,7 @@
<entry key="file://$PROJECT_DIR$/config/fun.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770100625919" />
<option name="lastModified" value="1770427966184" />
</ScannedPath>
</value>
</entry>
@ -173,7 +171,7 @@
<entry key="file://$PROJECT_DIR$/config/structs.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770022381063" />
<option name="lastModified" value="1770444054990" />
<option name="schema">
<list>
<option value="BaseConfig" />
@ -188,7 +186,7 @@
<entry key="file://$PROJECT_DIR$/curd/curd.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770102564408" />
<option name="lastModified" value="1770362565302" />
<option name="schema">
<list>
<option value="Paginate" />
@ -236,14 +234,14 @@
<entry key="file://$PROJECT_DIR$/main.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770025551953" />
<option name="lastModified" value="1770362565303" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/middleware/middleware.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770108038945" />
<option name="lastModified" value="1770444203947" />
</ScannedPath>
</value>
</entry>
@ -262,11 +260,11 @@
<entry key="file://$PROJECT_DIR$/response/code.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770014431840" />
<option name="lastModified" value="1770444657704" />
<option name="schema">
<list>
<option value="response" />
<option value="Api" />
<option value="api" />
</list>
</option>
</ScannedPath>
@ -275,7 +273,7 @@
<entry key="file://$PROJECT_DIR$/server/server.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770026239951" />
<option name="lastModified" value="1770362565303" />
</ScannedPath>
</value>
</entry>
@ -342,7 +340,7 @@
<entry key="file://$PROJECT_DIR$/valid/valid.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770197737203" />
<option name="lastModified" value="1772004278917" />
</ScannedPath>
</value>
</entry>
@ -368,17 +366,10 @@
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/api/home.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770190095707" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/api/new.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770191206242" />
<option name="lastModified" value="1772003979851" />
<option name="schema">
<list>
<option value="NewsOne" />
@ -388,44 +379,17 @@
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/houeRouter.go">
<entry key="file://$PROJECT_DIR$/../gin_test/router/homeRouter.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770191001042" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/index.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770255253603" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/router.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770190557848" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/req/new.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770186504009" />
<option name="schema">
<list>
<option value="NewsOne" />
<option value="NewsSave" />
</list>
</option>
<option name="lastModified" value="1772003574712" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/router/router.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770191024510" />
<option name="lastModified" value="1772003574719" />
</ScannedPath>
</value>
</entry>
@ -433,7 +397,7 @@
</option>
<option name="tableStructMapping">
<map>
<entry key="api" value="Api" />
<entry key="api" value="api" />
<entry key="base_config" value="BaseConfig" />
<entry key="client" value="Client" />
<entry key="config" value="Config" />
@ -456,6 +420,6 @@
<entry key="tcp_server" value="TCPServer" />
</map>
</option>
<option name="lastTimeChecked" value="1770185677345" />
<option name="lastTimeChecked" value="1772003052939" />
</component>
</project>

View File

@ -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()
}

View File

@ -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"`

View File

@ -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()
}
}

View File

@ -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)
}

View File

@ -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 {