First Commit. Duplicate bunext-sqlite preset

This commit is contained in:
2026-10-18 16:47:11 +01:00
commit 81e4a8aecc
718 changed files with 41695 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
import bunext from "@moduletrace/bunext";
import handleMediaServer from "./functions/server/media/handle-media-server";
import cron from "./cron";
import { websocket } from "./websocket";
import userAuth from "./functions/backend/auth/user-auth";
import type { WebSocketSession } from "./websocket/types";
import { logError, grabRequestIP } from "./functions/backend/logger";
declare global {}
const WEBSOCKET_ENDPOINT = "/ws";
const development = process.env.NODE_ENV == "development";
const port = process.env.PORT || 7227;
await bunext.bunextInit();
const server = Bun.serve({
async fetch(req, server) {
try {
const url = new URL(req.url);
if (url.pathname.startsWith("/media")) {
try {
return await handleMediaServer({ req });
} catch {
return new Response("Not Found", { status: 404 });
}
}
if (url.pathname === WEBSOCKET_ENDPOINT) {
const session = await userAuth({ req });
if (!session.user?.id) {
return new Response("Unauthorized", { status: 401 });
}
const upgraded = server.upgrade(req, {
data: {
user: session.user,
user_types: session.user_types ?? [],
} satisfies WebSocketSession,
});
if (!upgraded) {
return new Response("Upgrade failed", { status: 400 });
}
return;
}
return await bunext.bunextRequestHandler({ req, server });
} catch (error: any) {
await logError({
title: "Unhandled request error",
error,
metadata: { url: req.url, method: req.method },
ip_address: grabRequestIP(req),
}).catch(() => {});
return Response.json(
{ success: false, msg: "Internal Server Error" },
{ status: 500 },
);
}
},
websocket,
development,
port,
idleTimeout: development ? 80 : undefined,
});
bunext.bunextLog.info(
`Server running on http://${server.hostname}:${server.port} ...`,
);
cron();