forked from gptme/gptme-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·56 lines (43 loc) · 1.88 KB
/
Copy pathserver.py
File metadata and controls
executable file
·56 lines (43 loc) · 1.88 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
#!/usr/bin/env python3
"""Simple HTTP server with SPA routing support.
Serves static files and falls back to index.html for all other routes,
enabling proper single-page application routing.
"""
import http.server
import socketserver
import os
from pathlib import Path
class SPAHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
"""HTTP request handler with SPA routing support."""
def do_GET(self):
"""Handle GET requests with SPA fallback."""
# Get the requested path
path = self.translate_path(self.path)
# If path is a directory, try index.html
if os.path.isdir(path):
index_path = os.path.join(path, 'index.html')
if os.path.exists(index_path):
path = index_path
# If file doesn't exist and it's not an asset, serve index.html
if not os.path.exists(path):
# Check if this looks like an asset request
is_asset = any(self.path.endswith(ext) for ext in
['.js', '.css', '.png', '.jpg', '.svg', '.ico', '.woff', '.woff2', '.ttf', '.eot', '.otf', '.json', '.webp', '.gif'])
if not is_asset:
# Serve index.html for SPA routes
self.path = '/index.html'
# Call parent handler
return http.server.SimpleHTTPRequestHandler.do_GET(self)
def run_server(port=5701, bind='0.0.0.0'):
"""Run the HTTP server."""
with socketserver.TCPServer((bind, port), SPAHTTPRequestHandler) as httpd:
print(f"Serving on {bind}:{port}")
print("SPA routing enabled - serving index.html for non-asset routes")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server...")
if __name__ == "__main__":
import sys
port = int(sys.argv[1]) if len(sys.argv) > 1 else 5701
run_server(port=port)