OhMyDitzzy commited on
Commit
97069a3
·
1 Parent(s): ec01417
Files changed (1) hide show
  1. src/server/router.ts +22 -22
src/server/router.ts CHANGED
@@ -1,6 +1,5 @@
1
  import { join } from "path";
2
 
3
- // Serve static files from dist/public
4
  const publicDir = join(process.cwd(), "dist", "public");
5
 
6
  export async function router(req: Request): Promise<Response> {
@@ -23,7 +22,6 @@ export async function router(req: Request): Promise<Response> {
23
  async function handleApiRequest(pathname: string): Promise<Response> {
24
  if (pathname.startsWith("/api/session/")) {
25
  const sessionId = pathname.split("/").pop();
26
- // TODO: add session validation logic if needed
27
  return new Response(JSON.stringify({ sessionId, valid: true }), {
28
  headers: { "Content-Type": "application/json" },
29
  });
@@ -35,32 +33,34 @@ async function handleApiRequest(pathname: string): Promise<Response> {
35
  });
36
  }
37
 
38
- function serveStatic(pathname: string): Response {
39
- try {
40
- let filePath = pathname === "/" ? "/index.html" : pathname;
41
-
42
- const fullPath = join(publicDir, filePath);
43
- const file = Bun.file(fullPath);
 
 
44
 
 
 
 
 
 
45
  return new Response(file, {
46
  headers: {
47
- "Content-Type": getContentType(filePath),
48
  },
49
  });
50
- } catch (error) {
51
- // If file not found, serve index.html for SPA routing
52
- try {
53
- const indexPath = join(publicDir, "index.html");
54
- const file = Bun.file(indexPath);
55
- return new Response(file, {
56
- headers: {
57
- "Content-Type": "text/html",
58
- },
59
- });
60
- } catch {
61
- return new Response("Not found", { status: 404 });
62
- }
63
  }
 
 
 
 
 
 
 
 
64
  }
65
 
66
  function getContentType(pathname: string): string {
 
1
  import { join } from "path";
2
 
 
3
  const publicDir = join(process.cwd(), "dist", "public");
4
 
5
  export async function router(req: Request): Promise<Response> {
 
22
  async function handleApiRequest(pathname: string): Promise<Response> {
23
  if (pathname.startsWith("/api/session/")) {
24
  const sessionId = pathname.split("/").pop();
 
25
  return new Response(JSON.stringify({ sessionId, valid: true }), {
26
  headers: { "Content-Type": "application/json" },
27
  });
 
33
  });
34
  }
35
 
36
+ async function serveStatic(pathname: string): Promise<Response> {
37
+ if (pathname === "/") {
38
+ const indexPath = join(publicDir, "index.html");
39
+ const file = Bun.file(indexPath);
40
+ return new Response(file, {
41
+ headers: { "Content-Type": "text/html" },
42
+ });
43
+ }
44
 
45
+ const fullPath = join(publicDir, pathname);
46
+ const file = Bun.file(fullPath);
47
+ const exists = await file.exists();
48
+
49
+ if (exists) {
50
  return new Response(file, {
51
  headers: {
52
+ "Content-Type": getContentType(pathname),
53
  },
54
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  }
56
+
57
+ const indexPath = join(publicDir, "index.html");
58
+ const indexFile = Bun.file(indexPath);
59
+ return new Response(indexFile, {
60
+ headers: {
61
+ "Content-Type": "text/html",
62
+ },
63
+ });
64
  }
65
 
66
  function getContentType(pathname: string): string {