-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·76 lines (62 loc) · 2.63 KB
/
server.py
File metadata and controls
executable file
·76 lines (62 loc) · 2.63 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
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""
Simple HTTP server for local testing of GitHub Pages site.
This solves CORS issues when opening HTML files directly.
Also handles SPA routing by serving index.html for client-side routes.
Usage:
python3 server.py
Then open: http://localhost:8000
"""
import http.server
import socketserver
import os
import sys
import urllib.parse
PORT = 8000
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def translate_path(self, path):
# Parse the path
parsed_path = urllib.parse.urlparse(path)
original_path = parsed_path.path
# Handle root path
if original_path == '/' or original_path == '':
return super().translate_path('/index.html')
# Check if it's a client-side route (starts with /article/)
if original_path.startswith('/article/'):
# Serve index.html for SPA routing
return super().translate_path('/index.html')
# Translate the path normally first
translated = super().translate_path(path)
# Check if the translated file exists
if os.path.exists(translated) and os.path.isfile(translated):
# File exists, return it
return translated
elif os.path.exists(translated) and os.path.isdir(translated):
# Directory exists, return it (will be handled by list_directory)
return translated
else:
# File doesn't exist - serve index.html for SPA fallback
return super().translate_path('/index.html')
def end_headers(self):
# Add CORS headers to allow local development
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
super().end_headers()
def log_message(self, format, *args):
# Custom log format
sys.stderr.write("%s - - [%s] %s\n" %
(self.address_string(),
self.log_date_time_string(),
format % args))
if __name__ == "__main__":
# Change to the directory where the script is located
os.chdir(os.path.dirname(os.path.abspath(__file__)))
with socketserver.TCPServer(("", PORT), MyHTTPRequestHandler) as httpd:
print(f"🚀 Server running at http://localhost:{PORT}/")
print(f"📁 Serving directory: {os.getcwd()}")
print("Press Ctrl+C to stop the server")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n👋 Server stopped")