mail-address-semantics/examples/statute/serve.py

38 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Serve a static MAS statute directory for local experiments.
python examples/statute/serve.py examples/statute/great-service.example --port 8080
This is deliberately not a statute server framework: it is a static file
server that labels `/.well-known/mas` as application/json, which
`mas discover` requires. Production deployments should use their normal web
server with an equivalent rule, e.g. for nginx:
location = /.well-known/mas { default_type application/json; }
"""
import argparse
import functools
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
class StatuteHandler(SimpleHTTPRequestHandler):
def guess_type(self, path):
if path.rstrip("/").endswith("/.well-known/mas"):
return "application/json"
return super().guess_type(path)
def make_server(root: str, host: str = "127.0.0.1", port: int = 8080) -> ThreadingHTTPServer:
return ThreadingHTTPServer((host, port), functools.partial(StatuteHandler, directory=root))
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("root")
ap.add_argument("--host", default="127.0.0.1")
ap.add_argument("--port", type=int, default=8080)
a = ap.parse_args()
print(f"serving {a.root} on http://{a.host}:{a.port}/.well-known/mas")
make_server(a.root, a.host, a.port).serve_forever()