feat(frontend): Allow usage of glitch-soc as frontend (alpha)

This commit is contained in:
Jesse Wierzbinski 2024-04-15 00:46:19 -10:00
parent 1aacf7d743
commit ff6a91f916
No known key found for this signature in database
7 changed files with 81 additions and 1 deletions

View file

@ -0,0 +1,37 @@
import { dualLogger } from "@loggers";
import { errorResponse } from "@response";
import { config } from "config-manager";
import { join } from "node:path";
import {
LogLevel,
type LogManager,
type MultiLogManager,
} from "~packages/log-manager";
export const handleGlitchRequest = async (
req: Request,
logger: LogManager | MultiLogManager,
): Promise<Response> => {
const url = new URL(req.url);
let path = url.pathname;
// Strip leading /web from path
if (path.startsWith("/web")) path = path.slice(4);
// Redirect / to /index.html
if (path === "/" || path === "") path = "/index.html";
// If path doesn't have an extension (e.g. /about), serve index.html
// Also check if Accept header contains text/html
if (!path.includes(".") && req.headers.get("Accept")?.includes("text/html"))
path = "/index.html";
const file = Bun.file(join(config.frontend.glitch.assets, path));
if (await file.exists()) {
return new Response(file);
}
dualLogger.log(LogLevel.WARNING, "Glitch-Soc", `Asset not found: ${path}`);
return errorResponse("Glitch-Soc route not found", 404);
};

View file

@ -0,0 +1,6 @@
{
"name": "glitch-server",
"version": "0.0.0",
"main": "index.ts",
"dependencies": {}
}