2023-02-23 17:53:04 +08:00
|
|
|
|
// Package addons
|
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
|
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
|
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
|
|
|
package addons
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"hotgo/internal/consts"
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-07 20:08:56 +08:00
|
|
|
|
var cacheResourcePath string
|
|
|
|
|
|
|
|
|
|
// GetResourcePath 获取插件资源路径
|
|
|
|
|
func GetResourcePath(ctx context.Context) string {
|
|
|
|
|
if len(cacheResourcePath) > 0 {
|
|
|
|
|
return cacheResourcePath
|
|
|
|
|
}
|
2024-04-22 23:08:40 +08:00
|
|
|
|
basePath := g.Cfg().MustGet(ctx, "system.addonsResourcePath").String()
|
2024-03-07 20:08:56 +08:00
|
|
|
|
if basePath == "" {
|
2024-04-22 23:08:40 +08:00
|
|
|
|
g.Log().Warning(ctx, "addons GetResourcePath not config found:'system.addonsResourcePath', use default values:'resource'")
|
2024-03-07 20:08:56 +08:00
|
|
|
|
basePath = "resource"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cacheResourcePath = basePath
|
|
|
|
|
return basePath
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-14 18:09:49 +08:00
|
|
|
|
// GetModulePath 获取指定模块相对路径
|
|
|
|
|
func GetModulePath(name string) string {
|
|
|
|
|
return "./" + consts.AddonsDir + "/" + name
|
2023-02-23 17:53:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-14 18:09:49 +08:00
|
|
|
|
// ViewPath 默认的插件模板路径
|
2024-03-07 20:08:56 +08:00
|
|
|
|
// 模板路径:resource/addons/插件模块名称/template
|
|
|
|
|
// 例如:resource/addons/hgexample/template
|
|
|
|
|
// 如果你不喜欢现在的风格,可以自行调整
|
|
|
|
|
func ViewPath(name, basePath string) string {
|
|
|
|
|
return basePath + "/" + consts.AddonsDir + "/" + name + "/template"
|
2023-06-14 18:09:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StaticPath 默认的插件静态路映射关系
|
2024-03-07 20:08:56 +08:00
|
|
|
|
// 静态资源路径:resource/public/addons/插件模块名称/public
|
|
|
|
|
// 例如访问:http://127.0.0.1:8000/addons/hgexample/default 则指向文件-> resource/addons/hgexample/public/default
|
|
|
|
|
// 如果你不喜欢现在的风格,可以自行调整
|
|
|
|
|
func StaticPath(name, basePath string) (string, string) {
|
|
|
|
|
return "/" + consts.AddonsDir + "/" + name, basePath + "/" + consts.AddonsDir + "/" + name + "/public"
|
2023-02-23 17:53:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RouterPrefix 路由前缀
|
2023-06-14 18:09:49 +08:00
|
|
|
|
// 最终效果:/应用名称/插件模块名称/xxx/xxx。
|
2024-03-07 20:08:56 +08:00
|
|
|
|
// 如果你不喜欢现在的风格,可以自行调整
|
2023-02-23 17:53:04 +08:00
|
|
|
|
func RouterPrefix(ctx context.Context, app, name string) string {
|
|
|
|
|
var prefix = "/"
|
2023-05-12 16:20:22 +08:00
|
|
|
|
if app != "" {
|
|
|
|
|
prefix = g.Cfg().MustGet(ctx, "router."+app+".prefix", "/"+app+"").String()
|
2023-02-23 17:53:04 +08:00
|
|
|
|
}
|
|
|
|
|
return prefix + "/" + name
|
|
|
|
|
}
|