|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell (hyperpolymath) |
| 3 | + |
| 4 | +/** |
| 5 | + * Deno HTTP server for ECHIDNA Playground (echidna-playground). |
| 6 | + * Serves the compiled ReScript page, falling back to a build-required message. |
| 7 | + * Distinct from Server.res which is the Coq-Jr server in this same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +/** FFI: Deno.readFile */ |
| 11 | +@scope("Deno") @val |
| 12 | +external readFile: string => promise<Js.TypedArray2.Uint8Array.t> = "readFile" |
| 13 | + |
| 14 | +/** FFI: Deno.serve */ |
| 15 | +type serveOptions = {port: int} |
| 16 | +@scope("Deno") @val |
| 17 | +external denoServe: (serveOptions, Deno.request => promise<Deno.response>) => unit = "serve" |
| 18 | + |
| 19 | +/** FFI: console.log */ |
| 20 | +@scope("console") @val |
| 21 | +external log: string => unit = "log" |
| 22 | + |
| 23 | +/** MIME type mapping for static file serving */ |
| 24 | +let mimeTypes: Js.Dict.t<string> = Js.Dict.fromArray([ |
| 25 | + (".html", "text/html"), |
| 26 | + (".css", "text/css"), |
| 27 | + (".js", "application/javascript"), |
| 28 | + (".json", "application/json"), |
| 29 | + (".png", "image/png"), |
| 30 | + (".jpg", "image/jpeg"), |
| 31 | + (".jpeg", "image/jpeg"), |
| 32 | + (".gif", "image/gif"), |
| 33 | + (".svg", "image/svg+xml"), |
| 34 | + (".ico", "image/x-icon"), |
| 35 | + (".woff", "font/woff"), |
| 36 | + (".woff2", "font/woff2"), |
| 37 | +]) |
| 38 | + |
| 39 | +/** Extract file extension from a path */ |
| 40 | +let getExtension = (path: string): string => { |
| 41 | + let lastDot = Js.String2.lastIndexOf(path, ".") |
| 42 | + if lastDot >= 0 { |
| 43 | + Js.String2.substr(path, ~from=lastDot) |
| 44 | + } else { |
| 45 | + "" |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** Look up MIME type for a path */ |
| 50 | +let getMimeType = (path: string): string => { |
| 51 | + let ext = getExtension(path) |
| 52 | + switch Js.Dict.get(mimeTypes, ext) { |
| 53 | + | Some(mime) => mime |
| 54 | + | None => "application/octet-stream" |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** Attempt to serve a static file, returning None if not found */ |
| 59 | +let serveStaticFile = async (path: string): option<Deno.response> => { |
| 60 | + try { |
| 61 | + let bytes = await readFile(path) |
| 62 | + Some(Deno.makeResponseBytes(bytes, {"headers": {"content-type": getMimeType(path)}})) |
| 63 | + } catch { |
| 64 | + | _ => None |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** Fallback HTML when ReScript hasn't been compiled yet */ |
| 69 | +let buildRequiredHtml = ` |
| 70 | +<!DOCTYPE html> |
| 71 | +<html> |
| 72 | + <head> |
| 73 | + <meta charset="utf-8"> |
| 74 | + <title>ECHIDNA Playground - Build Required</title> |
| 75 | + <style> |
| 76 | + body { font-family: system-ui, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; } |
| 77 | + code { background: #f4f4f4; padding: 2px 6px; border-radius: 3px; } |
| 78 | + pre { background: #f4f4f4; padding: 15px; border-radius: 5px; overflow-x: auto; } |
| 79 | + </style> |
| 80 | + </head> |
| 81 | + <body> |
| 82 | + <h1>ECHIDNA Playground</h1> |
| 83 | + <p>The ReScript sources need to be compiled first.</p> |
| 84 | + <h2>Quick Start</h2> |
| 85 | + <pre><code>deno task build |
| 86 | +deno task serve</code></pre> |
| 87 | + </body> |
| 88 | +</html>` |
| 89 | + |
| 90 | +/** Try to load the compiled ReScript page renderer, falling back to static HTML */ |
| 91 | +let getPageHtml = (): string => { |
| 92 | + // In production, Main.res.js should provide the page content via Page.render() |
| 93 | + // If not available, return the build-required fallback |
| 94 | + try { |
| 95 | + Page.render() |
| 96 | + } catch { |
| 97 | + | _ => buildRequiredHtml |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** Server port */ |
| 102 | +let port = 8000 |
| 103 | + |
| 104 | +/** Request handler */ |
| 105 | +let handler = async (request: Deno.request): Deno.response => { |
| 106 | + let urlStr = Deno.getUrl(request) |
| 107 | + let url = Deno.makeUrl(urlStr) |
| 108 | + let pathname = url.pathname |
| 109 | + |
| 110 | + log(`${Deno.getMethod(request)} ${pathname}`) |
| 111 | + |
| 112 | + // Serve index page |
| 113 | + if pathname == "/" || pathname == "/index.html" { |
| 114 | + Deno.makeResponse(getPageHtml(), {"headers": {"content-type": "text/html; charset=utf-8"}}) |
| 115 | + } else { |
| 116 | + // Try to serve static files |
| 117 | + let staticPath = "." ++ pathname |
| 118 | + let staticResponse = await serveStaticFile(staticPath) |
| 119 | + switch staticResponse { |
| 120 | + | Some(response) => response |
| 121 | + | None => Deno.makeResponseWithStatus("Not Found", {"status": 404}) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** Start the playground server */ |
| 127 | +let start = () => { |
| 128 | + log(`ECHIDNA Playground server running at http://localhost:${Belt.Int.toString(port)}/`) |
| 129 | + denoServe({port: port}, handler) |
| 130 | +} |
| 131 | + |
| 132 | +// Auto-start when loaded |
| 133 | +let _ = start() |
0 commit comments