Replace Python server with Guile

This commit is contained in:
2026-03-02 17:47:22 +00:00
parent 6c30d96e96
commit a0e3706db7
3 changed files with 48 additions and 22 deletions

View File

@@ -19,19 +19,18 @@ To run, first compile the WebAssembly module:
wat2wasm --enable-threads wipforth.wat
```
Then run the server:
Then run the development server:
```
python3 server.py
guile server.scm
```
You should then be able to open <http://localhost:8080> in a browser
and use the system from there.
**NOTE**: The server is just a very simple instantiation of Python's
built-in `http.server.HTTPServer`, configured to set the cross-origin
headers required for `SharedMemoryBuffer` use. You could use any HTTP
server that sets these headers.
**NOTE**: The server is very simple and just serves the files with the
cross-origin isolation headers required for `SharedMemoryBuffer` use.
You could use any HTTP server that sets these headers.
## Peripherals

View File

@@ -1,16 +0,0 @@
from http.server import HTTPServer, SimpleHTTPRequestHandler
import sys
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header(
'Cross-Origin-Opener-Policy', 'same-origin')
self.send_header(
'Cross-Origin-Embedder-Policy', 'require-corp')
self.send_header(
'Cache-Control', 'no-store, no-cache, must-revalidate')
super().end_headers()
if __name__ == '__main__':
server = HTTPServer(('localhost', 8080), CORSRequestHandler)
server.serve_forever()

43
server.scm Normal file
View File

@@ -0,0 +1,43 @@
(use-modules
(ice-9 binary-ports)
(web server)
(web request)
(web response)
(web uri))
(define mime-types
'(("html" . (text/html))
("css" . (text/css))
("js" . (application/javascript))
("wasm" . (application/wasm))
("f" . (text/plain))
("wat" . (text/plain))
("png" . (image/png))))
(define (mime-type path)
(let* ((dot (string-rindex path #\.))
(ext (when dot (substring path (1+ dot)))))
(or (assoc-ref mime-types ext) "application/octet-stream")))
(define headers
'((cross-origin-opener-policy . "same-origin")
(cross-origin-embedder-policy . "require-corp")
(cache-control . (no-store no-cache must-revalidate))))
(define (is-dir? path)
(eq? 'directory (stat:type (stat path))))
(define (request-path request)
(let ((path (string-append "." (uri-path (request-uri request)))))
(cond ((not (file-exists? path)) #nil)
((is-dir? path) (string-append path "index.html"))
(#t path))))
(define (file-handler request body)
(let ((path (request-path request)))
(if path
(values (cons `(content-type . ,(mime-type path)) headers)
(get-bytevector-all (open-input-file path)))
(values (build-response #:code 404) ""))))
(run-server file-handler)