mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-24 07:42:15 +08:00
通过反射注册路由
This commit is contained in:
parent
5287353d07
commit
9eb2560ed1
25
app/app.go
25
app/app.go
@ -12,8 +12,6 @@ import (
|
|||||||
"gofiber.study.skcks.cn/global"
|
"gofiber.study.skcks.cn/global"
|
||||||
)
|
)
|
||||||
|
|
||||||
var App *fiber.App
|
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
err := viper.Unmarshal(global.Config)
|
err := viper.Unmarshal(global.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -28,20 +26,20 @@ func Run() {
|
|||||||
reloadDataSources(global.Config)
|
reloadDataSources(global.Config)
|
||||||
reloadJwt(global.Config)
|
reloadJwt(global.Config)
|
||||||
|
|
||||||
App = reloadApp(global.Config)
|
global.App = reloadApp(global.Config)
|
||||||
|
|
||||||
if err := App.Listen(global.Config.Server.Addr); err != nil {
|
if err := global.App.Listen(global.Config.Server.Addr); err != nil {
|
||||||
logger.Log.Fatalf("[x] [Fiber] 致命错误: %s", err)
|
logger.Log.Fatalf("[x] [Fiber] 致命错误: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func reloadApp(c *config.BasicConfig) *fiber.App {
|
func reloadApp(c *config.BasicConfig) *fiber.App {
|
||||||
if App != nil {
|
if global.App != nil {
|
||||||
_ = App.Shutdown()
|
_ = global.App.Shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
app := fiber.New(fiber.Config{
|
global.App = fiber.New(fiber.Config{
|
||||||
JSONEncoder: json.Marshal,
|
JSONEncoder: json.Marshal,
|
||||||
JSONDecoder: json.Unmarshal,
|
JSONDecoder: json.Unmarshal,
|
||||||
Prefork: c.Server.PreFork,
|
Prefork: c.Server.PreFork,
|
||||||
@ -65,20 +63,19 @@ func reloadApp(c *config.BasicConfig) *fiber.App {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if c.Server.EnableRoutesMsg {
|
if c.Server.EnableRoutesMsg {
|
||||||
routes(app)
|
routes(global.App)
|
||||||
}
|
}
|
||||||
|
|
||||||
controller.SwaggerHandler(app)
|
controller.SwaggerHandler(global.App)
|
||||||
controller.RegisterController(app)
|
controller.RegisterController(global.App)
|
||||||
controller.HelloWorld(app)
|
controller.ErrorHandler(global.App)
|
||||||
controller.ErrorHandler(app)
|
|
||||||
|
|
||||||
app.Hooks().OnListen(func() error {
|
global.App.Hooks().OnListen(func() error {
|
||||||
utils.MainAppExec(func() {
|
utils.MainAppExec(func() {
|
||||||
logger.Log.Infoln("[√] [服务启动完成]")
|
logger.Log.Infoln("[√] [服务启动完成]")
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
return app
|
return global.App
|
||||||
}
|
}
|
||||||
|
1
controller/auth/auth.go
Normal file
1
controller/auth/auth.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package auth
|
@ -4,34 +4,31 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/swagger"
|
"github.com/gofiber/swagger"
|
||||||
"gofiber.study.skcks.cn/common/errorx"
|
"gofiber.study.skcks.cn/common/errorx"
|
||||||
"gofiber.study.skcks.cn/common/logger"
|
|
||||||
"gofiber.study.skcks.cn/common/response"
|
|
||||||
"gofiber.study.skcks.cn/controller/casbin"
|
"gofiber.study.skcks.cn/controller/casbin"
|
||||||
"gofiber.study.skcks.cn/controller/test"
|
"gofiber.study.skcks.cn/controller/test"
|
||||||
"gofiber.study.skcks.cn/controller/user"
|
"gofiber.study.skcks.cn/controller/user"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterController(app *fiber.App) {
|
func RegisterController(app *fiber.App) {
|
||||||
user.RegisterController(app)
|
controllers := make([]interface{}, 0)
|
||||||
|
controllers = append(controllers, user.NewUserController(app))
|
||||||
|
|
||||||
|
for _, controller := range controllers {
|
||||||
|
reflectRegisterRoute(controller)
|
||||||
|
}
|
||||||
|
|
||||||
casbin.RegisterController(app)
|
casbin.RegisterController(app)
|
||||||
test.RegisterController(app)
|
test.RegisterController(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HelloWorld
|
func reflectRegisterRoute(controller interface{}) {
|
||||||
//
|
t := reflect.ValueOf(controller)
|
||||||
// @Summary HelloWorld
|
for i := 0; i < t.NumMethod(); i++ {
|
||||||
// @Description 简单的 HelloWorld 示例
|
method := t.Method(i)
|
||||||
// @Tags HelloWorld
|
args := make([]reflect.Value, 0)
|
||||||
// @Accept json
|
method.Call(args)
|
||||||
// @Produce json
|
}
|
||||||
// @Success 200 {object} response.Response{data=string}
|
|
||||||
// @Failure default {object} errorx.CodeErrorResponse
|
|
||||||
// @Router / [get]
|
|
||||||
func HelloWorld(app *fiber.App) {
|
|
||||||
app.Get("/", func(c *fiber.Ctx) error {
|
|
||||||
logger.Log.Infof("\n%s", c.Request().String())
|
|
||||||
return c.JSON(response.NewResponse("Hello, World 👋!"))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorHandler
|
// ErrorHandler
|
||||||
|
15
controller/types/type.go
Normal file
15
controller/types/type.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Controller struct {
|
||||||
|
Group fiber.Router
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewController(app *fiber.App, group string) *Controller {
|
||||||
|
return &Controller{
|
||||||
|
Group: app.Group(group),
|
||||||
|
}
|
||||||
|
}
|
@ -4,12 +4,46 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"gofiber.study.skcks.cn/common/errorx"
|
"gofiber.study.skcks.cn/common/errorx"
|
||||||
"gofiber.study.skcks.cn/common/response"
|
"gofiber.study.skcks.cn/common/response"
|
||||||
|
"gofiber.study.skcks.cn/controller/types"
|
||||||
"gofiber.study.skcks.cn/services/user"
|
"gofiber.study.skcks.cn/services/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Controller struct {
|
||||||
|
*types.Controller
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserController(app *fiber.App) *Controller {
|
||||||
|
controller := Controller{
|
||||||
|
Controller: types.NewController(app, "/user"),
|
||||||
|
}
|
||||||
|
|
||||||
|
//t := reflect.TypeOf(&controller)
|
||||||
|
//for i := 0; i < t.NumMethod(); i++ {
|
||||||
|
// method := t.Method(i)
|
||||||
|
// args := make([]reflect.Value, 0)
|
||||||
|
// args = append(args, reflect.ValueOf(&controller))
|
||||||
|
// method.Func.Call(args)
|
||||||
|
//}
|
||||||
|
return &controller
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) GetByAccount() {
|
||||||
|
c.Group.Get("/account", func(ctx *fiber.Ctx) error {
|
||||||
|
account := ctx.Query("account")
|
||||||
|
|
||||||
|
u, err := user.GetUserByAccount(account)
|
||||||
|
if err != nil {
|
||||||
|
return ctx.JSON(errorx.NewDefaultError(err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(response.NewResponse(u))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func RegisterController(app *fiber.App) {
|
func RegisterController(app *fiber.App) {
|
||||||
group := app.Group("/user")
|
NewUserController(app)
|
||||||
group.Add(fiber.MethodGet, "/account", getByAccount)
|
//group := app.Group("/user")
|
||||||
|
//group.Add(fiber.MethodGet, "/account", getByAccount)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getByAccount 根据 账号 获取用户信息
|
// getByAccount 根据 账号 获取用户信息
|
||||||
|
5
global/app.go
Normal file
5
global/app.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package global
|
||||||
|
|
||||||
|
import "github.com/gofiber/fiber/v2"
|
||||||
|
|
||||||
|
var App *fiber.App
|
Loading…
Reference in New Issue
Block a user