mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 15:22:14 +08:00
简单的数据库查询
This commit is contained in:
parent
d5f84ade1d
commit
fbb65eac73
@ -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)
|
||||
|
@ -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
|
||||
|
35
controller/user/user.go
Normal file
35
controller/user/user.go
Normal 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))
|
||||
}
|
88
docs/docs.go
88
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": {
|
||||
|
@ -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": {
|
||||
|
@ -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"
|
||||
|
6
main.go
6
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)
|
||||
|
28
services/user/user.go
Normal file
28
services/user/user.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user