80 lines
3.1 KiB
Python
80 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Serve the project site and the small plain-text RouterOS update feed."""
|
|
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
from urllib.error import URLError
|
|
from urllib.request import Request, urlopen
|
|
from urllib.parse import urlsplit
|
|
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
SITE = HERE / "site"
|
|
if not SITE.is_dir(): # Running from the source tree: service/server.py.
|
|
SITE = HERE.parent / "site"
|
|
FEED = json.loads((SITE / "feed" / "channels.json").read_text())
|
|
RELEASES_URL = (
|
|
"https://code.brunner.ninja/api/v1/repos/feedc0de/mikrotik-olsr/releases?limit=10"
|
|
)
|
|
|
|
|
|
class Handler(SimpleHTTPRequestHandler):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, directory=str(SITE), **kwargs)
|
|
|
|
def send_bytes(self, status, body, content_type):
|
|
self.send_response(status)
|
|
self.send_header("Content-Type", content_type)
|
|
self.send_header("Content-Length", str(len(body)))
|
|
self.send_header("Cache-Control", "public, max-age=300")
|
|
self.end_headers()
|
|
if self.command != "HEAD":
|
|
self.wfile.write(body)
|
|
|
|
def do_HEAD(self):
|
|
self.do_GET()
|
|
|
|
def do_GET(self):
|
|
path = urlsplit(self.path).path
|
|
if path == "/healthz":
|
|
return self.send_bytes(200, b"ok\n", "text/plain; charset=utf-8")
|
|
if path == "/api/releases":
|
|
try:
|
|
req = Request(RELEASES_URL, headers={"User-Agent": "mikrotik-olsr-site"})
|
|
with urlopen(req, timeout=8) as response:
|
|
body = response.read()
|
|
json.loads(body)
|
|
return self.send_bytes(200, body, "application/json; charset=utf-8")
|
|
except (URLError, TimeoutError, ValueError) as exc:
|
|
return self.send_bytes(502, (str(exc) + "\n").encode(), "text/plain; charset=utf-8")
|
|
|
|
prefix = "/routeros/NEWESTa7."
|
|
if path.startswith(prefix):
|
|
channel = path[len(prefix):]
|
|
item = FEED.get(channel)
|
|
if item is None:
|
|
return self.send_bytes(200, b"No version available in this channel\n", "text/plain; charset=utf-8")
|
|
body = f"{item['version']} {item['epoch']}\n".encode("ascii")
|
|
return self.send_bytes(200, body, "text/plain; charset=utf-8")
|
|
|
|
marker = "/CHANGELOG"
|
|
if path.startswith("/routeros/") and path.endswith(marker):
|
|
version = path[len("/routeros/"):-len(marker)].strip("/")
|
|
if version and all(ch.isdigit() or ch == "." for ch in version):
|
|
changelog = SITE / "feed" / "changelogs" / f"{version}.txt"
|
|
if changelog.is_file():
|
|
return self.send_bytes(200, changelog.read_bytes(), "text/plain; charset=utf-8")
|
|
return self.send_bytes(404, b"Unknown RouterOS changelog\n", "text/plain; charset=utf-8")
|
|
|
|
return super().do_GET()
|
|
|
|
|
|
def main():
|
|
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
|
|
ThreadingHTTPServer(("0.0.0.0", port), Handler).serve_forever()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|