web/ is a static site generator for the dataset, in Python's standard library only. `python3 web/build.py --out _site` writes a page for each of the 138 records, a finder on the home page with a map of the 85 lines, region and route indexes, and the repository's documents as pages. Each line is offered as the record's GeoJSON and as a GPX simplified to 5 m, with two archives of everything. The site's name and address are set in web/site.toml: Long Hikes, at https://longhikes.org. The dataset keeps its own name. netlify.toml and web/static/_headers configure the Netlify site (content types, download dispositions, caching, a content security policy with no inline script). It is deployed with the Netlify CLI from a local build, because Netlify cannot build from this forge. MapLibre GL JS and the two typefaces are vendored under web/assets/; map tiles come from OpenFreeMap. Tests: python3 -m unittest discover -s web/tests (164). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
c42cfbbe73
commit
abfbeb00bd
77 files changed
+9287
No files matched your search
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Serve the built site locally with the response headers from _site/_headers applied.
|
||||
|
||||
For checking pages under the production Content-Security-Policy (Netlify
|
||||
applies _headers in production; plain `python3 -m http.server` does not).
|
||||
|
||||
python3 web/serve.py [--dir _site] [--port 8000]
|
||||
"""
|
||||
import argparse
|
||||
import functools
|
||||
import http.server
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
def parse_headers(path):
|
||||
"""Netlify _headers file -> [(compiled path pattern, [(name, value), ...])]."""
|
||||
rules = []
|
||||
if not os.path.isfile(path):
|
||||
return rules
|
||||
with open(path, encoding="utf-8") as f:
|
||||
for line in f:
|
||||
if not line.strip() or line.lstrip().startswith("#"):
|
||||
continue
|
||||
if not line[0].isspace():
|
||||
pattern = "^" + re.escape(line.strip()).replace(r"\*", ".*") + "$"
|
||||
rules.append((re.compile(pattern), []))
|
||||
elif rules and ":" in line:
|
||||
name, value = line.strip().split(":", 1)
|
||||
rules[-1][1].append((name.strip(), value.strip()))
|
||||
return rules
|
||||
|
||||
|
||||
class Handler(http.server.SimpleHTTPRequestHandler):
|
||||
rules = []
|
||||
|
||||
def _rule_headers(self):
|
||||
path = self.path.split("?", 1)[0].split("#", 1)[0]
|
||||
out = {}
|
||||
for pattern, headers in self.rules:
|
||||
if pattern.match(path):
|
||||
for name, value in headers:
|
||||
out[name.lower()] = (name, value)
|
||||
return out
|
||||
|
||||
def send_header(self, keyword, value):
|
||||
# A Content-Type from _headers replaces the guessed one.
|
||||
if keyword.lower() == "content-type" and "content-type" in self._rule_headers():
|
||||
return
|
||||
super().send_header(keyword, value)
|
||||
|
||||
def end_headers(self):
|
||||
for name, value in self._rule_headers().values():
|
||||
super().send_header(name, value)
|
||||
super().end_headers()
|
||||
|
||||
def send_error(self, code, message=None, explain=None):
|
||||
page = os.path.join(self.directory, "404.html")
|
||||
if code == 404 and os.path.isfile(page):
|
||||
with open(page, "rb") as f:
|
||||
body = f.read()
|
||||
self.send_response(404)
|
||||
self.send_header("Content-Type", "text/html; charset=utf-8")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
if self.command != "HEAD":
|
||||
self.wfile.write(body)
|
||||
return
|
||||
super().send_error(code, message, explain)
|
||||
|
||||
|
||||
def main():
|
||||
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument("--dir", default=os.path.join(root, "_site"))
|
||||
ap.add_argument("--port", type=int, default=8000)
|
||||
args = ap.parse_args()
|
||||
Handler.rules = parse_headers(os.path.join(args.dir, "_headers"))
|
||||
handler = functools.partial(Handler, directory=args.dir)
|
||||
with http.server.ThreadingHTTPServer(("127.0.0.1", args.port), handler) as httpd:
|
||||
print(f"serving {args.dir} at http://127.0.0.1:{args.port}/ with {len(Handler.rules)} header rules")
|
||||
httpd.serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user