From a5c57b93630a19609eaf7ff7af9cf66083661e04 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 4 Jan 2021 16:14:19 +0100 Subject: :sparkles: update to use new lambert-server --- src/Server.ts | 64 ++++++++++++++--------------------------------------------- 1 file changed, 15 insertions(+), 49 deletions(-) (limited to 'src/Server.ts') diff --git a/src/Server.ts b/src/Server.ts index 92c73a35..39b1fa56 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -2,27 +2,26 @@ import express, { Application, Router } from "express"; import { traverseDirectory } from "./Utils"; import { Server as HTTPServer } from "http"; import fs from "fs/promises"; +import { Server, ServerOptions } from "lambert-server"; -export type ServerOptions = { - port: number; -}; +export interface DiscordServerOptions extends ServerOptions {} -export class Server { - private app: Application; - private http: HTTPServer; - private options: ServerOptions; - private routes: Router[]; - private initalized: Promise; - - constructor(opts: ServerOptions = { port: 8080 }) { - this.options = opts; +declare global { + namespace Express { + interface Request { + server: DiscordServer; + } + } +} - this.app = express(); +export class DiscordServer extends Server { + public options: DiscordServerOptions; - this.initalized = this.init(); + constructor(opts?: DiscordServerOptions) { + super(opts); } - async init() { + async start() { // recursively loads files in routes/ this.routes = await this.registerRoutes(__dirname + "/routes/"); // const indexHTML = await (await fetch("https://discord.com/app")).buffer(); @@ -33,39 +32,6 @@ export class Server { res.set("content-type", "text/html"); res.send(indexHTML); }); - } - - async start() { - await this.initalized; - await new Promise((res) => this.app.listen(this.options.port, () => res())); - console.log(`[Server] started on ${this.options.port}`); - } - - async registerRoutes(root: string) { - return await traverseDirectory({ dirname: root, recursive: true }, this.registerRoute.bind(this, root)); - } - - 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); - console.log(`[Server] Route ${path} registerd`); - return router; - } catch (error) { - console.error(new Error(`[Server] Failed to register route ${path}: ${error}`)); - } - } - - async stop() { - return new Promise((res) => this.http.close(() => res())); + return super.start(); } } -- cgit 1.5.1