-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressUtilities.ts
74 lines (70 loc) · 3.07 KB
/
ExpressUtilities.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
67
68
69
70
71
72
73
74
import { InitializationOptions } from "./@types/index.interfaces";
import { RedisRatelimiter } from "./ratelimiter/redis";
import { ControllersManager } from "./decorators/Controller/Manager";
import { getCurrentWorkerHealth } from './utils';
import "./prototypes/index";
import { MemoryRatelimiter } from "./ratelimiter/memory";
import compression from "compression";
export class ExpressUtilities {
ratelimiter?: MemoryRatelimiter | RedisRatelimiter;
options: InitializationOptions;
constructor(options: InitializationOptions) {
this.options = options;
this.printBanner();
if (options.ratelimiter) {
this.debug("Initalizing Ratelimiter module");
// Initialize ratelimiter according to options
if (this.options.ratelimiter?.type === "redis") {
this.ratelimiter = new RedisRatelimiter(
this.options.ratelimiter.options
);
} else {
this.ratelimiter = new MemoryRatelimiter(
this.options.ratelimiter?.options!
);
}
this.debug("Ratelimiter module initialized");
}
if (options.controllers) {
this.debug("Initalizing Controllers");
// Create a new controllers manager and load controllers
new ControllersManager({
controllers: options.controllers?.controllers!,
container: options.controllers?.container,
}).init(this.options.expressApp);
this.debug("Controllers initialized");
}
if (options.healthRoute) {
this.debug("Creating Health Route");
// Create a health route
this.options.expressApp.get(
options.healthRoute,
(req, res) => res.reply(200, getCurrentWorkerHealth())
);
this.debug("Health Route Created");
}
if (options.production) {
this.debug("Production mode enabled");
this.options.expressApp.use(compression());
}
}
public printBanner() {
console.log(`\x1b[35m
███████╗██╗░░░██╗████████╗██╗██╗░░░░░░██████╗
██╔════╝██║░░░██║╚══██╔══╝██║██║░░░░░██╔════╝
█████╗░░██║░░░██║░░░██║░░░██║██║░░░░░╚█████╗░
██╔══╝░░██║░░░██║░░░██║░░░██║██║░░░░░░╚═══██╗
███████╗╚██████╔╝░░░██║░░░██║███████╗██████╔╝
╚══════╝░╚═════╝░░░░╚═╝░░░╚═╝╚══════╝╚═════╝░\x1b[0m`)
console.log(`\x1b[37m
\x1b[33mAuthored By:\x1b[0m \x1b[2mGaurish Sethia (https://gaurishsethia.me)\x1b[0m
\x1b[33mVersion:\x1b[0m \x1b[2m1.0.0\x1b[0m
\x1b[0m`)
}
debug(message: string) {
if (!this.options.debugMode) return;
process.stdout.write(
`[\x1b[33mDEBUG\x1b[0m] \x1b[2m${new Date().toLocaleString()}\x1b[0m ${message}\n`
);
}
}