diff --git a/global/app.go b/app/app.go similarity index 83% rename from global/app.go rename to app/app.go index fdec116..0836878 100644 --- a/global/app.go +++ b/app/app.go @@ -1,4 +1,4 @@ -package global +package app import ( "context" @@ -10,35 +10,36 @@ import ( "gofiber.study.skcks.cn/common/logger" "gofiber.study.skcks.cn/common/response" "gofiber.study.skcks.cn/controller" + "gofiber.study.skcks.cn/global" "xorm.io/xorm" ) var App *fiber.App func LoadApp() { - err := viper.Unmarshal(Config) + err := viper.Unmarshal(global.Config) if err != nil { logger.Log.Fatalf("配置文件解析失败: %s", err) } mainAppExec(func() { - logger.Log.Infof("\n%#v", Config) + logger.Log.Infof("\n%#v", global.Config) }) - reloadRedis(Config) - reloadDataSources(Config) + reloadRedis(global.Config) + reloadDataSources(global.Config) - App = reloadApp(Config) + App = reloadApp(global.Config) - if err := App.Listen(Config.Server.Addr); err != nil { + if err := App.Listen(global.Config.Server.Addr); err != nil { logger.Log.Fatalf("[x] [Fiber] 致命错误: %s", err) } } func reloadRedis(c *config.BasicConfig) { - if Redis != nil { - _ = Redis.Close() + if global.Redis != nil { + _ = global.Redis.Close() } r := redis.NewClient(&redis.Options{ @@ -57,12 +58,12 @@ func reloadRedis(c *config.BasicConfig) { logger.Log.Infoln("[√] [Redis] 连接成功") }) - Redis = r + global.Redis = r } func reloadDataSources(c *config.BasicConfig) { - if DataSources != nil { - _ = DataSources.Close() + if global.DataSources != nil { + _ = global.DataSources.Close() } connGroup, err := xorm.NewEngineGroup("mysql", c.Mysql.DataSources) @@ -74,10 +75,10 @@ func reloadDataSources(c *config.BasicConfig) { logger.Log.Infoln("[√] [数据源] 初始化完成") }) - DataSources = connGroup + global.DataSources = connGroup if c.Mysql.AutoSync && !fiber.IsChild() { - SyncModelToDataSource() + global.SyncModelToDataSource() } } @@ -112,6 +113,7 @@ func reloadApp(c *config.BasicConfig) *fiber.App { } controller.SwaggerHandler(app) + controller.RegisterController(app) controller.HelloWorld(app) controller.ErrorHandler(app) diff --git a/controller/controller.go b/controller/controller.go index aa14136..f38421c 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -6,8 +6,13 @@ import ( "gofiber.study.skcks.cn/common/errorx" "gofiber.study.skcks.cn/common/logger" "gofiber.study.skcks.cn/common/response" + "gofiber.study.skcks.cn/controller/user" ) +func RegisterController(app *fiber.App) { + user.RegisterController(app) +} + // HelloWorld // // @Summary HelloWorld diff --git a/controller/user/user.go b/controller/user/user.go new file mode 100644 index 0000000..b2edf19 --- /dev/null +++ b/controller/user/user.go @@ -0,0 +1,35 @@ +package user + +import ( + "github.com/gofiber/fiber/v2" + "gofiber.study.skcks.cn/common/errorx" + "gofiber.study.skcks.cn/common/response" + "gofiber.study.skcks.cn/services/user" +) + +func RegisterController(app *fiber.App) { + group := app.Group("/user") + group.Add(fiber.MethodGet, "/account", getByAccount) +} + +// getByAccount 根据 账号 获取用户信息 +// +// @Summary 根据 账号 获取用户信息 +// @Description 根据 账号 获取用户信息 +// @Tags User +// @Accept json +// @Produce json +// @Param account query string true "账号名称" +// @Success 200 {object} response.Response{data=models.User} +// @Failure default {object} errorx.CodeErrorResponse +// @Router /user/account [get] +func getByAccount(ctx *fiber.Ctx) error { + account := ctx.Query("account") + + u, err := user.GetUserByAccount(account) + if err != nil { + return ctx.JSON(errorx.NewDefaultError(err.Error())) + } + + return ctx.JSON(response.NewResponse(u)) +} diff --git a/docs/docs.go b/docs/docs.go index 1fbf313..ea7b391 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -212,6 +212,56 @@ const docTemplate = `{ } } } + }, + "/user/account": { + "get": { + "description": "根据 账号 获取用户信息", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "根据 账号 获取用户信息", + "parameters": [ + { + "type": "string", + "description": "账号名称", + "name": "account", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/models.User" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } } }, "definitions": { @@ -253,6 +303,44 @@ const docTemplate = `{ } } }, + "models.User": { + "type": "object", + "properties": { + "account": { + "type": "string" + }, + "active": { + "type": "boolean" + }, + "alias": { + "type": "string" + }, + "contact": { + "type": "string" + }, + "email": { + "type": "string" + }, + "emailVerify": { + "type": "boolean" + }, + "id": { + "type": "string" + }, + "lastLoginTime": { + "type": "string" + }, + "memo": { + "type": "string" + }, + "registerTime": { + "type": "string" + }, + "userName": { + "type": "string" + } + } + }, "response.Response": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 1df6bca..2af0a74 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -204,6 +204,56 @@ } } } + }, + "/user/account": { + "get": { + "description": "根据 账号 获取用户信息", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "根据 账号 获取用户信息", + "parameters": [ + { + "type": "string", + "description": "账号名称", + "name": "account", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/models.User" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } } }, "definitions": { @@ -245,6 +295,44 @@ } } }, + "models.User": { + "type": "object", + "properties": { + "account": { + "type": "string" + }, + "active": { + "type": "boolean" + }, + "alias": { + "type": "string" + }, + "contact": { + "type": "string" + }, + "email": { + "type": "string" + }, + "emailVerify": { + "type": "boolean" + }, + "id": { + "type": "string" + }, + "lastLoginTime": { + "type": "string" + }, + "memo": { + "type": "string" + }, + "registerTime": { + "type": "string" + }, + "userName": { + "type": "string" + } + } + }, "response.Response": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 2afa00e..5956700 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -27,6 +27,31 @@ definitions: description: Original registered route path type: string type: object + models.User: + properties: + account: + type: string + active: + type: boolean + alias: + type: string + contact: + type: string + email: + type: string + emailVerify: + type: boolean + id: + type: string + lastLoginTime: + type: string + memo: + type: string + registerTime: + type: string + userName: + type: string + type: object response.Response: properties: code: @@ -168,4 +193,34 @@ paths: summary: 获取所有路由 tags: - Routes + /user/account: + get: + consumes: + - application/json + description: 根据 账号 获取用户信息 + parameters: + - description: 账号名称 + in: query + name: account + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/response.Response' + - properties: + data: + $ref: '#/definitions/models.User' + type: object + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: 根据 账号 获取用户信息 + tags: + - User swagger: "2.0" diff --git a/main.go b/main.go index 861b9ae..717f7a9 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,8 @@ package main import ( "github.com/fsnotify/fsnotify" "github.com/spf13/viper" + "gofiber.study.skcks.cn/app" "gofiber.study.skcks.cn/common/logger" - "gofiber.study.skcks.cn/global" "os" "os/signal" @@ -38,10 +38,10 @@ func main() { viper.WatchConfig() viper.OnConfigChange(func(in fsnotify.Event) { logger.Log.Infoln(in.Name) - global.LoadApp() + app.LoadApp() }) - global.LoadApp() + app.LoadApp() quit := make(chan os.Signal) signal.Notify(quit, os.Interrupt) diff --git a/services/user/user.go b/services/user/user.go new file mode 100644 index 0000000..f4241a0 --- /dev/null +++ b/services/user/user.go @@ -0,0 +1,28 @@ +package user + +import ( + "errors" + "gofiber.study.skcks.cn/global" + "gofiber.study.skcks.cn/model/user/models" +) + +var ( + NotExists = errors.New("用户不存在") +) + +func GetUserByAccount(account string) (*models.User, error) { + u := &models.User{ + Account: account, + } + + has, err := global.DataSources.Get(u) + if err != nil { + return nil, err + } + + if !has { + return nil, NotExists + } + + return u, nil +}