1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-21 18:02:33 +01:00

Start work on adding a custom /util route root in api

This commit is contained in:
TheArcaneBrony 2022-09-19 00:44:29 +02:00
parent b07e71a1f0
commit b146d33aa0
No known key found for this signature in database
GPG Key ID: 32FC5AAADAD75A22
10 changed files with 86 additions and 3 deletions

2
.gitignore vendored
View File

@ -32,3 +32,5 @@ yarn.lock
dbconf.json
migrations.db
package-lock.json

View File

@ -11,6 +11,6 @@
"quiet": false,
"steps": {
"pre": ["clean"],
"post": ["remap_imports"]
"post": ["util_resources", "remap_imports"]
}
}

View File

@ -0,0 +1,19 @@
const { execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const { argv, stdout, exit } = require("process");
const { execIn, parts, getDirs, walk, sanitizeVarName } = require("../utils");
module.exports = function (config) {
console.log(`==> Copying all util resources...`);
let utilRes = walk(path.join(config.srcDir, "api", "routes-util")).filter((x) => !x.endsWith(".ts"));
utilRes.forEach((x) => {
fs.copyFileSync(x, x.replace("src", "dist"));
});
console.log(`==> Cleaning util resources...`);
let dirt = walk(path.join(config.distDir, "api", "routes-util")).filter((x) => x.endsWith(".ts") || x.endsWith(".js.map") || x.endsWith(".d.ts"));
dirt.forEach((x) => {
fs.rmSync(x, { force: true });
});
};

View File

@ -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);

View File

@ -44,6 +44,7 @@ declare global {
export async function Authentication(req: Request, res: Response, next: NextFunction) {
if (req.method === "OPTIONS") return res.sendStatus(204);
if (req.url.startsWith("/util")) return next();
const url = req.url.replace(API_PREFIX, "");
if (url.startsWith("/invites") && req.method === "GET") return next();
if (

View File

@ -83,6 +83,7 @@ export default function TestClient(app: Application) {
res.send(fs.readFileSync(path.join(__dirname, "..", "..", "..", "assets", "developers.html"), { encoding: "utf8" }));
});
app.get("*", (req: Request, res: Response) => {
if (req.url.startsWith("/util")) return;
const { useTestClient } = Config.get().client;
res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
res.set("content-type", "text/html");

View File

View File

@ -0,0 +1,12 @@
import { Config, Release } from "@fosscord/util";
import { Request, Response, Router } from "express";
import { route } from "..";
export default (path: string, router: Router) => {
router.get(path, route({}), async (req: Request, res: Response) => {
router.get("/", route({}), async (req: Request, res: Response) => {
res.json({ok:true});
});
});
};

View File

@ -0,0 +1,29 @@
import fs from "fs";
import path from "path";
export function getDirs(dir: string) {
return fs.readdirSync(dir).filter((x) => {
try {
fs.readdirSync(path.join(dir, x));
return true;
} catch (e) {
return false;
}
});
}
export function walk(dir: string) {
let results: string[] = [];
let list = fs.readdirSync(dir);
list.forEach(function (file) {
file = dir + "/" + file;
let stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file));
} else {
results.push(file);
}
});
return results;
}

View File

@ -23,3 +23,4 @@ export * from "./Snowflake";
export * from "./String";
export * from "./Token";
export * from "./TraverseDirectory";
export * from "./Directory";