2022-10-04 00:24:41 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/swagger"
|
|
|
|
"gofiber.study.skcks.cn/common/errorx"
|
2022-10-06 17:05:15 +08:00
|
|
|
"gofiber.study.skcks.cn/controller/auth"
|
2022-10-05 19:46:48 +08:00
|
|
|
"gofiber.study.skcks.cn/controller/casbin"
|
2022-10-05 16:02:09 +08:00
|
|
|
"gofiber.study.skcks.cn/controller/test"
|
2022-11-13 19:34:48 +08:00
|
|
|
"gofiber.study.skcks.cn/controller/topical"
|
2022-10-06 16:58:09 +08:00
|
|
|
"gofiber.study.skcks.cn/controller/types"
|
2022-10-04 14:59:46 +08:00
|
|
|
"gofiber.study.skcks.cn/controller/user"
|
2022-10-06 16:41:32 +08:00
|
|
|
"reflect"
|
2022-10-04 00:24:41 +08:00
|
|
|
)
|
|
|
|
|
2022-10-04 14:59:46 +08:00
|
|
|
func RegisterController(app *fiber.App) {
|
2022-10-06 17:05:15 +08:00
|
|
|
controllers := []types.ControllerInterface{
|
|
|
|
auth.NewController(app),
|
|
|
|
user.NewController(app),
|
|
|
|
casbin.NewController(app),
|
|
|
|
test.NewController(app),
|
2022-11-13 19:34:48 +08:00
|
|
|
topical.NewController(app),
|
2022-10-06 17:05:15 +08:00
|
|
|
}
|
2022-10-06 16:41:32 +08:00
|
|
|
|
2022-10-06 17:05:15 +08:00
|
|
|
args := make([]reflect.Value, 0)
|
2022-10-06 16:41:32 +08:00
|
|
|
for _, controller := range controllers {
|
2022-10-06 17:05:15 +08:00
|
|
|
reflectRegisterRoute(controller, args)
|
2022-10-06 16:41:32 +08:00
|
|
|
}
|
2022-10-04 14:59:46 +08:00
|
|
|
}
|
|
|
|
|
2022-10-06 17:05:15 +08:00
|
|
|
func reflectRegisterRoute(controller interface{}, args []reflect.Value) {
|
2022-10-06 16:41:32 +08:00
|
|
|
t := reflect.ValueOf(controller)
|
|
|
|
for i := 0; i < t.NumMethod(); i++ {
|
|
|
|
method := t.Method(i)
|
|
|
|
method.Call(args)
|
|
|
|
}
|
2022-10-04 00:24:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorHandler
|
|
|
|
//
|
|
|
|
// @Summary 错误处理
|
|
|
|
// @Description 错误信息示例
|
|
|
|
// @Tags Error
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Failure default {object} errorx.CodeErrorResponse
|
|
|
|
// @Router /error [get]
|
|
|
|
// @Router /error [post]
|
|
|
|
// @Router /error [put]
|
|
|
|
// @Router /error [delete]
|
|
|
|
// @Router /error [patch]
|
|
|
|
func ErrorHandler(app *fiber.App) {
|
|
|
|
app.All("/error", func(ctx *fiber.Ctx) error {
|
|
|
|
return ctx.JSON(errorx.NewDefaultError("错误信息示例"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwaggerHandler(app *fiber.App) {
|
|
|
|
app.Get("/swagger/*", swagger.New(swagger.Config{
|
|
|
|
DeepLinking: false,
|
|
|
|
// Expand ("list") or Collapse ("none") tag groups by default
|
|
|
|
DocExpansion: "list",
|
|
|
|
}))
|
|
|
|
}
|