summary refs log tree commit diff
path: root/src/Server.ts
blob: 7d93c4448a72c636ec02118974a865b6d715152e (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import express, { Application, Router, Request, Response, NextFunction } from "express";
import { MongoDatabase, Database } from "lambert-db";
import { Server as HTTPServer } from "http";
import { traverseDirectory } from "./Util";
import bodyParser from "body-parser";
import "express-async-errors";

const log = console.log;
console.log = (content) => {
	log(`[${new Date().toTimeString().split(" ")[0]}]`, content);
};

export type ServerOptions = {
	db: string;
	port: number;
	host: string;
};

declare global {
	namespace Express {
		interface Request {
			server: Server;
		}
	}
}

export class Server {
	app: Application;
	http: HTTPServer;
	db: Database;
	routes: Router[];
	options: ServerOptions;

	constructor(options: Partial<ServerOptions> = { port: 3000, host: "0.0.0.0" }) {
		this.app = express();
		this.db = new MongoDatabase(options?.db);
		this.options = options as ServerOptions;
	}

	async init() {
		await this.db.init();

		console.log("[Database] connected...");
		await new Promise((res, rej) => {
			this.http = this.app.listen(this.options.port, this.options.host, () => res(null));
		});
		this.routes = await this.registerRoutes(__dirname + "/routes/");
	}

	async registerRoutes(root: string) {
		this.app.use((req, res, next) => {
			req.server = this;
			next();
		});
		const routes = await traverseDirectory({ dirname: root, recursive: true }, this.registerRoute.bind(this, root));
		this.app.use((err: string | Error, req: Request, res: Response, next: NextFunction) => {
			res.status(400).send(err);
			next(err);
		});
		return routes;
	}

	registerRoute(root: string, file: string): any {
		if (root.endsWith("/") || root.endsWith("\\")) root = root.slice(0, -1); // removes slash at the end of the root dir
		let path = file.replace(root, ""); // remove root from path and
		path = path.split(".").slice(0, -1).join("."); // trancate .js/.ts file extension of path
		if (path.endsWith("/index")) path = path.slice(0, -6); // delete index from path

		try {
			var router = require(file);
			if (router.router) router = router.router;
			if (router.default) router = router.default;
			if (!router || router?.prototype?.constructor?.name !== "router")
				throw `File doesn't export any default router`;
			this.app.use(path, <Router>router);
			console.log(`[Routes] ${path} registerd`);

			return router;
		} catch (error) {
			console.error(new Error(`[Server] ¯\\_(ツ)_/¯ Failed to register route ${path}: ${error}`));
		}
	}

	async destroy() {
		await this.db.destroy();
		await new Promise((res, rej) => {
			this.http.close((err) => {
				if (err) return rej(err);
				return res("");
			});
		});
	}
}