Skip to content

Commit

Permalink
feat:backend add router global prefix (flipped-aurora#1333)
Browse files Browse the repository at this point in the history
* feat:backend add router global prefix

* feat: global prefix in plugin router

* feat: remove plugin router prefix and add web router setting
  • Loading branch information
Znonymous29 committed Jan 14, 2023
1 parent 92eb83b commit fe9dd6d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions server/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ system:
iplimit-count: 15000
# IP限制一个小时
iplimit-time: 3600
# 路由全局前缀
router-prefix: ""

# captcha configuration
captcha:
Expand Down
1 change: 1 addition & 0 deletions server/config/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type System struct {
UseRedis bool `mapstructure:"use-redis" json:"use-redis" yaml:"use-redis"` // 使用redis
LimitCountIP int `mapstructure:"iplimit-count" json:"iplimit-count" yaml:"iplimit-count"`
LimitTimeIP int `mapstructure:"iplimit-time" json:"iplimit-time" yaml:"iplimit-time"`
RouterPrefix string `mapstructure:"router-prefix" json:"router-prefix" yaml:"router-prefix"`
}
1 change: 1 addition & 0 deletions server/initialize/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initialize

import (
"fmt"

"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/middleware"
"github.com/flipped-aurora/gin-vue-admin/server/plugin/email"
Expand Down
11 changes: 6 additions & 5 deletions server/initialize/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package initialize
import (
"net/http"

_ "github.com/flipped-aurora/gin-vue-admin/server/docs"
"github.com/flipped-aurora/gin-vue-admin/server/docs"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/middleware"
"github.com/flipped-aurora/gin-vue-admin/server/router"
"github.com/gin-gonic/gin"
"github.com/swaggo/gin-swagger"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
)

Expand All @@ -33,11 +33,12 @@ func Routers() *gin.Engine {
// Router.Use(middleware.Cors()) // 直接放行全部跨域请求
// Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
//global.GVA_LOG.Info("use middleware cors")
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
docs.SwaggerInfo.BasePath = global.GVA_CONFIG.System.RouterPrefix
Router.GET(global.GVA_CONFIG.System.RouterPrefix+"/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
global.GVA_LOG.Info("register swagger handler")
// 方便统一添加路由组前缀 多服务器上线使用

PublicGroup := Router.Group("")
PublicGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
{
// 健康监测
PublicGroup.GET("/health", func(c *gin.Context) {
Expand All @@ -48,7 +49,7 @@ func Routers() *gin.Engine {
systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
systemRouter.InitInitRouter(PublicGroup) // 自动初始化相关
}
PrivateGroup := Router.Group("")
PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
{
systemRouter.InitApiRouter(PrivateGroup) // 注册功能api路由
Expand Down
7 changes: 5 additions & 2 deletions server/middleware/casbin_rbac.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package middleware

import (
"strconv"
"strings"

"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
"github.com/flipped-aurora/gin-vue-admin/server/service"
"github.com/flipped-aurora/gin-vue-admin/server/utils"
"github.com/gin-gonic/gin"
"strconv"
)

var casbinService = service.ServiceGroupApp.SystemServiceGroup.CasbinService
Expand All @@ -17,7 +19,8 @@ func CasbinHandler() gin.HandlerFunc {
if global.GVA_CONFIG.System.Env != "develop" {
waitUse, _ := utils.GetClaims(c)
//获取请求的PATH
obj := c.Request.URL.Path
path := c.Request.URL.Path
obj := strings.TrimPrefix(path, global.GVA_CONFIG.System.RouterPrefix)
// 获取请求方法
act := c.Request.Method
// 获取用户的角色
Expand Down
8 changes: 8 additions & 0 deletions web/src/view/systemTools/system/system.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
<el-form-item label="限流时间">
<el-input-number v-model.number="config.system['iplimit-time']" />
</el-form-item>
<el-tooltip
content="请修改完成后,注意一并修改前端env环境下的VITE_BASE_PATH"
placement="top-start"
>
<el-form-item label="全局路由前缀">
<el-input v-model="config.system['router-prefix']" />
</el-form-item>
</el-tooltip>
</el-collapse-item>
<el-collapse-item title="jwt签名" name="2">
<el-form-item label="jwt签名">
Expand Down

0 comments on commit fe9dd6d

Please sign in to comment.