feat: add load types

This commit is contained in:
2024-11-10 21:12:45 +08:00
parent 2bbf3983c1
commit 696be2a28c
14 changed files with 356 additions and 217 deletions

View File

@@ -1,4 +1,6 @@
import { EventEmitter, once } from 'stream';
import stream from 'stream'; // 默认导入整个模块
const { once } = stream; // 从中解构出 EventEmitter
import { load, CodeManager, CodeStatus, loadOne } from './load.ts';
import { RouterCodeModel, TableIsExist } from '../models/code.ts';
import { emitter } from '../modules/event.ts';

View File

@@ -1,10 +1,11 @@
import { EventEmitter, once } from 'stream';
import stream from 'stream'; // 默认导入整个模块
const { EventEmitter, once } = stream; // 从中解构出 EventEmitter
// 事件
export const emitter = new EventEmitter();
type EmitterType = typeof emitter;
// 异步触发事件 demo
export const asyncEmit = (emitter: EventEmitter, eventName: string) => {
export const asyncEmit = (emitter: EmitterType, eventName: string) => {
return new Promise((resolve) => {
emitter.once(eventName, (value: any) => resolve(value));
});

View File

@@ -5,13 +5,14 @@ import { redisPublisher, redisSubscriber, redis } from './modules/redis.ts';
import { neode, getSession } from './modules/neo4j.ts';
import { minioClient } from './modules/minio.ts';
import { sequelize } from './modules/sequelize.ts';
useConfig();
export const emit = (channel: string, message?: any) => {
redisPublisher.publish(channel, JSON.stringify(message));
};
export { neode, getSession, redis, minioClient, sequelize };
export const app = new App<{ import: any; emit: typeof emit }>({
export const app = new App<{ import: any; emit: typeof emit; sequelize: typeof sequelize }>({
serverOptions: {
cors: {
origin: '*',
@@ -21,6 +22,7 @@ export const app = new App<{ import: any; emit: typeof emit }>({
routerContext: {
import: dynamicImport,
emit,
sequelize,
},
// routerHandle(res) {
// console.log('routerHandle', res.query);

View File

@@ -4,9 +4,10 @@ import './route.ts';
const config = useConfig();
import { app as aiApp } from '@kevisual/ai-lang/src/index.ts';
import { uploadMiddleware } from './lib/upload.ts';
//
import { loadApps } from './load-apps.ts';
export { aiApp };
export { app };
loadApps(app);
app.listen(config.port, () => {
console.log(`server is running at http://localhost:${config.port}`);
});

13
src/load-apps.ts Normal file
View File

@@ -0,0 +1,13 @@
import * as glob from 'glob';
import * as path from 'path';
const files = glob.sync(path.resolve(__dirname, './apps/**/index.cjs'));
export const loadApps = async (app: any) => {
for (const file of files) {
const module = __non_webpack_require__(file);
if (module.render) {
module.render(app);
}
}
};