diff --git a/server/internal/router/admin.go b/server/internal/router/admin.go index 82d6523..4c5c27c 100644 --- a/server/internal/router/admin.go +++ b/server/internal/router/admin.go @@ -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, // 基础 ) diff --git a/server/internal/router/api.go b/server/internal/router/api.go index dd9836b..382ba06 100644 --- a/server/internal/router/api.go +++ b/server/internal/router/api.go @@ -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, // 支付异步通知 diff --git a/server/internal/router/genrouter/init.go b/server/internal/router/genrouter/init.go index dcfd590..520437a 100644 --- a/server/internal/router/genrouter/init.go +++ b/server/internal/router/genrouter/init.go @@ -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...) } diff --git a/server/internal/router/home.go b/server/internal/router/home.go index e0152ab..9ddb930 100644 --- a/server/internal/router/home.go +++ b/server/internal/router/home.go @@ -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, // 基础 ) diff --git a/server/internal/router/websocket.go b/server/internal/router/websocket.go index 3d03905..6e69e8c 100644 --- a/server/internal/router/websocket.go +++ b/server/internal/router/websocket.go @@ -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消息。方便测试没有放权限中间件,实际使用时请自行调整 diff --git a/server/resource/generate/default/addon/router/genrouter/init.go.template b/server/resource/generate/default/addon/router/genrouter/init.go.template index d15d513..6ba3e98 100644 --- a/server/resource/generate/default/addon/router/genrouter/init.go.template +++ b/server/resource/generate/default/addon/router/genrouter/init.go.template @@ -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" diff --git a/server/utility/simple/simple.go b/server/utility/simple/simple.go index fd2dd21..94c03ed 100644 --- a/server/utility/simple/simple.go +++ b/server/utility/simple/simple.go @@ -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 {