|
1 | 1 | import { Database } from "jsr:@db/sqlite@0.12"; |
2 | 2 | import { gzipSync } from "node:zlib"; |
3 | 3 |
|
| 4 | +const MIME_TYPES: Record<string, string> = { |
| 5 | + ".css": "text/css", ".js": "application/javascript", ".html": "text/html", |
| 6 | + ".woff2": "font/woff2", ".svg": "image/svg+xml", ".webp": "image/webp", ".json": "application/json", |
| 7 | +}; |
| 8 | + |
| 9 | +// Pre-load static files |
| 10 | +const staticFiles: Record<string, { data: Uint8Array; contentType: string }> = {}; |
| 11 | +try { |
| 12 | + for (const entry of Deno.readDirSync("/data/static")) { |
| 13 | + if (!entry.isFile) continue; |
| 14 | + const data = Deno.readFileSync(`/data/static/${entry.name}`); |
| 15 | + const ext = entry.name.slice(entry.name.lastIndexOf(".")); |
| 16 | + staticFiles[entry.name] = { data, contentType: MIME_TYPES[ext] || "application/octet-stream" }; |
| 17 | + } |
| 18 | +} catch { /* static dir not available */ } |
| 19 | + |
4 | 20 | const datasetPath = Deno.env.get("DATASET_PATH") || "/data/dataset.json"; |
5 | 21 | let datasetItems: any[] | undefined; |
6 | 22 |
|
@@ -167,6 +183,17 @@ export default { |
167 | 183 | return new Response(String(size), { headers: PLAIN }); |
168 | 184 | } |
169 | 185 |
|
| 186 | + if (path.startsWith("/static/")) { |
| 187 | + const filename = path.slice(8); |
| 188 | + const sf = staticFiles[filename]; |
| 189 | + if (sf) { |
| 190 | + return new Response(sf.data, { |
| 191 | + headers: { "content-type": sf.contentType, "content-length": String(sf.data.length), "server": "deno" }, |
| 192 | + }); |
| 193 | + } |
| 194 | + return new Response("Not found", { status: 404 }); |
| 195 | + } |
| 196 | + |
170 | 197 | // /baseline11 |
171 | 198 | let sum = sumQuery(url, pathStart); |
172 | 199 | if (req.method === "POST") { |
|
0 commit comments