50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import express from "express";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
export default async function webserver() {
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
app.use(express.json());
|
|
|
|
const webDir = path.join(__dirname, "web");
|
|
if (fs.existsSync(webDir)) {
|
|
const files = fs
|
|
.readdirSync(webDir)
|
|
.filter((f) => f.endsWith(".ts") || f.endsWith(".js"));
|
|
for (const file of files) {
|
|
const mod = await import(path.join(webDir, file));
|
|
const handler = mod.default ?? mod;
|
|
if (typeof handler === "function") {
|
|
await handler(app);
|
|
console.log(`[WEB] Loaded web route: ${file}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`[WEB] Web server is running on port ${PORT}`);
|
|
});
|
|
|
|
const IgnoredPaths = ["/favicon.ico", "/robots.txt", "/", "/hello"];
|
|
const KnownPaths = ["/git-commit", "/", "/health"];
|
|
|
|
// log all incoming requests
|
|
app.use((req, res, next) => {
|
|
if (IgnoredPaths.includes(req.url)) {
|
|
return next();
|
|
}
|
|
if (!KnownPaths.includes(req.url)) {
|
|
console.warn(
|
|
`[WEB] Unknown Route request: ${req.method} ${req.url} {${req.ip}}`,
|
|
);
|
|
} else {
|
|
console.log(
|
|
`[WEB] Trusted Route request: ${req.method} ${req.url} {${req.ip}}`,
|
|
);
|
|
}
|
|
next();
|
|
});
|
|
}
|