-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
98 lines (85 loc) · 2.59 KB
/
Copy pathserver.mjs
File metadata and controls
98 lines (85 loc) · 2.59 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
93
94
95
96
97
98
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2025 Coq-Jr Contributors
// Deno HTTP server for Coq-Jr (plain JS — TS types removed to satisfy
// the RSR antipattern check; this file is a thin Deno entrypoint that
// imports the compiled ReScript page renderer.)
const PORT = 8000;
// Import the compiled ReScript page renderer
// After running `npm run res:build`, the compiled JS will be available
let getPageHtml;
try {
const mainModule = await import("./src/Main.res.js");
getPageHtml = mainModule.getPageHtml;
} catch {
// Fallback if ReScript hasn't been compiled yet
getPageHtml = () => `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Coq-Jr - Build Required</title>
<style>
body { font-family: system-ui, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
code { background: #f4f4f4; padding: 2px 6px; border-radius: 3px; }
pre { background: #f4f4f4; padding: 15px; border-radius: 5px; overflow-x: auto; }
</style>
</head>
<body>
<h1>Coq-Jr</h1>
<p>The ReScript sources need to be compiled first.</p>
<h2>Quick Start</h2>
<pre><code>npm install
npm run res:build
deno task serve</code></pre>
</body>
</html>`;
}
const MIME_TYPES = {
".html": "text/html",
".css": "text/css",
".js": "application/javascript",
".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",
};
function getMimeType(path) {
const ext = path.substring(path.lastIndexOf("."));
return MIME_TYPES[ext] || "application/octet-stream";
}
async function serveStaticFile(path) {
try {
const file = await Deno.readFile(path);
return new Response(file, {
headers: { "content-type": getMimeType(path) },
});
} catch {
return null;
}
}
async function handler(request) {
const url = new URL(request.url);
let pathname = url.pathname;
console.log(`${request.method} ${pathname}`);
// Serve index page
if (pathname === "/" || pathname === "/index.html") {
return new Response(getPageHtml(), {
headers: { "content-type": "text/html; charset=utf-8" },
});
}
// Try to serve static files
const staticPath = `.${pathname}`;
const staticResponse = await serveStaticFile(staticPath);
if (staticResponse) {
return staticResponse;
}
// 404 for everything else
return new Response("Not Found", { status: 404 });
}
console.log(`Coq-Jr server running at http://localhost:${PORT}/`);
Deno.serve({ port: PORT }, handler);