diff --git a/README.md b/README.md index b0d2354..6019088 100644 --- a/README.md +++ b/README.md @@ -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 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 diff --git a/server.py b/server.py deleted file mode 100644 index 001b78f..0000000 --- a/server.py +++ /dev/null @@ -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() diff --git a/server.scm b/server.scm new file mode 100644 index 0000000..c54589c --- /dev/null +++ b/server.scm @@ -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)