-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
66 lines (54 loc) · 2.05 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import "reflect-metadata";
import dotenv from "dotenv";
import express, { Express } from "express";
import { Container } from "typedi";
import { useExpressServer, useContainer as routingContainer } from "routing-controllers";
import bodyParser from "body-parser";
import DatabaseMiddleware from "./middlewares/database.middleware";
class App {
private baseDirectory : string;
private expressApp : Express
// Middleware Instances
private databaseMiddleware : DatabaseMiddleware;
constructor() {
this.environmentDeterminer();
this.baseDirectory = __dirname;
this.expressApp = express();
routingContainer(Container);
useExpressServer(this.expressApp, {
routePrefix: process.env.MAMLAKHA_ROUTE_PREFIX,
defaultErrorHandler: false,
controllers: [this.baseDirectory + `/**/controllers/*.ts`]
});
this.expressApp.use(bodyParser.urlencoded({ extended: false }));
this.expressApp.use(bodyParser.json());
this.databaseMiddleware = new DatabaseMiddleware();
}
run() {
this.expressApp.listen(process.env.MAMLAKHA_REST_API_PORT, () => {
console.info(`Hello there, it's Mamlakha app and it's running on port ${process.env.MAMLAKHA_REST_API_PORT} 🤘`);
});
this.databaseMiddleware.connectToDatabase();
}
get expressAppInstance() : Express {
return this.expressApp;
}
private environmentDeterminer() {
const environment : string = process.env.NODE_ENV ?? "";
console.log(`Environment is`, environment);
switch (environment) {
case "development":
dotenv.config({ path: "./.env.development" });
break;
case "testing":
dotenv.config({ path: "./.env.testing" });
break;
default:
console.log("Defaulting to development");
dotenv.config({ path: "./.env.development" });
break;
}
}
}
const main : App = new App();
main.run();