dazjsx
Version:
参考nestjs,基于KOA2的一款轻量级的后端开发框架
57 lines (56 loc) • 2.19 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StartFactory = void 0;
const koa_1 = __importDefault(require("koa"));
const koa_body_1 = __importDefault(require("koa-body"));
const export_factory_1 = require("./export.factory");
const middleware_1 = require("../middleware");
const service_1 = require("../service");
const typeorm_factory_1 = require("./typeorm.factory");
class StartFactory {
constructor(cn, option) {
this.otherMiddleware = [(0, koa_body_1.default)()];
this.app = new koa_1.default();
this.factory = new export_factory_1.DazExportFactory(cn);
this.option = option || {};
this.responseMiddleware = middleware_1.ResponseMiddleware;
this.initialize();
}
initialize() {
service_1.LogService.LEVELS = this.option.log || ['info', 'debug', 'error'];
}
use(middleware) {
this.otherMiddleware.push(middleware);
}
async start(...option) {
return new Promise(async (reslve) => {
typeorm_factory_1.TypeOrmFactory.createTypeOrmConnect(this.factory.getEntity(), (con) => {
this.routeMiddleware = this.factory.getRouters();
this.otherMiddleware.map((m) => {
this.app.use(m);
});
if (this.routeMiddleware) {
this.app.use(this.routeMiddleware.routes());
this.app.use(this.routeMiddleware.allowedMethods());
}
if (this.responseMiddleware) {
this.app.use(this.responseMiddleware);
}
this.app.listen(...option, () => reslve());
});
});
}
useBody(middleware) {
this.otherMiddleware.splice(0, 1, middleware);
}
useRoute(router) {
this.routeMiddleware = router;
}
useResponse(middleware) {
this.responseMiddleware = middleware;
}
}
exports.StartFactory = StartFactory;