im 管理后台
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
var cacheResourcePath string
|
||||
|
||||
// GetResourcePath 获取插件资源路径
|
||||
func GetResourcePath(ctx context.Context) string {
|
||||
if len(cacheResourcePath) > 0 {
|
||||
return cacheResourcePath
|
||||
}
|
||||
basePath := g.Cfg().MustGet(ctx, "system.addonsResourcePath").String()
|
||||
if basePath == "" {
|
||||
g.Log().Warning(ctx, "addons GetResourcePath not config found:'system.addonsResourcePath', use default values:'resource'")
|
||||
basePath = "resource"
|
||||
}
|
||||
|
||||
cacheResourcePath = basePath
|
||||
return basePath
|
||||
}
|
||||
|
||||
// GetModulePath 获取指定模块相对路径
|
||||
func GetModulePath(name string) string {
|
||||
return "./" + consts.AddonsDir + "/" + name
|
||||
}
|
||||
|
||||
// ViewPath 默认的插件模板路径
|
||||
// 模板路径:resource/addons/插件模块名称/template
|
||||
// 例如:resource/addons/hgexample/template
|
||||
// 如果你不喜欢现在的风格,可以自行调整
|
||||
func ViewPath(name, basePath string) string {
|
||||
return basePath + "/" + consts.AddonsDir + "/" + name + "/template"
|
||||
}
|
||||
|
||||
// StaticPath 默认的插件静态路映射关系
|
||||
// 静态资源路径: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"
|
||||
}
|
||||
|
||||
// RouterPrefix 路由前缀
|
||||
// 最终效果:/应用名称/插件模块名称/xxx/xxx。
|
||||
// 如果你不喜欢现在的风格,可以自行调整
|
||||
func RouterPrefix(ctx context.Context, app, name string) string {
|
||||
var prefix = "/"
|
||||
if app != "" {
|
||||
prefix = g.Cfg().MustGet(ctx, "router."+app+".prefix", "/"+app+"").String()
|
||||
}
|
||||
return prefix + "/" + name
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
// 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"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/utility/validate"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BuildOption struct {
|
||||
Skeleton Skeleton
|
||||
Config *model.BuildAddonConfig
|
||||
Extend []string `json:"extend" dc:"扩展功能"`
|
||||
}
|
||||
|
||||
// Build 构建新插件
|
||||
func Build(ctx context.Context, option *BuildOption) (err error) {
|
||||
var (
|
||||
resourcePath = GetResourcePath(ctx)
|
||||
buildPath = "./" + consts.AddonsDir + "/" + option.Skeleton.Name
|
||||
modulesPath = "./" + consts.AddonsDir + "/modules/" + option.Skeleton.Name + ".go"
|
||||
webApiPath = gstr.Replace(option.Config.WebApiPath, "{$name}", option.Skeleton.Name)
|
||||
webViewsPath = gstr.Replace(option.Config.WebViewsPath, "{$name}", option.Skeleton.Name)
|
||||
replaces = map[string]string{
|
||||
"@{.label}": option.Skeleton.Label,
|
||||
"@{.name}": option.Skeleton.Name,
|
||||
"@{.group}": strconv.Itoa(option.Skeleton.Group),
|
||||
"@{.brief}": option.Skeleton.Brief,
|
||||
"@{.description}": option.Skeleton.Description,
|
||||
"@{.author}": option.Skeleton.Author,
|
||||
"@{.version}": option.Skeleton.Version,
|
||||
"@{.hgVersion}": consts.VersionApp, // HG 版本
|
||||
}
|
||||
)
|
||||
|
||||
if resourcePath == "" {
|
||||
err = gerror.New("请先设置一个有效的插件资源路径,配置名称:'system.addonsResourcePath'")
|
||||
return
|
||||
}
|
||||
|
||||
if err = checkBuildDir(buildPath, modulesPath, webApiPath, webViewsPath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// scans directory recursively
|
||||
list, err := gfile.ScanDirFunc(option.Config.SrcPath, "*", true, func(path string) string {
|
||||
return path
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, path := range list {
|
||||
if !gfile.IsReadable(path) {
|
||||
err = gerror.Newf("file:%v is unreadable, please check permissions", path)
|
||||
return
|
||||
}
|
||||
|
||||
if gfile.IsDir(path) {
|
||||
continue
|
||||
}
|
||||
|
||||
flowFile := gstr.ReplaceByMap(path, map[string]string{
|
||||
gfile.RealPath(option.Config.SrcPath): "",
|
||||
".template": "",
|
||||
})
|
||||
flowFile = buildPath + "/" + flowFile
|
||||
|
||||
content := gstr.ReplaceByMap(gfile.GetContents(path), replaces)
|
||||
|
||||
if err = gfile.PutContents(flowFile, content); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 隐式注入插件
|
||||
if err = gfile.PutContents(modulesPath, gstr.ReplaceByMap(importModules, replaces)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// webApi
|
||||
if err = gfile.PutContents(webApiPath+"/config/index.ts", gstr.ReplaceByMap(webApiLayout, replaces)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// web插件配置主页面
|
||||
if err = gfile.PutContents(webViewsPath+"/config/BasicSetting.vue", gstr.ReplaceByMap(webConfigBasicSetting, replaces)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// web插件基础配置页面
|
||||
if err = gfile.PutContents(webViewsPath+"/config/system.vue", gstr.ReplaceByMap(webConfigSystem, replaces)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建静态目录
|
||||
if validate.InSlice(option.Extend, consts.AddonsExtendResourcePublic) {
|
||||
_, staticPath := StaticPath(option.Skeleton.Name, resourcePath)
|
||||
content := fmt.Sprintf(resourcePublicDefaultFile, option.Skeleton.Label)
|
||||
if err = gfile.PutContents(staticPath+"/default", content); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 创建模板目录
|
||||
if validate.InSlice(option.Extend, consts.AddonsExtendResourceTemplate) {
|
||||
viewPath := ViewPath(option.Skeleton.Name, resourcePath)
|
||||
if err = gfile.PutContents(viewPath+"/home/index.html", resourceTemplateHomeFile); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkBuildDir(paths ...string) (err error) {
|
||||
if len(paths) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, path := range paths {
|
||||
if gfile.Exists(path) {
|
||||
return gerror.Newf("插件已存在,请换一个插件名称或者经确认无误后依次删除文件夹: [%v] 后重新生成", strings.Join(paths, "、\t"))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
// 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
|
||||
|
||||
const (
|
||||
importModules = `// Package modules
|
||||
// @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 modules
|
||||
|
||||
import _ "hotgo/addons/@{.name}"
|
||||
`
|
||||
|
||||
webApiLayout = `import { http } from '@/utils/http/axios';
|
||||
|
||||
export function getConfig(params) {
|
||||
return http.request({
|
||||
url: '/@{.name}/config/get',
|
||||
method: 'get',
|
||||
params,
|
||||
});
|
||||
}
|
||||
|
||||
export function updateConfig(params) {
|
||||
return http.request({
|
||||
url: '/@{.name}/config/update',
|
||||
method: 'post',
|
||||
params,
|
||||
});
|
||||
}
|
||||
`
|
||||
|
||||
webConfigBasicSetting = `<template>
|
||||
<div>
|
||||
<n-spin :show="show" description="请稍候...">
|
||||
<n-form :label-width="80" :model="formValue" :rules="rules" ref="formRef">
|
||||
<n-form-item label="测试参数" path="basicTest">
|
||||
<n-input v-model:value="formValue.basicTest" placeholder="请输入测试参数" />
|
||||
<template #feedback>
|
||||
这是一个测试参数,每个插件都可以有独立的配置项,可以按需添加</template
|
||||
>
|
||||
</n-form-item>
|
||||
|
||||
<div>
|
||||
<n-space>
|
||||
<n-button type="primary" @click="formSubmit">保存更新</n-button>
|
||||
</n-space>
|
||||
</div>
|
||||
</n-form>
|
||||
</n-spin>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, onMounted } from 'vue';
|
||||
import { useMessage } from 'naive-ui';
|
||||
import { getConfig, updateConfig } from '@/api/addons/@{.name}/config';
|
||||
|
||||
const group = ref('basic');
|
||||
|
||||
const show = ref(false);
|
||||
const rules = {
|
||||
basicTest: {
|
||||
required: true,
|
||||
message: '请输入测试参数',
|
||||
trigger: 'blur',
|
||||
},
|
||||
};
|
||||
|
||||
const formRef: any = ref(null);
|
||||
const message = useMessage();
|
||||
|
||||
const formValue = ref({
|
||||
basicTest: 'HotGo',
|
||||
});
|
||||
|
||||
function formSubmit() {
|
||||
formRef.value.validate((errors) => {
|
||||
if (!errors) {
|
||||
updateConfig({ group: group.value, list: formValue.value }).then((_res) => {
|
||||
message.success('更新成功');
|
||||
load();
|
||||
});
|
||||
} else {
|
||||
message.error('验证失败,请填写完整信息');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
load();
|
||||
});
|
||||
|
||||
function load() {
|
||||
show.value = true;
|
||||
new Promise((_resolve, _reject) => {
|
||||
getConfig({ group: group.value })
|
||||
.then((res) => {
|
||||
formValue.value = res.list;
|
||||
})
|
||||
.finally(() => {
|
||||
show.value = false;
|
||||
});
|
||||
});
|
||||
}
|
||||
</script>
|
||||
`
|
||||
|
||||
webConfigSystem = `<template>
|
||||
<div>
|
||||
<n-grid cols="24 300:1 600:24" :x-gap="12">
|
||||
<n-grid-item span="6">
|
||||
<n-card :bordered="false" size="small" class="proCard">
|
||||
<n-thing
|
||||
class="thing-cell"
|
||||
v-for="item in typeTabList"
|
||||
:key="item.key"
|
||||
:class="{ 'thing-cell-on': type === item.key }"
|
||||
@click="switchType(item)"
|
||||
>
|
||||
<template #header>{{ item.name }}</template>
|
||||
<template #description>{{ item.desc }}</template>
|
||||
</n-thing>
|
||||
</n-card>
|
||||
</n-grid-item>
|
||||
<n-grid-item span="18">
|
||||
<n-card :bordered="false" size="small" :title="typeTitle" class="proCard">
|
||||
<BasicSetting v-if="type === 1" />
|
||||
</n-card>
|
||||
</n-grid-item>
|
||||
</n-grid>
|
||||
</div>
|
||||
</template>
|
||||
<script lang="ts">
|
||||
import { defineComponent, reactive, toRefs } from 'vue';
|
||||
import BasicSetting from './BasicSetting.vue';
|
||||
const typeTabList = [
|
||||
{
|
||||
name: '基本设置',
|
||||
desc: '系统常规设置',
|
||||
key: 1,
|
||||
},
|
||||
];
|
||||
export default defineComponent({
|
||||
components: {
|
||||
BasicSetting,
|
||||
},
|
||||
setup() {
|
||||
const state = reactive({
|
||||
type: 1,
|
||||
typeTitle: '基本设置',
|
||||
});
|
||||
|
||||
function switchType(e) {
|
||||
state.type = e.key;
|
||||
state.typeTitle = e.name;
|
||||
}
|
||||
|
||||
return {
|
||||
...toRefs(state),
|
||||
switchType,
|
||||
typeTabList,
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
<style lang="less" scoped>
|
||||
.thing-cell {
|
||||
margin: 0 -16px 10px;
|
||||
padding: 5px 16px;
|
||||
|
||||
&:hover {
|
||||
background: #f3f3f3;
|
||||
cursor: pointer;
|
||||
}
|
||||
}
|
||||
|
||||
.thing-cell-on {
|
||||
background: #f0faff;
|
||||
color: #2d8cf0;
|
||||
|
||||
::v-deep(.n-thing-main .n-thing-header .n-thing-header__title) {
|
||||
color: #2d8cf0;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
background: #f0faff;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
`
|
||||
|
||||
resourcePublicDefaultFile = "Hello!这是创建插件 [%v] 时默认生成的一个静态目录文件,用于测试,当你看到这个提示时,说明已经联调成功啦!"
|
||||
|
||||
resourceTemplateHomeFile = `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
|
||||
<meta name="keywords" content="@{.Keywords}"/>
|
||||
<meta name="description" content="@{.Description}"/>
|
||||
<title>@{.Title}</title>
|
||||
<script type="text/javascript" src="/resource/home/js/jquery-3.6.0.min.js"></script>
|
||||
<style>
|
||||
html, body {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background-color: #f6f6f6;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div style="padding-top: 100px;text-align:center;">
|
||||
<h1><p>Hello,@{.Data.name}!!</p></h1>
|
||||
<h2><p>@{.Data.module}</p></h2>
|
||||
<h2><p>服务器时间:@{.Data.time}</p></h2>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
<script>
|
||||
|
||||
</script>
|
||||
</html>`
|
||||
)
|
||||
@@ -0,0 +1,120 @@
|
||||
// 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"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
// InstallRecord 安装记录
|
||||
type InstallRecord struct {
|
||||
Id int64 `json:"id" description:"安装ID"`
|
||||
Version string `json:"version" description:"安装版本"`
|
||||
Status int `json:"status" description:"安装状态"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" description:"创建时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" description:"更新时间"`
|
||||
}
|
||||
|
||||
func GetModel(ctx context.Context) *gdb.Model {
|
||||
return dao.SysAddonsInstall.Ctx(ctx)
|
||||
}
|
||||
|
||||
func ScanInstall(m Module) (record *InstallRecord, err error) {
|
||||
err = GetModel(m.Ctx()).Where(dao.SysAddonsInstall.Columns().Name, m.GetSkeleton().Name).Scan(&record)
|
||||
return
|
||||
}
|
||||
|
||||
// IsInstall 模块是否已安装
|
||||
func IsInstall(m Module) bool {
|
||||
record, err := ScanInstall(m)
|
||||
if err != nil {
|
||||
g.Log().Debugf(m.Ctx(), "addons.IsInstall err:%+v", err)
|
||||
return false
|
||||
}
|
||||
if record == nil {
|
||||
return false
|
||||
}
|
||||
return record.Status == consts.AddonsInstallStatusOk
|
||||
}
|
||||
|
||||
// Install 安装模块
|
||||
func Install(m Module) (err error) {
|
||||
record, err := ScanInstall(m)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if record != nil && record.Status == consts.AddonsInstallStatusOk {
|
||||
return gerror.New("插件已安装,无需重复操作!")
|
||||
}
|
||||
|
||||
data := g.Map{
|
||||
dao.SysAddonsInstall.Columns().Name: m.GetSkeleton().Name,
|
||||
dao.SysAddonsInstall.Columns().Version: m.GetSkeleton().Version,
|
||||
dao.SysAddonsInstall.Columns().Status: consts.AddonsInstallStatusOk,
|
||||
}
|
||||
return g.DB().Transaction(m.Ctx(), func(ctx context.Context, tx gdb.TX) error {
|
||||
if record != nil {
|
||||
_, _ = GetModel(ctx).Where(dao.SysAddonsInstall.Columns().Id, record.Id).Delete()
|
||||
}
|
||||
|
||||
if _, err = GetModel(ctx).Data(data).OmitEmptyData().Insert(); err != nil {
|
||||
return err
|
||||
}
|
||||
return m.Install(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
// Upgrade 更新模块
|
||||
func Upgrade(m Module) (err error) {
|
||||
record, err := ScanInstall(m)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if record == nil || record.Status != consts.AddonsInstallStatusOk {
|
||||
return gerror.New("插件未安装,请安装后操作!")
|
||||
}
|
||||
|
||||
data := g.Map{
|
||||
dao.SysAddonsInstall.Columns().Version: m.GetSkeleton().Version,
|
||||
}
|
||||
return g.DB().Transaction(m.Ctx(), func(ctx context.Context, tx gdb.TX) error {
|
||||
if _, err = GetModel(ctx).Where(dao.SysAddonsInstall.Columns().Id, record.Id).Data(data).Update(); err != nil {
|
||||
return err
|
||||
}
|
||||
return m.Upgrade(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
// UnInstall 卸载模块
|
||||
func UnInstall(m Module) (err error) {
|
||||
record, err := ScanInstall(m)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if record == nil || record.Status != consts.AddonsInstallStatusOk {
|
||||
return gerror.New("插件未安装,请安装后操作!")
|
||||
}
|
||||
|
||||
data := g.Map{
|
||||
dao.SysAddonsInstall.Columns().Version: m.GetSkeleton().Version,
|
||||
dao.SysAddonsInstall.Columns().Status: consts.AddonsInstallStatusUn,
|
||||
}
|
||||
return g.DB().Transaction(m.Ctx(), func(ctx context.Context, tx gdb.TX) error {
|
||||
if _, err = GetModel(ctx).Where(dao.SysAddonsInstall.Columns().Id, record.Id).Data(data).Update(); err != nil {
|
||||
return err
|
||||
}
|
||||
return m.UnInstall(ctx)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
// 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"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gres"
|
||||
"hotgo/internal/model/input/form"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Option 模块启动选项
|
||||
type Option struct {
|
||||
Server *ghttp.Server // http服务器
|
||||
// 更多选项参数
|
||||
// ..
|
||||
}
|
||||
|
||||
// Skeleton 模块骨架
|
||||
type Skeleton struct {
|
||||
Label string `json:"label"` // 标识
|
||||
Name string `json:"name"` // 名称
|
||||
Group int `json:"group"` // 分组
|
||||
Logo string `json:"logo"` // logo
|
||||
Brief string `json:"brief"` // 简介
|
||||
Description string `json:"description"` // 详细描述
|
||||
Author string `json:"author"` // 作者
|
||||
Version string `json:"version"` // 版本号
|
||||
RootPath string `json:"rootPath"` // 根路径
|
||||
}
|
||||
|
||||
func (s *Skeleton) GetModule() Module {
|
||||
return GetModule(s.Name)
|
||||
}
|
||||
|
||||
// Module 插件模块
|
||||
type Module interface {
|
||||
Start(option *Option) (err error) // 启动模块
|
||||
Stop() (err error) // 停止模块
|
||||
Ctx() context.Context // 上下文
|
||||
GetSkeleton() *Skeleton // 获取模块
|
||||
Install(ctx context.Context) (err error) // 安装模块
|
||||
Upgrade(ctx context.Context) (err error) // 更新模块
|
||||
UnInstall(ctx context.Context) (err error) // 卸载模块
|
||||
}
|
||||
|
||||
var (
|
||||
modules = make(map[string]Module)
|
||||
mLock sync.Mutex
|
||||
)
|
||||
|
||||
// StartModules 启动所有已安装模块
|
||||
func StartModules(ctx context.Context, option *Option) (err error) {
|
||||
for _, module := range filterInstalled() {
|
||||
if err = module.Start(option); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 为所有已安装模块设置静态资源路径
|
||||
AddStaticPath(ctx, option.Server)
|
||||
return
|
||||
}
|
||||
|
||||
// StopModules 停止所有已安装模块
|
||||
func StopModules(ctx context.Context) {
|
||||
for _, module := range filterInstalled() {
|
||||
if err := module.Stop(); err != nil {
|
||||
g.Log().Warningf(ctx, "StopModules err:%v, module:%v", err.Error(), module.GetSkeleton().Name)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// RegisterModule 注册模块
|
||||
func RegisterModule(m Module) Module {
|
||||
mLock.Lock()
|
||||
defer mLock.Unlock()
|
||||
name := m.GetSkeleton().Name
|
||||
_, ok := modules[name]
|
||||
if ok {
|
||||
panic("module repeat registration, name:" + name)
|
||||
}
|
||||
|
||||
sk := m.GetSkeleton()
|
||||
if sk == nil {
|
||||
panic("module skeleton not initialized, name:" + name)
|
||||
}
|
||||
|
||||
sk.RootPath = GetModulePath(name)
|
||||
modules[name] = m
|
||||
return m
|
||||
}
|
||||
|
||||
// GetModule 获取指定名称模块
|
||||
func GetModule(name string) Module {
|
||||
mLock.Lock()
|
||||
defer mLock.Unlock()
|
||||
m, ok := modules[name]
|
||||
if !ok {
|
||||
panic("implement not found for interface " + name + ", forgot register?")
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// GetSkeletons 获取所有模块骨架
|
||||
func GetSkeletons() (list []*Skeleton) {
|
||||
var keys []string
|
||||
for k := range modules {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, v := range keys {
|
||||
list = append(list, GetModule(v).GetSkeleton())
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
// GetModuleRealPath 获取指定模块绝对路径
|
||||
func GetModuleRealPath(name string) string {
|
||||
path := gfile.RealPath(GetModulePath(name))
|
||||
if path == "" {
|
||||
panic("no path is found. please confirm that the path " + GetModulePath(name) + " exists?")
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// AddStaticPath 设置插件静态目录映射
|
||||
func AddStaticPath(ctx context.Context, server *ghttp.Server) {
|
||||
basePath := GetResourcePath(ctx)
|
||||
if basePath == "" {
|
||||
return
|
||||
}
|
||||
|
||||
for _, module := range filterInstalled() {
|
||||
name := module.GetSkeleton().Name
|
||||
prefix, path := StaticPath(name, basePath)
|
||||
if !gres.Contains(path) {
|
||||
if _, err := gfile.Search(path); err != nil {
|
||||
g.Log().Warningf(ctx, `addons AddStaticPath failed: %v`, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
server.AddStaticPath(prefix, path)
|
||||
}
|
||||
}
|
||||
|
||||
// filterInstalled 过滤已安装模块
|
||||
func filterInstalled() []Module {
|
||||
var ms []Module
|
||||
for _, module := range modules {
|
||||
if IsInstall(module) {
|
||||
ms = append(ms, module)
|
||||
}
|
||||
}
|
||||
return ms
|
||||
}
|
||||
|
||||
// ModuleSelect 获取插件模块选项
|
||||
func ModuleSelect() form.Selects {
|
||||
sks := GetSkeletons()
|
||||
lst := make(form.Selects, 0)
|
||||
if len(sks) == 0 {
|
||||
return lst
|
||||
}
|
||||
|
||||
for _, skeleton := range sks {
|
||||
lst = append(lst, &form.Select{
|
||||
Value: skeleton.Name,
|
||||
Label: skeleton.Label,
|
||||
Name: skeleton.Label,
|
||||
})
|
||||
}
|
||||
return lst
|
||||
}
|
||||
Reference in New Issue
Block a user