app 调整

swag 调整
This commit is contained in:
Shikong 2022-10-04 13:06:13 +08:00
parent 856847a9fe
commit d5f84ade1d
6 changed files with 140 additions and 28 deletions

View File

@ -46,7 +46,10 @@ go install github.com/swaggo/swag/cmd/swag@latest
##### 生成 swagger 文档 ##### 生成 swagger 文档
```shell ```shell
swag init # --parseInternal 解析内部依赖包,默认值: false
# --parseDependency 解析外部依赖包,默认值: false
# --parseDepth 解析依赖包深度,默认值:100
swag init --parseDependency --parseInternal
``` ```
#### air 热重载 #### air 热重载

View File

@ -189,7 +189,25 @@ const docTemplate = `{
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response" "$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/fiber.Route"
}
}
}
}
}
]
} }
} }
} }
@ -211,6 +229,30 @@ const docTemplate = `{
} }
} }
}, },
"fiber.Route": {
"type": "object",
"properties": {
"method": {
"description": "Public fields",
"type": "string"
},
"name": {
"description": "Route's name",
"type": "string"
},
"params": {
"description": "Case sensitive param keys",
"type": "array",
"items": {
"type": "string"
}
},
"path": {
"description": "Original registered route path",
"type": "string"
}
}
},
"response.Response": { "response.Response": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -181,7 +181,25 @@
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response" "$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/fiber.Route"
}
}
}
}
}
]
} }
} }
} }
@ -203,6 +221,30 @@
} }
} }
}, },
"fiber.Route": {
"type": "object",
"properties": {
"method": {
"description": "Public fields",
"type": "string"
},
"name": {
"description": "Route's name",
"type": "string"
},
"params": {
"description": "Case sensitive param keys",
"type": "array",
"items": {
"type": "string"
}
},
"path": {
"description": "Original registered route path",
"type": "string"
}
}
},
"response.Response": { "response.Response": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -10,6 +10,23 @@ definitions:
example: OK example: OK
type: string type: string
type: object type: object
fiber.Route:
properties:
method:
description: Public fields
type: string
name:
description: Route's name
type: string
params:
description: Case sensitive param keys
items:
type: string
type: array
path:
description: Original registered route path
type: string
type: object
response.Response: response.Response:
properties: properties:
code: code:
@ -138,7 +155,16 @@ paths:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/response.Response' allOf:
- $ref: '#/definitions/response.Response'
- properties:
data:
items:
items:
$ref: '#/definitions/fiber.Route'
type: array
type: array
type: object
summary: 获取所有路由 summary: 获取所有路由
tags: tags:
- Routes - Routes

View File

@ -1,4 +1,4 @@
package main package global
import ( import (
"context" "context"
@ -10,34 +10,35 @@ 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"
) )
func loadApp() { var App *fiber.App
err := viper.Unmarshal(global.Config)
func LoadApp() {
err := viper.Unmarshal(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", global.Config) logger.Log.Infof("\n%#v", Config)
}) })
reloadRedis(global.Config) reloadRedis(Config)
reloadDataSources(global.Config) reloadDataSources(Config)
app = reloadApp(global.Config) App = reloadApp(Config)
if err := app.Listen(global.Config.Server.Addr); err != nil { if err := App.Listen(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 global.Redis != nil { if Redis != nil {
_ = global.Redis.Close() _ = Redis.Close()
} }
r := redis.NewClient(&redis.Options{ r := redis.NewClient(&redis.Options{
@ -56,12 +57,12 @@ func reloadRedis(c *config.BasicConfig) {
logger.Log.Infoln("[√] [Redis] 连接成功") logger.Log.Infoln("[√] [Redis] 连接成功")
}) })
global.Redis = r Redis = r
} }
func reloadDataSources(c *config.BasicConfig) { func reloadDataSources(c *config.BasicConfig) {
if global.DataSources != nil { if DataSources != nil {
_ = global.DataSources.Close() _ = DataSources.Close()
} }
connGroup, err := xorm.NewEngineGroup("mysql", c.Mysql.DataSources) connGroup, err := xorm.NewEngineGroup("mysql", c.Mysql.DataSources)
@ -73,16 +74,16 @@ func reloadDataSources(c *config.BasicConfig) {
logger.Log.Infoln("[√] [数据源] 初始化完成") logger.Log.Infoln("[√] [数据源] 初始化完成")
}) })
global.DataSources = connGroup DataSources = connGroup
if c.Mysql.AutoSync && !fiber.IsChild() { if c.Mysql.AutoSync && !fiber.IsChild() {
global.SyncModelToDataSource() SyncModelToDataSource()
} }
} }
func reloadApp(c *config.BasicConfig) *fiber.App { func reloadApp(c *config.BasicConfig) *fiber.App {
if app != nil { if App != nil {
_ = app.Shutdown() _ = App.Shutdown()
} }
app := fiber.New(fiber.Config{ app := fiber.New(fiber.Config{
@ -137,7 +138,7 @@ func mainAppExec(fn func()) {
// @Tags Routes // @Tags Routes
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} response.Response(Data=[][]fiber.Route) // @Success 200 {object} response.Response{data=[][]fiber.Route}
// @Router /routes [get] // @Router /routes [get]
func routes(app *fiber.App) { func routes(app *fiber.App) {
app.Get("/routes", func(ctx *fiber.Ctx) error { app.Get("/routes", func(ctx *fiber.Ctx) error {

View File

@ -2,9 +2,9 @@ package main
import ( import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/gofiber/fiber/v2"
"github.com/spf13/viper" "github.com/spf13/viper"
"gofiber.study.skcks.cn/common/logger" "gofiber.study.skcks.cn/common/logger"
"gofiber.study.skcks.cn/global"
"os" "os"
"os/signal" "os/signal"
@ -12,8 +12,6 @@ import (
_ "gofiber.study.skcks.cn/docs" _ "gofiber.study.skcks.cn/docs"
) )
var app *fiber.App
// @title GoFiber Study API // @title GoFiber Study API
// @version 1.0 // @version 1.0
// @description 基于 Fiber 框架的 swagger // @description 基于 Fiber 框架的 swagger
@ -40,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)
loadApp() global.LoadApp()
}) })
loadApp() global.LoadApp()
quit := make(chan os.Signal) quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt) signal.Notify(quit, os.Interrupt)