44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { Elysia, status } from "elysia";
|
|
import { cors } from "@elysiajs/cors";
|
|
import { videoRoutes } from "./routes/videos";
|
|
import { initDb } from "./services/db";
|
|
import { join } from "path";
|
|
import { existsSync, statSync } from "fs";
|
|
|
|
initDb();
|
|
|
|
const PORT = parseInt(process.env.PORT || "3001");
|
|
const STATIC_DIR = process.env.STATIC_DIR || "./public";
|
|
|
|
const app = new Elysia()
|
|
.use(
|
|
cors({
|
|
exposeHeaders: ["Content-Range", "Accept-Ranges", "Content-Length"],
|
|
}),
|
|
)
|
|
.onError(({ error }) => {
|
|
console.error("Unhandled error:", error);
|
|
return status(500, { error: "Internal server error" });
|
|
})
|
|
.use(videoRoutes)
|
|
.get("/api/health", () => ({ status: "ok" }))
|
|
.get("*", ({ request }) => {
|
|
const url = new URL(request.url);
|
|
const pathname = url.pathname;
|
|
|
|
if (pathname.startsWith("/api")) return status(404, "Not found");
|
|
|
|
const filePath = join(STATIC_DIR, pathname);
|
|
if (existsSync(filePath) && statSync(filePath).isFile())
|
|
return Bun.file(filePath);
|
|
|
|
const indexPath = join(STATIC_DIR, "index.html");
|
|
if (existsSync(indexPath)) return Bun.file(indexPath);
|
|
|
|
return status(404, "Not found");
|
|
})
|
|
.listen(PORT);
|
|
|
|
console.log(`surf.nathan.rip running on port ${PORT}`);
|
|
console.log(`Serving static files from ${STATIC_DIR}`);
|