Replace Python server with Guile
This commit is contained in:
11
README.md
11
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 <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
|
||||
|
||||
|
||||
16
server.py
16
server.py
@@ -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
43
server.scm
Normal 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)
|
||||
Reference in New Issue
Block a user