taevas.xyz/index.ts

80 lines
2.5 KiB
TypeScript
Raw Normal View History

import type { Server } from "bun";
import { parseArgs } from "util";
2025-04-03 15:16:59 +02:00
import { api } from "./api";
2025-03-06 22:18:15 +01:00
// PORT AND SSL STUFF
const { values } = parseArgs({args: Bun.argv, allowPositionals: true, options: {dev: {type: "boolean"}}});
const dev = values.dev ?? false;
console.log("Are we in development mode?", dev);
const cert = Bun.file(process.env["SSL_CERT"] ?? "./cert.pem");
const key = Bun.file(process.env["SSL_KEY"] ?? "./key.pem");
const ssl_available = await cert.exists() && await key.exists();
console.log("Are we able to use SSL?", ssl_available);
// const port = dev ? 8000 : ssl_available ? 443 : 80;
const tls = ssl_available ? {cert, key} : undefined;
const ports: number[] = [dev ? 8000 : 80];
if (!dev && tls) ports.push(443);
console.log("Therefore, we are opening ports on:", ports);
// ACTUAL CODE
const servers: Server[] = ports.map((port) => Bun.serve({
2025-03-07 17:43:41 +01:00
idleTimeout: 30,
tls: port !== 80 ? tls : undefined,
port,
2025-03-06 22:18:15 +01:00
fetch: async (req) => {
2025-03-30 16:17:49 +02:00
try {
const request_url = req.url.startsWith("/") ? "https://taevas.xyz".concat(req.url) : req.url;
const url = new URL(request_url);
const parameters = url.searchParams;
// merciless sanitization
let pathname = url.pathname;
pathname = pathname
.replace(/([^A-Za-z0-9/._-])/g, "")
.replace(/(?<![a-zA-Z])\.(?![a-zA-Z])/g, "");
if (req.method !== "GET") {
return new Response("Method Not Allowed", { status: 405 });
}
2025-03-06 22:18:15 +01:00
2025-03-30 16:17:49 +02:00
// MAIN PAGE
2025-03-06 22:18:15 +01:00
2025-03-30 16:17:49 +02:00
if (pathname === "/") {
const indexContent = await Bun.file("./dist/index.html").text();
return new Response(indexContent, {headers: {"Content-Type": "text/html"}});
}
2025-03-06 22:18:15 +01:00
2025-03-30 16:17:49 +02:00
// EXTERNAL TO MAIN PAGE
2025-03-06 22:18:15 +01:00
2025-03-30 16:17:49 +02:00
if (pathname.startsWith("/compressed")) {
const asset = Bun.file("./dist" + pathname);
return await asset.exists() ? new Response(asset, {status: 200}) : new Response("Not Found", {status: 404});
}
2025-03-06 22:18:15 +01:00
2025-03-30 16:17:49 +02:00
if (pathname.startsWith("/assets")) {
const asset = Bun.file("." + pathname);
return await asset.exists() ? new Response(asset, {status: 200}) : new Response("Not Found", {status: 404});
}
2025-03-06 22:18:15 +01:00
2025-03-30 16:17:49 +02:00
// API
2025-03-06 22:18:15 +01:00
2025-03-30 16:17:49 +02:00
if (pathname.startsWith("/api")) {
2025-04-03 15:16:59 +02:00
return await api(pathname, parameters);
2025-03-06 22:18:15 +01:00
}
2025-03-30 16:17:49 +02:00
return new Response("Not Found", {status: 404});
} catch(e) {
console.error("Returning a 500 because:\n", e);
return new Response("Internal Server Error", {status: 500});
}
2025-03-06 22:18:15 +01:00
},
}));
servers.forEach((server) => console.log(`Listening on ${server.hostname}:${server.port}`));
console.log("\n\n--------\n\n");