code-center/src/admin/index.ts

107 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// admin 需要最后运行并在route中进行过滤。
import { Route } from '@abearxiong/router';
import { router } from '../modules/router.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'));
return ctx;
};
router.add(getRouterList);
// remove router
export const removeRouter = new Route('admin', 'removeRouter');
removeRouter.run = async (ctx) => {
const { path, key } = ctx.query;
router.remove({ path, key });
ctx.body = 'success';
return ctx;
};
router.add(removeRouter);
// remove router by id
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);
// 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;
};
// add or update router
export const updateRouter = new Route('admin', 'updateRouter');
updateRouter.run = async (ctx) => {
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;
};
router.add(updateRouter);
// get manager status
export const managerRouter = new Route('admin', 'getManagerStatus');
managerRouter.run = async (ctx) => {
ctx.body = {
status: manager.loaded,
msg: 'system is running, and load manager success.',
};
return ctx;
};
router.add(managerRouter);
// get manager list
export const managerList = new Route('admin', 'getManagerList');
managerList.run = async (ctx) => {
ctx.body = manager.list;
return ctx;
};
router.add(managerList);