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-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-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 16:41:32 +08:00
|
|
|
controllers := make([]interface{}, 0)
|
|
|
|
controllers = append(controllers, user.NewUserController(app))
|
|
|
|
|
|
|
|
for _, controller := range controllers {
|
|
|
|
reflectRegisterRoute(controller)
|
|
|
|
}
|
|
|
|
|
2022-10-05 19:46:48 +08:00
|
|
|
casbin.RegisterController(app)
|
2022-10-05 16:02:09 +08:00
|
|
|
test.RegisterController(app)
|
2022-10-04 14:59:46 +08:00
|
|
|
}
|
|
|
|
|
2022-10-06 16:41:32 +08:00
|
|
|
func reflectRegisterRoute(controller interface{}) {
|
|
|
|
t := reflect.ValueOf(controller)
|
|
|
|
for i := 0; i < t.NumMethod(); i++ {
|
|
|
|
method := t.Method(i)
|
|
|
|
args := make([]reflect.Value, 0)
|
|
|
|
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",
|
|
|
|
}))
|
|
|
|
}
|