-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
executable file
·40 lines (28 loc) · 1.27 KB
/
serve.py
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
#!/usr/bin/env python3
import http.server
import socketserver
import os.path
address = ("0.0.0.0", 8080)
BUILD_DIR = "./build"
def is_assets_dir(path):
return path.startswith("/assets/") or path.startswith("assets/")
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory="./build", **kwargs)
# This is
# so our local server matches Cloudflare's practice of removing the .html
def translate_path(self, path):
"""Translate web requests (without .html) to their path on disk (with .html).
We want to do this because Cloudflare Pages does this, and it's nice to have a
local server which does this too so we can identify any incorrect links.
"""
# No need to do anything for /, the server already serves index.html
# If the requested path doesn't have .html, but the file exists with the
# .html suffix, then rewrite to serve that
if path != "/" and not path.endswith(".html"):
if os.path.isfile(BUILD_DIR + path + ".html"):
path += ".html"
return super().translate_path(path)
with socketserver.TCPServer(address, Handler) as httpd:
print(f"Serving at {address}")
httpd.serve_forever()