统一应用路由前缀规则

This commit is contained in:
孟帅 2023-06-12 17:49:29 +08:00
parent d9d257a067
commit e6abfbcacf
7 changed files with 21 additions and 16 deletions

View File

@ -7,14 +7,15 @@ package router
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"hotgo/internal/consts"
"hotgo/internal/controller/admin/admin"
"hotgo/internal/controller/admin/common"
"hotgo/internal/controller/admin/pay"
"hotgo/internal/controller/admin/sys"
"hotgo/internal/router/genrouter"
"hotgo/internal/service"
"hotgo/utility/simple"
)
func Admin(ctx context.Context, group *ghttp.RouterGroup) {
@ -23,8 +24,7 @@ func Admin(ctx context.Context, group *ghttp.RouterGroup) {
r.Response.RedirectTo("/admin")
})
prefix := g.Cfg().MustGet(ctx, "router.admin.prefix", "/admin")
group.Group(prefix.String(), func(group *ghttp.RouterGroup) {
group.Group(simple.RouterPrefix(ctx, consts.AppAdmin), func(group *ghttp.RouterGroup) {
group.Bind(
common.Site, // 基础
)

View File

@ -7,18 +7,18 @@ package router
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"hotgo/internal/consts"
"hotgo/internal/controller/api/member"
"hotgo/internal/controller/api/pay"
"hotgo/internal/controller/api/user"
"hotgo/internal/service"
"hotgo/utility/simple"
)
// Api 前台路由
func Api(ctx context.Context, group *ghttp.RouterGroup) {
prefix := g.Cfg().MustGet(ctx, "router.api.prefix", "/api")
group.Group(prefix.String(), func(group *ghttp.RouterGroup) {
group.Group(simple.RouterPrefix(ctx, consts.AppApi), func(group *ghttp.RouterGroup) {
group.Bind(
user.Hello,
pay.Notify, // 支付异步通知

View File

@ -7,9 +7,10 @@ package genrouter
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"hotgo/internal/consts"
"hotgo/internal/service"
"hotgo/utility/simple"
)
var (
@ -19,8 +20,7 @@ var (
// Register 注册通过代码生成的后台路由
func Register(ctx context.Context, group *ghttp.RouterGroup) {
prefix := g.Cfg().MustGet(ctx, "router.admin.prefix", "/admin")
group.Group(prefix.String(), func(group *ghttp.RouterGroup) {
group.Group(simple.RouterPrefix(ctx, consts.AppAdmin), func(group *ghttp.RouterGroup) {
if len(NoLoginRouter) > 0 {
group.Bind(NoLoginRouter...)
}

View File

@ -7,10 +7,11 @@ package router
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
api "hotgo/api/home/base"
"hotgo/internal/consts"
"hotgo/internal/controller/home/base"
"hotgo/utility/simple"
)
// Home 前台页面路由
@ -20,8 +21,7 @@ func Home(ctx context.Context, group *ghttp.RouterGroup) {
_, _ = base.Site.Index(r.Context(), &api.SiteIndexReq{})
})
prefix := g.Cfg().MustGet(ctx, "router.home.prefix", "/home")
group.Group(prefix.String(), func(group *ghttp.RouterGroup) {
group.Group(simple.RouterPrefix(ctx, consts.AppHome), func(group *ghttp.RouterGroup) {
group.Bind(
base.Site, // 基础
)

View File

@ -7,19 +7,19 @@ package router
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"hotgo/internal/consts"
controller "hotgo/internal/controller/websocket"
"hotgo/internal/controller/websocket/handler/admin"
"hotgo/internal/controller/websocket/handler/common"
"hotgo/internal/service"
"hotgo/internal/websocket"
"hotgo/utility/simple"
)
// WebSocket ws路由配置
func WebSocket(ctx context.Context, group *ghttp.RouterGroup) {
prefix := g.Cfg().MustGet(ctx, "router.ws.prefix", "/socket")
group.Group(prefix.String(), func(group *ghttp.RouterGroup) {
group.Group(simple.RouterPrefix(ctx, consts.AppWebSocket), func(group *ghttp.RouterGroup) {
group.Middleware()
group.Bind(
controller.Send, // 通过http发送ws消息。方便测试没有放权限中间件实际使用时请自行调整

View File

@ -8,7 +8,7 @@ package genrouter
import (
"context"
"github.com/gogf/gf/v2/net/ghttp"
"hotgo/addons/hgexample/global"
"hotgo/addons/@{.name}/global"
"hotgo/internal/consts"
"hotgo/internal/library/addons"
"hotgo/internal/service"

View File

@ -23,6 +23,11 @@ func AppName(ctx context.Context) string {
return g.Cfg().MustGet(ctx, "appName", "hotgo").String()
}
// RouterPrefix 获取应用路由前缀
func RouterPrefix(ctx context.Context, app string) string {
return g.Cfg().MustGet(ctx, "router."+app+".prefix", "/"+app+"").String()
}
// FilterMaskDemo 过滤演示环境下的配置隐藏字段
func FilterMaskDemo(ctx context.Context, src g.Map) g.Map {
if src == nil {