koa-springboot
Version:
springboot-like koa
64 lines (63 loc) • 2.25 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const Koa = require("koa");
const scanner_1 = require("./scanner");
class Application {
constructor() {
const { initParams } = Application;
if (initParams && initParams.controllerPath) {
this.controllerPath = initParams.controllerPath;
}
else {
console.warn('controllerPath is not configured');
}
if (initParams && initParams.modelPath) {
this.modelPath = initParams.modelPath;
}
this.koaApp = new Koa();
this.initScanner();
if (scanner_1.default.modelPath) {
this.app.use((ctx, next) => __awaiter(this, void 0, void 0, function* () {
ctx.models = this.models;
yield next();
}));
}
this.init();
}
initScanner() {
scanner_1.default.controllerPath = this.controllerPath;
scanner_1.default.modelPath = this.modelPath;
this.scanner = new scanner_1.default();
this.scanner.scan();
}
get app() {
return this.koaApp;
}
get router() {
return this.scanner.router;
}
get models() {
return this.scanner.models;
}
init() {
this.app
.use(this.router.routes())
.use(this.router.allowedMethods());
}
start(port = 3000, mute = false) {
return this.app.listen(port, () => {
if (!mute)
console.log(`Server is running at http://localhost:${port}`);
});
}
}
Application.initParams = {};
exports.default = Application;