From b146d33aa0d244dff2dfb3dbab7a1cafefdcbbe9 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 19 Sep 2022 00:44:29 +0200 Subject: Start work on adding a custom /util route root in api --- src/api/Server.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/api/Server.ts') diff --git a/src/api/Server.ts b/src/api/Server.ts index e92335a5..d5ca50a0 100644 --- a/src/api/Server.ts +++ b/src/api/Server.ts @@ -1,6 +1,6 @@ -import { Config, getOrInitialiseDatabase, initEvent, registerRoutes } from "@fosscord/util"; +import { Config, getOrInitialiseDatabase, initEvent, registerRoutes, walk } from "@fosscord/util"; import { NextFunction, Request, Response, Router } from "express"; -import { Server, ServerOptions } from "lambert-server"; +import { Server, ServerOptions, traverseDirectory } from "lambert-server"; import morgan from "morgan"; import path from "path"; import { red } from "picocolors"; @@ -82,6 +82,24 @@ export class FosscordServer extends Server { app.use("/api/v9", api); app.use("/api", api); // allow unversioned requests + let utils = walk(path.join(__dirname, "routes-util")); + for (let file of utils) { + const fullPath = file; + file = file.replace(path.join(__dirname, "routes-util"), ""); + console.log(`[API] Registering util ${file}`); + if(file.endsWith(".js") || file.endsWith(".ts") && !file.endsWith(".web.js")) { + require(fullPath).default("/util/" + file.replace(".ts","").replace(".js", ""), app); + } + else { + app.use("/util/"+file, (req, res) => { + return res.sendFile(path.join(__dirname, "routes-util", file)); + }) + } + } + /*await traverseDirectory({dirname: }, (file) => { + app.use("/" + file, (file.endsWith(".ts") || file.endsWith(".js")) ? require(file) : (req, res) => res.status(404).json({ message: "404 endpoint not found", code: 0 })); + });*/ + this.app.use(ErrorHandler); TestClient(this.app); -- cgit 1.5.1