mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 23:32:15 +08:00
通过反射注册路由
This commit is contained in:
parent
9eb2560ed1
commit
4102109984
@ -4,17 +4,25 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gofiber.study.skcks.cn/common/errorx"
|
||||
"gofiber.study.skcks.cn/common/response"
|
||||
"gofiber.study.skcks.cn/controller/types"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
)
|
||||
|
||||
func RegisterController(app *fiber.App) {
|
||||
group := app.Group("/casbin")
|
||||
group.Add(fiber.MethodGet, "/test", testCasbin)
|
||||
group.Add(fiber.MethodPost, "/reload", reloadCasbin)
|
||||
group.Add(fiber.MethodGet, "/getUserRoles", getUserRoles)
|
||||
type Controller struct {
|
||||
*types.Controller
|
||||
}
|
||||
|
||||
// testCasbin casbin 鉴权测试
|
||||
func (c *Controller) GetRouter() fiber.Router {
|
||||
return c.Router
|
||||
}
|
||||
|
||||
func NewController(app *fiber.App) *Controller {
|
||||
return &Controller{
|
||||
Controller: types.NewController(app, "/casbin"),
|
||||
}
|
||||
}
|
||||
|
||||
// TestCasbin casbin 鉴权测试
|
||||
//
|
||||
// @Summary casbin 鉴权测试
|
||||
// @Description casbin 鉴权测试
|
||||
@ -28,27 +36,29 @@ func RegisterController(app *fiber.App) {
|
||||
// @Success 200 {object} response.Response{data=string}
|
||||
// @Failure default {object} errorx.CodeErrorResponse
|
||||
// @Router /casbin/test [get]
|
||||
func testCasbin(ctx *fiber.Ctx) error {
|
||||
var identity, system, api, act string
|
||||
func (c *Controller) TestCasbin() {
|
||||
c.Router.Get("/test", func(ctx *fiber.Ctx) error {
|
||||
var identity, system, api, act string
|
||||
|
||||
identity = ctx.Query("identity")
|
||||
system = ctx.Query("system")
|
||||
api = ctx.Query("api")
|
||||
act = ctx.Query("act")
|
||||
identity = ctx.Query("identity")
|
||||
system = ctx.Query("system")
|
||||
api = ctx.Query("api")
|
||||
act = ctx.Query("act")
|
||||
|
||||
enforce, err := global.Enforcer.Enforce(identity, system, api, act)
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.NewDefaultError(err.Error()))
|
||||
}
|
||||
enforce, err := global.Enforcer.Enforce(identity, system, api, act)
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.NewDefaultError(err.Error()))
|
||||
}
|
||||
|
||||
if !enforce {
|
||||
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, "无权访问"))
|
||||
}
|
||||
if !enforce {
|
||||
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, "无权访问"))
|
||||
}
|
||||
|
||||
return ctx.JSON(response.NewResponse("授权访问"))
|
||||
return ctx.JSON(response.NewResponse("授权访问"))
|
||||
})
|
||||
}
|
||||
|
||||
// reloadCasbin 重新加载 casbin 策略
|
||||
// ReloadCasbin 重新加载 casbin 策略
|
||||
//
|
||||
// @Summary 重新加载 casbin 策略
|
||||
// @Description 重新加载 casbin 策略
|
||||
@ -58,20 +68,22 @@ func testCasbin(ctx *fiber.Ctx) error {
|
||||
// @Success 200 {object} response.Response{data=string}
|
||||
// @Failure default {object} errorx.CodeErrorResponse
|
||||
// @Router /casbin/reload [post]
|
||||
func reloadCasbin(ctx *fiber.Ctx) error {
|
||||
err := global.Enforcer.LoadPolicy()
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, err.Error()))
|
||||
}
|
||||
func (c *Controller) ReloadCasbin() {
|
||||
c.Router.Post("/reload", func(ctx *fiber.Ctx) error {
|
||||
err := global.Enforcer.LoadPolicy()
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, err.Error()))
|
||||
}
|
||||
|
||||
return ctx.JSON(response.NewResponse("重载成功"))
|
||||
return ctx.JSON(response.NewResponse("重载成功"))
|
||||
})
|
||||
}
|
||||
|
||||
type GetUserRolesQuery struct {
|
||||
Account string `json:"account" example:"root"`
|
||||
}
|
||||
|
||||
// getUserRoles 用户所有角色
|
||||
// GetUserRoles 用户所有角色
|
||||
//
|
||||
// @Summary 用户所有角色
|
||||
// @Description 用户所有角色
|
||||
@ -82,15 +94,17 @@ type GetUserRolesQuery struct {
|
||||
// @Success 200 {object} response.Response{data=[]string}
|
||||
// @Failure default {object} errorx.CodeErrorResponse
|
||||
// @Router /casbin/getUserRoles [get]
|
||||
func getUserRoles(ctx *fiber.Ctx) error {
|
||||
query := &GetUserRolesQuery{}
|
||||
err := ctx.QueryParser(query)
|
||||
roles, err := global.Enforcer.GetRolesForUser("user::" + query.Account)
|
||||
if err != nil {
|
||||
func (c *Controller) GetUserRoles() {
|
||||
c.Router.Get("/getUserRoles", func(ctx *fiber.Ctx) error {
|
||||
query := &GetUserRolesQuery{}
|
||||
err := ctx.QueryParser(query)
|
||||
roles, err := global.Enforcer.GetRolesForUser("user::" + query.Account)
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, err.Error()))
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusForbidden, err.Error()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.JSON(response.NewResponse(roles))
|
||||
return ctx.JSON(response.NewResponse(roles))
|
||||
})
|
||||
}
|
||||
|
@ -6,20 +6,20 @@ import (
|
||||
"gofiber.study.skcks.cn/common/errorx"
|
||||
"gofiber.study.skcks.cn/controller/casbin"
|
||||
"gofiber.study.skcks.cn/controller/test"
|
||||
"gofiber.study.skcks.cn/controller/types"
|
||||
"gofiber.study.skcks.cn/controller/user"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func RegisterController(app *fiber.App) {
|
||||
controllers := make([]interface{}, 0)
|
||||
controllers = append(controllers, user.NewUserController(app))
|
||||
controllers := make([]types.ControllerInterface, 0)
|
||||
controllers = append(controllers, user.NewController(app))
|
||||
controllers = append(controllers, casbin.NewController(app))
|
||||
controllers = append(controllers, test.NewController(app))
|
||||
|
||||
for _, controller := range controllers {
|
||||
reflectRegisterRoute(controller)
|
||||
}
|
||||
|
||||
casbin.RegisterController(app)
|
||||
test.RegisterController(app)
|
||||
}
|
||||
|
||||
func reflectRegisterRoute(controller interface{}) {
|
||||
|
@ -4,16 +4,25 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gofiber.study.skcks.cn/common/errorx"
|
||||
"gofiber.study.skcks.cn/common/response"
|
||||
"gofiber.study.skcks.cn/controller/types"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
)
|
||||
|
||||
func RegisterController(app *fiber.App) {
|
||||
group := app.Group("/test")
|
||||
jwtEncode(group)
|
||||
jwtDecode(group)
|
||||
type Controller struct {
|
||||
*types.Controller
|
||||
}
|
||||
|
||||
// jwtEncode jwt token 生成测试
|
||||
func (c *Controller) GetRouter() fiber.Router {
|
||||
return c.Router
|
||||
}
|
||||
|
||||
func NewController(app *fiber.App) *Controller {
|
||||
return &Controller{
|
||||
Controller: types.NewController(app, "/test"),
|
||||
}
|
||||
}
|
||||
|
||||
// JwtEncode jwt token 生成测试
|
||||
//
|
||||
// @Summary jwt token 生成测试
|
||||
// @Description jwt token 生成测试
|
||||
@ -24,8 +33,8 @@ func RegisterController(app *fiber.App) {
|
||||
// @Success 200 {object} response.Response{data=string}
|
||||
// @Failure default {object} errorx.CodeErrorResponse
|
||||
// @Router /test/jwt [post]
|
||||
func jwtEncode(r fiber.Router) {
|
||||
r.Add(fiber.MethodPost, "/jwt", func(ctx *fiber.Ctx) error {
|
||||
func (c *Controller) JwtEncode() {
|
||||
c.Router.Add(fiber.MethodPost, "/jwt", func(ctx *fiber.Ctx) error {
|
||||
claims := global.UserClaims{}
|
||||
err := ctx.BodyParser(&claims)
|
||||
|
||||
@ -43,7 +52,7 @@ func jwtEncode(r fiber.Router) {
|
||||
})
|
||||
}
|
||||
|
||||
// jwtDecode jwt token 解析测试
|
||||
// JwtDecode jwt token 解析测试
|
||||
//
|
||||
// @Summary jwt token 解析测试
|
||||
// @Description jwt token 解析测试
|
||||
@ -54,8 +63,8 @@ func jwtEncode(r fiber.Router) {
|
||||
// @Success 200 {object} response.Response{data=global.UserClaims}
|
||||
// @Failure default {object} errorx.CodeErrorResponse
|
||||
// @Router /test/jwt [get]
|
||||
func jwtDecode(r fiber.Router) {
|
||||
r.Add(fiber.MethodGet, "/jwt", func(ctx *fiber.Ctx) error {
|
||||
func (c *Controller) JwtDecode() {
|
||||
c.Router.Add(fiber.MethodGet, "/jwt", func(ctx *fiber.Ctx) error {
|
||||
token := ctx.Query("token")
|
||||
claims, err := global.ParseToken(token)
|
||||
|
||||
|
@ -4,12 +4,16 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ControllerInterface interface {
|
||||
GetRouter() fiber.Router
|
||||
}
|
||||
|
||||
type Controller struct {
|
||||
Group fiber.Router
|
||||
Router fiber.Router
|
||||
}
|
||||
|
||||
func NewController(app *fiber.App, group string) *Controller {
|
||||
return &Controller{
|
||||
Group: app.Group(group),
|
||||
Router: app.Group(group),
|
||||
}
|
||||
}
|
||||
|
@ -12,41 +12,17 @@ type Controller struct {
|
||||
*types.Controller
|
||||
}
|
||||
|
||||
func NewUserController(app *fiber.App) *Controller {
|
||||
controller := Controller{
|
||||
func (c *Controller) GetRouter() fiber.Router {
|
||||
return c.Router
|
||||
}
|
||||
|
||||
func NewController(app *fiber.App) *Controller {
|
||||
return &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) {
|
||||
NewUserController(app)
|
||||
//group := app.Group("/user")
|
||||
//group.Add(fiber.MethodGet, "/account", getByAccount)
|
||||
}
|
||||
|
||||
// getByAccount 根据 账号 获取用户信息
|
||||
// GetByAccount 根据 账号 获取用户信息
|
||||
//
|
||||
// @Summary 根据 账号 获取用户信息
|
||||
// @Description 根据 账号 获取用户信息
|
||||
@ -58,13 +34,15 @@ func RegisterController(app *fiber.App) {
|
||||
// @Failure default {object} errorx.CodeErrorResponse
|
||||
// @Router /user/account [get]
|
||||
// @Security Token
|
||||
func getByAccount(ctx *fiber.Ctx) error {
|
||||
account := ctx.Query("account")
|
||||
func (c *Controller) GetByAccount() {
|
||||
c.Router.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()))
|
||||
}
|
||||
u, err := user.GetUserByAccount(account)
|
||||
if err != nil {
|
||||
return ctx.JSON(errorx.NewDefaultError(err.Error()))
|
||||
}
|
||||
|
||||
return ctx.JSON(response.NewResponse(u))
|
||||
return ctx.JSON(response.NewResponse(u))
|
||||
})
|
||||
}
|
||||
|
41
docs/docs.go
41
docs/docs.go
@ -24,47 +24,6 @@ const docTemplate = `{
|
||||
"host": "{{.Host}}",
|
||||
"basePath": "{{.BasePath}}",
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"description": "简单的 HelloWorld 示例",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"HelloWorld"
|
||||
],
|
||||
"summary": "HelloWorld",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/response.Response"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/casbin/getUserRoles": {
|
||||
"get": {
|
||||
"description": "用户所有角色",
|
||||
|
@ -16,47 +16,6 @@
|
||||
},
|
||||
"basePath": "/",
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"description": "简单的 HelloWorld 示例",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"HelloWorld"
|
||||
],
|
||||
"summary": "HelloWorld",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/response.Response"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/casbin/getUserRoles": {
|
||||
"get": {
|
||||
"description": "用户所有角色",
|
||||
|
@ -81,30 +81,6 @@ info:
|
||||
title: GoFiber Study API
|
||||
version: "1.0"
|
||||
paths:
|
||||
/:
|
||||
get:
|
||||
consumes:
|
||||
- application/json
|
||||
description: 简单的 HelloWorld 示例
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
allOf:
|
||||
- $ref: '#/definitions/response.Response'
|
||||
- properties:
|
||||
data:
|
||||
type: string
|
||||
type: object
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/errorx.CodeErrorResponse'
|
||||
summary: HelloWorld
|
||||
tags:
|
||||
- HelloWorld
|
||||
/casbin/getUserRoles:
|
||||
get:
|
||||
consumes:
|
||||
|
Loading…
Reference in New Issue
Block a user