-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
66 lines (52 loc) · 1.94 KB
/
Copy pathserver.py
File metadata and controls
66 lines (52 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""Local static server for the Rigs of Rods WebAssembly build.
Serves the `web/` directory and adds the cross-origin-isolation headers
(COOP/COEP) that the pthreads build needs for SharedArrayBuffer.
"""
import os
import sys
import http.server
import socketserver
PORT = 8000
WEB_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "web")
MIME = {
".html": "text/html; charset=utf-8",
".js": "text/javascript",
".css": "text/css",
".wasm": "application/wasm",
".data": "application/octet-stream",
".json": "application/json",
".png": "image/png",
".svg": "image/svg+xml",
}
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=WEB_DIR, **kwargs)
def end_headers(self):
# Cross-origin isolation -> enables SharedArrayBuffer (required by the
# -pthread build). Only meaningful over http(s); localhost works fine.
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
super().end_headers()
def guess_type(self, path):
ext = os.path.splitext(path)[1].lower()
if ext in MIME:
return MIME[ext]
return super().guess_type(path)
def log_message(self, fmt, *args):
sys.stdout.write("[%s] %s\n" % (self.log_date_time_string(), fmt % args))
class ThreadingServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
daemon_threads = True
allow_reuse_address = True
def main():
os.chdir(WEB_DIR)
print("Serving Rigs of Rods from: %s" % WEB_DIR)
print("Open: http://localhost:%d/" % PORT)
print("Press Ctrl+C to stop.")
with ThreadingServer(("127.0.0.1", PORT), Handler) as httpd:
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nStopped.")
if __name__ == "__main__":
main()