-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
64 lines (52 loc) · 1.76 KB
/
Copy pathserve.py
File metadata and controls
64 lines (52 loc) · 1.76 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
#!/usr/bin/env python3
"""
Local web server for testing the site before deployment.
Usage:
python3 serve.py # Start server at localhost:8000
python3 serve.py --port 3000 # Start at custom port
"""
import sys
import os
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path
DEFAULT_PORT = 8000
HOST = "localhost"
class SiteHandler(SimpleHTTPRequestHandler):
"""Custom handler that serves index.html for directories."""
def end_headers(self):
"""Add cache control headers."""
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate")
self.send_header("Expires", "0")
super().end_headers()
def do_GET(self):
"""Serve index.html for directory requests."""
if self.path.endswith('/'):
self.path += 'index.html'
return super().do_GET()
def start_server(port=DEFAULT_PORT):
"""Start the local development server."""
server_address = (HOST, port)
httpd = HTTPServer(server_address, SiteHandler)
print(f"🚀 Starting local server...")
print(f" → http://{HOST}:{port}")
print(f" → Press Ctrl+C to stop")
print()
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n✓ Server stopped")
sys.exit(0)
if __name__ == "__main__":
port = DEFAULT_PORT
# Parse arguments
if len(sys.argv) > 1:
if sys.argv[1] == "--port" and len(sys.argv) > 2:
try:
port = int(sys.argv[2])
except ValueError:
print(f"ERROR: Invalid port {sys.argv[2]}")
sys.exit(1)
else:
print(f"Usage: python3 serve.py [--port PORT]")
sys.exit(1)
start_server(port)