feat: init base module
This commit is contained in:
45
src/admin/index.ts
Normal file
45
src/admin/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
// admin 需要最后运行,并在route中进行过滤。
|
||||
import { Route } from '@abearxiong/router';
|
||||
import { router } from '../modules/router.ts';
|
||||
|
||||
|
||||
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);
|
||||
|
||||
// add router
|
||||
export const addRouter = new Route('admin', 'addRouter');
|
||||
addRouter.run = async (ctx) => {
|
||||
const { path, key } = ctx.query;
|
||||
router.add(new Route(path, key));
|
||||
ctx.body = 'success';
|
||||
return ctx;
|
||||
}
|
||||
router.add(addRouter);
|
||||
|
||||
// update router
|
||||
export const updateRouter = new Route('admin', 'updateRouter');
|
||||
updateRouter.run = async (ctx) => {
|
||||
const { path, key } = ctx.query;
|
||||
// router.update(path, key);
|
||||
ctx.body = 'success';
|
||||
return ctx;
|
||||
}
|
||||
router.add(updateRouter);
|
||||
13
src/app.ts
Normal file
13
src/app.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { useConfig } from '@abearxiong/use-config';
|
||||
import { handleMessage } from './route.ts';
|
||||
import { server as routerServer } from './modules/router.ts';
|
||||
import http from 'http';
|
||||
|
||||
const config = useConfig();
|
||||
routerServer.setHandle(handleMessage);
|
||||
|
||||
const server = http.createServer(routerServer.callback());
|
||||
|
||||
server.listen(config.port, () => {
|
||||
console.log(`Server running at http://localhost:${config.port}/`);
|
||||
});
|
||||
5
src/modules/router.ts
Normal file
5
src/modules/router.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { QueryRouter, Server } from '@abearxiong/router';
|
||||
export const router = new QueryRouter();
|
||||
export const server = new Server({
|
||||
path: '/api/router',
|
||||
});
|
||||
19
src/route.ts
Normal file
19
src/route.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { router } from './modules/router.ts';
|
||||
|
||||
import './test/index.ts';
|
||||
import './admin/index.ts';
|
||||
|
||||
|
||||
type Message = {
|
||||
path: string;
|
||||
key?: string;
|
||||
};
|
||||
|
||||
export const handleMessage = async function (m: Message) {
|
||||
console.log('message', m);
|
||||
|
||||
const res = await router.parse(m);
|
||||
const { code, body, message } = res;
|
||||
console.log('response', res);
|
||||
return { code, data: body, message };
|
||||
};
|
||||
9
src/test/index.ts
Normal file
9
src/test/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { router } from '../modules/router.ts';
|
||||
import { Route } from '@abearxiong/router';
|
||||
|
||||
const getList = new Route('test', 'getList');
|
||||
getList.run = async (ctx) => {
|
||||
ctx.body = 'test';
|
||||
return ctx;
|
||||
};
|
||||
router.add(getList);
|
||||
Reference in New Issue
Block a user