@scefira/dfw-nodejs
Version:
136 lines (114 loc) • 4.13 kB
text/typescript
import { DFWConfig, DFWScheme } from "../..";
import DatabaseManager, { Database } from "../modules/DatabaseManager";
import SessionManager from "../modules/SessionManager";
import APIManager, { DFWAPISchema } from "../modules/APIManager";
import SecurityManager, { DFWSecuritySchema } from "../modules/SecurityManager";
import cookieParser = require("cookie-parser");
import { AddressInfo } from "net";
import { Express, Response, Request, NextFunction , json as JSONMiddleware , urlencoded as URLEncodedMiddleware , static as StaticMiddleware } from "express";
export default class DFWInstance {
/**
* default DFW config
*/
public config: DFWConfig = {
server:{}
};
/**
*
*/
public modules:{
"APIManager" :APIManager,
"DatabaseManager" :DatabaseManager,
"SessionManager" :SessionManager,
"SecurityManager" :SecurityManager,
}
/**
*
*/
public server!: Express;
/**
* Initialices all dependencies and middlewares for DFW
*/
public constructor(config?: DFWConfig) {
this.config = Object.assign({},this.config,config?config:{});
this.modules = {
"APIManager" : new APIManager(this),
"DatabaseManager" : new DatabaseManager(this),
"SessionManager" : new SessionManager(this),
"SecurityManager" : new SecurityManager(this),
}
console.log("[DFW] Core loaded mode: " + process.env.NODE_ENV,this.config);
}
/**
* Initialices the express server
* @param port
* @param callback
*/
public startServer(port: number = process.env.NODE_ENV !== 'production' ? parseInt(process.env.PORT || '3000', 10) : 80, callback?: Function): Express {
if (this.server != undefined) {
return this.server;
}
var ExpressCore = require("express");
this.server = ExpressCore();
this.server.use(cookieParser()); // Cookie middleware needed
if(this.config.server !== undefined) {
switch(this.config.server.requestBody){
case "json":{
this.server.use(JSONMiddleware(this.config.server.requestBodyConfig));
break;
}
case "urlencoded":{
this.server.use(URLEncodedMiddleware(this.config.server.requestBodyConfig));
break;
}
case "form-data":{
// TODO
}
case "none":{
break;
}
default:{
this.server.use(JSONMiddleware());
this.server.use(URLEncodedMiddleware({ extended: true }));
break;
}
}
}
let listener = this.server.listen(port, () => {
console.log("[DFW] Express server running on port " + (listener.address() as AddressInfo).port);
if (callback != undefined && callback != null) {
callback();
}
});
return this.server;
}
/**
*
* @param req
* @param res
*/
public async touchAsync(req: Request, res: Response,next?: NextFunction) {
let dfw: DFWScheme = {
session: {} as any,
security: {} as DFWSecuritySchema,
api: {} as DFWAPISchema,
db: {} as any,
request: req,
response: res,
next:next,
DFWInstance: this,
}
var moduleKeys = Object.keys(this.modules);
for (var i = 0; i < moduleKeys.length; i++) {
await this.modules[moduleKeys[i]].touchAsync(dfw)
}
return dfw;
}
/**
*
* @param name
*/
public getDatabase(name: string = "default"): Database {
return this.modules.DatabaseManager.getDatabase(name);
}
}