-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
43 lines (34 loc) · 1.53 KB
/
Copy pathserver.py
File metadata and controls
43 lines (34 loc) · 1.53 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
#!/usr/bin/env python3
"""Single-port static server for the fish schooling simulation.
Usage: python3 server.py [port] (default 8000)
Serves three things from one port:
/ landing page linking to the two versions
/2d/ top-down 2D simulation (Canvas)
/3d/ 3D aquarium simulation (Three.js, vendored locally)
The simulation itself is 100% client-side; this only serves static files.
Any static file server works equally well.
"""
import http.server
import os
import sys
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
os.chdir(os.path.dirname(os.path.abspath(__file__)))
class Handler(http.server.SimpleHTTPRequestHandler):
# Correct MIME type for the vendored ES module, regardless of OS defaults.
extensions_map = {**http.server.SimpleHTTPRequestHandler.extensions_map,
".js": "text/javascript", ".mjs": "text/javascript"}
def send_head(self):
# Redirect the bare paths to their directory so relative imports
# (e.g. ./vendor/three...) resolve against /2d/ and /3d/.
if self.path in ("/2d", "/3d"):
self.send_response(301)
self.send_header("Location", self.path + "/")
self.end_headers()
return None
return super().send_head()
server = http.server.ThreadingHTTPServer(("", PORT), Handler)
print(f"Fish tank running at http://localhost:{PORT}")
print(f" landing http://localhost:{PORT}/")
print(f" 2D http://localhost:{PORT}/2d/")
print(f" 3D http://localhost:{PORT}/3d/")
server.serve_forever()