-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
92 lines (80 loc) · 2.56 KB
/
server.ts
File metadata and controls
92 lines (80 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { join } from "node:path";
import app from "./dist/server/server.js";
const DIST_CLIENT = join(import.meta.dir, "dist", "client");
const MIME_TYPES: Record<string, string> = {
".js": "application/javascript",
".css": "text/css",
".html": "text/html",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".svg": "image/svg+xml",
".ico": "image/x-icon",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".eot": "application/vnd.ms-fontobject",
".webp": "image/webp",
".avif": "image/avif",
".map": "application/json",
".txt": "text/plain",
".xml": "application/xml",
".webmanifest": "application/manifest+json",
};
function getMimeType(path: string): string {
const ext = path.slice(path.lastIndexOf("."));
return MIME_TYPES[ext] || "application/octet-stream";
}
const port = Number(process.env.PORT) || 3000;
Bun.serve({
port,
async fetch(request) {
const url = new URL(request.url);
const pathname = url.pathname;
// Serve static files from dist/client
if (pathname.startsWith("/assets/")) {
const filePath = join(DIST_CLIENT, pathname);
const file = Bun.file(filePath);
if (await file.exists()) {
return new Response(file, {
headers: {
"Content-Type": getMimeType(pathname),
"Cache-Control": "public, max-age=31536000, immutable",
},
});
}
}
// RFC 8414: OAuth 2.0 Authorization Server Metadata
if (pathname.startsWith("/.well-known/")) {
const { auth } = await import("./src/lib/auth.ts");
const { oauthProviderAuthServerMetadata, oauthProviderOpenIdConfigMetadata } =
await import("@better-auth/oauth-provider");
if (pathname.startsWith("/.well-known/oauth-authorization-server")) {
const handler = oauthProviderAuthServerMetadata(auth);
return await handler(request);
}
if (pathname.startsWith("/.well-known/openid-configuration")) {
const handler = oauthProviderOpenIdConfigMetadata(auth);
return await handler(request);
}
}
// Forward everything else to TanStack Start handler
return app.fetch(request);
},
});
console.log(`Lanyard server listening on http://localhost:${port}`);
// Start background services
import("./src/server/services/health-checker.ts").then(({ startHealthChecker }) => {
startHealthChecker();
}).catch((err) => {
console.warn("Failed to start health checker:", err);
});
import("./src/server/services/self-register.ts")
.then(({ registerLanyardAsService }) => {
registerLanyardAsService();
})
.catch((err) => {
console.warn("Failed to self-register:", err);
});