-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve_spa.py
More file actions
23 lines (19 loc) · 910 Bytes
/
serve_spa.py
File metadata and controls
23 lines (19 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python3
"""Lightweight SPA static server. Serves files from dist/, falls back to index.html for Vue Router history mode."""
import http.server
import os
import sys
DIST = os.path.join(os.path.dirname(os.path.abspath(__file__)), "dist")
class SPAHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIST, **kwargs)
def do_GET(self):
path = self.translate_path(self.path)
if not os.path.exists(path) or (os.path.isdir(path) and not os.path.exists(os.path.join(path, "index.html"))):
self.path = "/index.html"
return super().do_GET()
if __name__ == "__main__":
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
server = http.server.HTTPServer(("0.0.0.0", port), SPAHandler)
print(f"Serving SPA from {DIST} on http://0.0.0.0:{port}")
server.serve_forever()