mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 23:32:15 +08:00
swag 添加 SecurityDefinitions 定义
This commit is contained in:
parent
45cb7c1c66
commit
42274bffd1
65
app/app.go
65
app/app.go
@ -1,18 +1,14 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/goccy/go-json"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/spf13/viper"
|
||||
"gofiber.study.skcks.cn/common/config"
|
||||
"gofiber.study.skcks.cn/common/errorx"
|
||||
"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
|
||||
@ -24,7 +20,7 @@ func Run() {
|
||||
}
|
||||
|
||||
mainAppExec(func() {
|
||||
logger.Log.Infof("\n%#v", global.Config)
|
||||
logger.Log.Debugf("\n%#v", global.Config)
|
||||
})
|
||||
|
||||
reloadRedis(global.Config)
|
||||
@ -38,51 +34,6 @@ func Run() {
|
||||
|
||||
}
|
||||
|
||||
func reloadRedis(c *config.BasicConfig) {
|
||||
if global.Redis != nil {
|
||||
_ = global.Redis.Close()
|
||||
}
|
||||
|
||||
r := redis.NewClient(&redis.Options{
|
||||
Addr: c.Redis.Addr,
|
||||
Password: c.Redis.Pass,
|
||||
DB: c.Redis.DB,
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
_, err := r.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
logger.Log.Fatalln("[x] [Redis] 连接失败: %s", err)
|
||||
}
|
||||
|
||||
mainAppExec(func() {
|
||||
logger.Log.Infoln("[√] [Redis] 连接成功")
|
||||
})
|
||||
|
||||
global.Redis = r
|
||||
}
|
||||
|
||||
func reloadDataSources(c *config.BasicConfig) {
|
||||
if global.DataSources != nil {
|
||||
_ = global.DataSources.Close()
|
||||
}
|
||||
|
||||
connGroup, err := xorm.NewEngineGroup("mysql", c.Mysql.DataSources)
|
||||
if err != nil {
|
||||
logger.Log.Fatalf("[x] [数据源] 致命错误: %s", err)
|
||||
}
|
||||
|
||||
mainAppExec(func() {
|
||||
logger.Log.Infoln("[√] [数据源] 初始化完成")
|
||||
})
|
||||
|
||||
global.DataSources = connGroup
|
||||
|
||||
if c.Mysql.AutoSync && !fiber.IsChild() {
|
||||
global.SyncModelToDataSource()
|
||||
}
|
||||
}
|
||||
|
||||
func reloadApp(c *config.BasicConfig) *fiber.App {
|
||||
if App != nil {
|
||||
_ = App.Shutdown()
|
||||
@ -136,17 +87,3 @@ func mainAppExec(fn func()) {
|
||||
fn()
|
||||
}
|
||||
}
|
||||
|
||||
// routes
|
||||
// @Summary 获取所有路由
|
||||
// @Description 获取所有路由信息
|
||||
// @Tags Routes
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.Response{data=[][]fiber.Route}
|
||||
// @Router /routes [get]
|
||||
func routes(app *fiber.App) {
|
||||
app.Get("/routes", func(ctx *fiber.Ctx) error {
|
||||
return ctx.JSON(response.NewResponse(app.Stack()))
|
||||
})
|
||||
}
|
||||
|
30
app/datasources.go
Normal file
30
app/datasources.go
Normal file
@ -0,0 +1,30 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gofiber.study.skcks.cn/common/config"
|
||||
"gofiber.study.skcks.cn/common/logger"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func reloadDataSources(c *config.BasicConfig) {
|
||||
if global.DataSources != nil {
|
||||
_ = global.DataSources.Close()
|
||||
}
|
||||
|
||||
connGroup, err := xorm.NewEngineGroup("mysql", c.Mysql.DataSources)
|
||||
if err != nil {
|
||||
logger.Log.Fatalf("[x] [数据源] 致命错误: %s", err)
|
||||
}
|
||||
|
||||
mainAppExec(func() {
|
||||
logger.Log.Infoln("[√] [数据源] 初始化完成")
|
||||
})
|
||||
|
||||
global.DataSources = connGroup
|
||||
|
||||
if c.Mysql.AutoSync && !fiber.IsChild() {
|
||||
global.SyncModelToDataSource()
|
||||
}
|
||||
}
|
33
app/redis.go
Normal file
33
app/redis.go
Normal file
@ -0,0 +1,33 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"gofiber.study.skcks.cn/common/config"
|
||||
"gofiber.study.skcks.cn/common/logger"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
)
|
||||
|
||||
func reloadRedis(c *config.BasicConfig) {
|
||||
if global.Redis != nil {
|
||||
_ = global.Redis.Close()
|
||||
}
|
||||
|
||||
r := redis.NewClient(&redis.Options{
|
||||
Addr: c.Redis.Addr,
|
||||
Password: c.Redis.Pass,
|
||||
DB: c.Redis.DB,
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
_, err := r.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
logger.Log.Fatalln("[x] [Redis] 连接失败: %s", err)
|
||||
}
|
||||
|
||||
mainAppExec(func() {
|
||||
logger.Log.Infoln("[√] [Redis] 连接成功")
|
||||
})
|
||||
|
||||
global.Redis = r
|
||||
}
|
20
app/routes.go
Normal file
20
app/routes.go
Normal file
@ -0,0 +1,20 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gofiber.study.skcks.cn/common/response"
|
||||
)
|
||||
|
||||
// routes
|
||||
// @Summary 获取所有路由
|
||||
// @Description 获取所有路由信息
|
||||
// @Tags Routes
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.Response{data=[][]fiber.Route}
|
||||
// @Router /routes [get]
|
||||
func routes(app *fiber.App) {
|
||||
app.Get("/routes", func(ctx *fiber.Ctx) error {
|
||||
return ctx.JSON(response.NewResponse(app.Stack()))
|
||||
})
|
||||
}
|
@ -23,6 +23,7 @@ func RegisterController(app *fiber.App) {
|
||||
// @Success 200 {object} response.Response{data=models.User}
|
||||
// @Failure default {object} errorx.CodeErrorResponse
|
||||
// @Router /user/account [get]
|
||||
// @Security Token
|
||||
func getByAccount(ctx *fiber.Ctx) error {
|
||||
account := ctx.Query("account")
|
||||
|
||||
|
12
docs/docs.go
12
docs/docs.go
@ -215,6 +215,11 @@ const docTemplate = `{
|
||||
},
|
||||
"/user/account": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"Token": []
|
||||
}
|
||||
],
|
||||
"description": "根据 账号 获取用户信息",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -355,6 +360,13 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"securityDefinitions": {
|
||||
"Token": {
|
||||
"type": "apiKey",
|
||||
"name": "token",
|
||||
"in": "header"
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
|
@ -207,6 +207,11 @@
|
||||
},
|
||||
"/user/account": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"Token": []
|
||||
}
|
||||
],
|
||||
"description": "根据 账号 获取用户信息",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
@ -347,5 +352,12 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"securityDefinitions": {
|
||||
"Token": {
|
||||
"type": "apiKey",
|
||||
"name": "token",
|
||||
"in": "header"
|
||||
}
|
||||
}
|
||||
}
|
@ -220,7 +220,14 @@ paths:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/errorx.CodeErrorResponse'
|
||||
security:
|
||||
- Token: []
|
||||
summary: 根据 账号 获取用户信息
|
||||
tags:
|
||||
- User
|
||||
securityDefinitions:
|
||||
Token:
|
||||
in: header
|
||||
name: token
|
||||
type: apiKey
|
||||
swagger: "2.0"
|
||||
|
Loading…
Reference in New Issue
Block a user