feat: add code-flow base load
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
// admin 需要最后运行,并在route中进行过滤。
|
||||
import { Route } from '@abearxiong/router';
|
||||
import { router } from '../modules/router.ts';
|
||||
import { manager } from './dashboard/manager.ts';
|
||||
import { manager, updateNewCode, removeCode, stopCode } from './dashboard/manager.ts';
|
||||
import { loadOne } from './dashboard/load.ts';
|
||||
import { RouterCodeModel } from '../models/code.ts';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
export const getRouterList = new Route('admin', 'getRouterList');
|
||||
|
||||
getRouterList.run = async (ctx) => {
|
||||
ctx.body = router.getList().filter((r) => !r.path.startsWith('admin'));
|
||||
// ctx.body = router.getList().filter((r) => r.path.startsWith('admin'));
|
||||
ctx.body = router.getList().filter((r) => r.path.startsWith('admin'));
|
||||
return ctx;
|
||||
};
|
||||
|
||||
@@ -28,27 +31,56 @@ export const removeRouterById = new Route('admin', 'removeRouterById');
|
||||
removeRouterById.run = async (ctx) => {
|
||||
const { id } = ctx.query;
|
||||
router.removeById(id);
|
||||
removeCode(id);
|
||||
await RouterCodeModel.destroy({ where: { id } });
|
||||
ctx.body = 'success';
|
||||
return ctx;
|
||||
};
|
||||
router.add(removeRouterById);
|
||||
|
||||
// add router
|
||||
export const addRouter = new Route('admin', 'addRouter');
|
||||
|
||||
addRouter.run = async (ctx) => {
|
||||
const { path, key } = ctx.query;
|
||||
router.add(new Route(path, key));
|
||||
// stop router by id
|
||||
export const stopRouterById = new Route('admin', 'stopRouterById');
|
||||
stopRouterById.run = async (ctx) => {
|
||||
const { id } = ctx.query;
|
||||
router.removeById(id);
|
||||
const routerCode = await RouterCodeModel.findByPk(id);
|
||||
if (routerCode) {
|
||||
routerCode.active = false;
|
||||
await routerCode.save();
|
||||
}
|
||||
stopCode(id);
|
||||
ctx.body = 'success';
|
||||
return ctx;
|
||||
};
|
||||
router.add(addRouter);
|
||||
|
||||
// update router
|
||||
// add or update router
|
||||
export const updateRouter = new Route('admin', 'updateRouter');
|
||||
updateRouter.run = async (ctx) => {
|
||||
const { path, key } = ctx.query;
|
||||
router.add(new Route(path, key));
|
||||
let { path, key, id, code } = ctx.query;
|
||||
if (!path && !key) {
|
||||
ctx.body = 'path and key is required';
|
||||
ctx.code = 500;
|
||||
return ctx;
|
||||
}
|
||||
let codeRouter: RouterCodeModel | null = null;
|
||||
const codeRouteCheck = await RouterCodeModel.findOne({ where: { path, key } }); // 检查是否存在
|
||||
if (codeRouteCheck && codeRouteCheck.id !== id) {
|
||||
key = `${key}-${nanoid(6)}`;
|
||||
}
|
||||
if (id) {
|
||||
codeRouter = await RouterCodeModel.findByPk(id);
|
||||
codeRouter.path = path;
|
||||
codeRouter.key = key;
|
||||
codeRouter.code = code;
|
||||
await codeRouter.save();
|
||||
} else {
|
||||
const newCodeRouter = new RouterCodeModel({ path, key, code });
|
||||
await newCodeRouter.save();
|
||||
codeRouter = newCodeRouter;
|
||||
}
|
||||
|
||||
const codeOne = await loadOne(codeRouter);
|
||||
updateNewCode(codeOne);
|
||||
|
||||
ctx.body = 'success';
|
||||
return ctx;
|
||||
};
|
||||
@@ -57,7 +89,10 @@ router.add(updateRouter);
|
||||
// get manager status
|
||||
export const managerRouter = new Route('admin', 'getManagerStatus');
|
||||
managerRouter.run = async (ctx) => {
|
||||
ctx.body = manager.loaded;
|
||||
ctx.body = {
|
||||
status: manager.loaded,
|
||||
msg: 'system is running, and load manager success.',
|
||||
};
|
||||
return ctx;
|
||||
};
|
||||
router.add(managerRouter);
|
||||
@@ -68,4 +103,4 @@ managerList.run = async (ctx) => {
|
||||
ctx.body = manager.list;
|
||||
return ctx;
|
||||
};
|
||||
router.add(managerList);
|
||||
router.add(managerList);
|
||||
|
||||
Reference in New Issue
Block a user