Keyborg/server/main.ts

57 lines
1.5 KiB
TypeScript

import HttpRouter from "@lib/router.ts";
import { Eta } from "@eta-dev/eta";
import { serveFile } from "jsr:@std/http/file-server";
import rateLimitMiddleware from "@src/middleware/rateLimiter.ts";
import authMiddleware from "@src/middleware/auth.ts";
import { ok, ResultFromJSON } from "@shared/utils/result.ts";
import { ResultResponseFromJSON } from "@src/lib/context.ts";
import admin from "@src/lib/admin.ts";
import UsbipManager from "@shared/utils/usbip.ts";
import loggerMiddleware from "@src/middleware/logger.ts";
const router = new HttpRouter();
const views = Deno.cwd() + "/views/";
const eta = new Eta({ views });
router.use(loggerMiddleware);
router.use(rateLimitMiddleware);
router.use(authMiddleware);
const cache: Map<string, Response> = new Map();
router.get("/public/*", async (c) => {
const filePath = "." + c.path;
const cached = cache.get(filePath);
if (cached) {
return cached.clone();
}
const res = await serveFile(c.req, filePath);
cache.set(filePath, res.clone());
return res;
});
router
.get(["", "/index.html"], (c) => {
return c.html(eta.render("./index.html", {}));
})
.get(["/login", "/login.html"], (c) => {
return c.html(eta.render("./login.html", {}));
})
.post("/login", async (c) => {
const r = await ResultFromJSON<{ password: string }>(
await c.req.text(),
);
});
export default {
async fetch(req, connInfo) {
return await router.handleRequest(req, connInfo);
},
} satisfies Deno.ServeDefaultExport;