mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 23:32:15 +08:00
app 调整
swag 调整
This commit is contained in:
parent
856847a9fe
commit
d5f84ade1d
@ -46,7 +46,10 @@ go install github.com/swaggo/swag/cmd/swag@latest
|
||||
|
||||
##### 生成 swagger 文档
|
||||
```shell
|
||||
swag init
|
||||
# --parseInternal 解析内部依赖包,默认值: false
|
||||
# --parseDependency 解析外部依赖包,默认值: false
|
||||
# --parseDepth 解析依赖包深度,默认值:100
|
||||
swag init --parseDependency --parseInternal
|
||||
```
|
||||
|
||||
#### air 热重载
|
||||
|
44
docs/docs.go
44
docs/docs.go
@ -189,7 +189,25 @@ const docTemplate = `{
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.Response"
|
||||
"allOf": [
|
||||
{
|
||||
"$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": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
@ -181,7 +181,25 @@
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/response.Response"
|
||||
"allOf": [
|
||||
{
|
||||
"$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": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
@ -10,6 +10,23 @@ definitions:
|
||||
example: OK
|
||||
type: string
|
||||
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:
|
||||
properties:
|
||||
code:
|
||||
@ -138,7 +155,16 @@ paths:
|
||||
"200":
|
||||
description: OK
|
||||
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: 获取所有路由
|
||||
tags:
|
||||
- Routes
|
||||
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package global
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -10,34 +10,35 @@ 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"
|
||||
)
|
||||
|
||||
func loadApp() {
|
||||
err := viper.Unmarshal(global.Config)
|
||||
var App *fiber.App
|
||||
|
||||
func LoadApp() {
|
||||
err := viper.Unmarshal(Config)
|
||||
if err != nil {
|
||||
logger.Log.Fatalf("配置文件解析失败: %s", err)
|
||||
}
|
||||
|
||||
mainAppExec(func() {
|
||||
logger.Log.Infof("\n%#v", global.Config)
|
||||
logger.Log.Infof("\n%#v", Config)
|
||||
})
|
||||
|
||||
reloadRedis(global.Config)
|
||||
reloadDataSources(global.Config)
|
||||
reloadRedis(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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func reloadRedis(c *config.BasicConfig) {
|
||||
if global.Redis != nil {
|
||||
_ = global.Redis.Close()
|
||||
if Redis != nil {
|
||||
_ = Redis.Close()
|
||||
}
|
||||
|
||||
r := redis.NewClient(&redis.Options{
|
||||
@ -56,12 +57,12 @@ func reloadRedis(c *config.BasicConfig) {
|
||||
logger.Log.Infoln("[√] [Redis] 连接成功")
|
||||
})
|
||||
|
||||
global.Redis = r
|
||||
Redis = r
|
||||
}
|
||||
|
||||
func reloadDataSources(c *config.BasicConfig) {
|
||||
if global.DataSources != nil {
|
||||
_ = global.DataSources.Close()
|
||||
if DataSources != nil {
|
||||
_ = DataSources.Close()
|
||||
}
|
||||
|
||||
connGroup, err := xorm.NewEngineGroup("mysql", c.Mysql.DataSources)
|
||||
@ -73,16 +74,16 @@ func reloadDataSources(c *config.BasicConfig) {
|
||||
logger.Log.Infoln("[√] [数据源] 初始化完成")
|
||||
})
|
||||
|
||||
global.DataSources = connGroup
|
||||
DataSources = connGroup
|
||||
|
||||
if c.Mysql.AutoSync && !fiber.IsChild() {
|
||||
global.SyncModelToDataSource()
|
||||
SyncModelToDataSource()
|
||||
}
|
||||
}
|
||||
|
||||
func reloadApp(c *config.BasicConfig) *fiber.App {
|
||||
if app != nil {
|
||||
_ = app.Shutdown()
|
||||
if App != nil {
|
||||
_ = App.Shutdown()
|
||||
}
|
||||
|
||||
app := fiber.New(fiber.Config{
|
||||
@ -137,7 +138,7 @@ func mainAppExec(fn func()) {
|
||||
// @Tags Routes
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.Response(Data=[][]fiber.Route)
|
||||
// @Success 200 {object} response.Response{data=[][]fiber.Route}
|
||||
// @Router /routes [get]
|
||||
func routes(app *fiber.App) {
|
||||
app.Get("/routes", func(ctx *fiber.Ctx) error {
|
8
main.go
8
main.go
@ -2,9 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/spf13/viper"
|
||||
"gofiber.study.skcks.cn/common/logger"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
@ -12,8 +12,6 @@ import (
|
||||
_ "gofiber.study.skcks.cn/docs"
|
||||
)
|
||||
|
||||
var app *fiber.App
|
||||
|
||||
// @title GoFiber Study API
|
||||
// @version 1.0
|
||||
// @description 基于 Fiber 框架的 swagger
|
||||
@ -40,10 +38,10 @@ func main() {
|
||||
viper.WatchConfig()
|
||||
viper.OnConfigChange(func(in fsnotify.Event) {
|
||||
logger.Log.Infoln(in.Name)
|
||||
loadApp()
|
||||
global.LoadApp()
|
||||
})
|
||||
|
||||
loadApp()
|
||||
global.LoadApp()
|
||||
|
||||
quit := make(chan os.Signal)
|
||||
signal.Notify(quit, os.Interrupt)
|
||||
|
Loading…
Reference in New Issue
Block a user