简单的数据库查询

This commit is contained in:
Shikong 2022-10-04 14:59:46 +08:00
parent d5f84ade1d
commit fbb65eac73
8 changed files with 318 additions and 17 deletions

View File

@ -1,4 +1,4 @@
package global package app
import ( import (
"context" "context"
@ -10,35 +10,36 @@ import (
"gofiber.study.skcks.cn/common/logger" "gofiber.study.skcks.cn/common/logger"
"gofiber.study.skcks.cn/common/response" "gofiber.study.skcks.cn/common/response"
"gofiber.study.skcks.cn/controller" "gofiber.study.skcks.cn/controller"
"gofiber.study.skcks.cn/global"
"xorm.io/xorm" "xorm.io/xorm"
) )
var App *fiber.App var App *fiber.App
func LoadApp() { func LoadApp() {
err := viper.Unmarshal(Config) err := viper.Unmarshal(global.Config)
if err != nil { if err != nil {
logger.Log.Fatalf("配置文件解析失败: %s", err) logger.Log.Fatalf("配置文件解析失败: %s", err)
} }
mainAppExec(func() { mainAppExec(func() {
logger.Log.Infof("\n%#v", Config) logger.Log.Infof("\n%#v", global.Config)
}) })
reloadRedis(Config) reloadRedis(global.Config)
reloadDataSources(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) logger.Log.Fatalf("[x] [Fiber] 致命错误: %s", err)
} }
} }
func reloadRedis(c *config.BasicConfig) { func reloadRedis(c *config.BasicConfig) {
if Redis != nil { if global.Redis != nil {
_ = Redis.Close() _ = global.Redis.Close()
} }
r := redis.NewClient(&redis.Options{ r := redis.NewClient(&redis.Options{
@ -57,12 +58,12 @@ func reloadRedis(c *config.BasicConfig) {
logger.Log.Infoln("[√] [Redis] 连接成功") logger.Log.Infoln("[√] [Redis] 连接成功")
}) })
Redis = r global.Redis = r
} }
func reloadDataSources(c *config.BasicConfig) { func reloadDataSources(c *config.BasicConfig) {
if DataSources != nil { if global.DataSources != nil {
_ = DataSources.Close() _ = global.DataSources.Close()
} }
connGroup, err := xorm.NewEngineGroup("mysql", c.Mysql.DataSources) connGroup, err := xorm.NewEngineGroup("mysql", c.Mysql.DataSources)
@ -74,10 +75,10 @@ func reloadDataSources(c *config.BasicConfig) {
logger.Log.Infoln("[√] [数据源] 初始化完成") logger.Log.Infoln("[√] [数据源] 初始化完成")
}) })
DataSources = connGroup global.DataSources = connGroup
if c.Mysql.AutoSync && !fiber.IsChild() { if c.Mysql.AutoSync && !fiber.IsChild() {
SyncModelToDataSource() global.SyncModelToDataSource()
} }
} }
@ -112,6 +113,7 @@ func reloadApp(c *config.BasicConfig) *fiber.App {
} }
controller.SwaggerHandler(app) controller.SwaggerHandler(app)
controller.RegisterController(app)
controller.HelloWorld(app) controller.HelloWorld(app)
controller.ErrorHandler(app) controller.ErrorHandler(app)

View File

@ -6,8 +6,13 @@ import (
"gofiber.study.skcks.cn/common/errorx" "gofiber.study.skcks.cn/common/errorx"
"gofiber.study.skcks.cn/common/logger" "gofiber.study.skcks.cn/common/logger"
"gofiber.study.skcks.cn/common/response" "gofiber.study.skcks.cn/common/response"
"gofiber.study.skcks.cn/controller/user"
) )
func RegisterController(app *fiber.App) {
user.RegisterController(app)
}
// HelloWorld // HelloWorld
// //
// @Summary HelloWorld // @Summary HelloWorld

35
controller/user/user.go Normal file
View File

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

View File

@ -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": { "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": { "response.Response": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -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": { "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": { "response.Response": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -27,6 +27,31 @@ definitions:
description: Original registered route path description: Original registered route path
type: string type: string
type: object 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: response.Response:
properties: properties:
code: code:
@ -168,4 +193,34 @@ paths:
summary: 获取所有路由 summary: 获取所有路由
tags: tags:
- Routes - 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" swagger: "2.0"

View File

@ -3,8 +3,8 @@ package main
import ( import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/spf13/viper" "github.com/spf13/viper"
"gofiber.study.skcks.cn/app"
"gofiber.study.skcks.cn/common/logger" "gofiber.study.skcks.cn/common/logger"
"gofiber.study.skcks.cn/global"
"os" "os"
"os/signal" "os/signal"
@ -38,10 +38,10 @@ func main() {
viper.WatchConfig() viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) { viper.OnConfigChange(func(in fsnotify.Event) {
logger.Log.Infoln(in.Name) logger.Log.Infoln(in.Name)
global.LoadApp() app.LoadApp()
}) })
global.LoadApp() app.LoadApp()
quit := make(chan os.Signal) quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt) signal.Notify(quit, os.Interrupt)

28
services/user/user.go Normal file
View File

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