50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
|
||
|
|
from urllib.parse import urlparse, unquote
|
||
|
|
|
||
|
|
MAPS_DIR = "maps"
|
||
|
|
ALLOWED_EXT = {".png", ".jpg", ".jpeg", ".webp", ".gif"}
|
||
|
|
|
||
|
|
class Handler(SimpleHTTPRequestHandler):
|
||
|
|
def do_GET(self):
|
||
|
|
parsed = urlparse(self.path)
|
||
|
|
|
||
|
|
# API: list maps (used by index.html)
|
||
|
|
if parsed.path == "/list-maps":
|
||
|
|
self._handle_list_maps()
|
||
|
|
return
|
||
|
|
|
||
|
|
# Convenience: "/" -> "/index.html"
|
||
|
|
if parsed.path == "/":
|
||
|
|
self.path = "/index.html"
|
||
|
|
|
||
|
|
super().do_GET()
|
||
|
|
|
||
|
|
def _handle_list_maps(self):
|
||
|
|
maps = []
|
||
|
|
if os.path.isdir(MAPS_DIR):
|
||
|
|
for name in sorted(os.listdir(MAPS_DIR)):
|
||
|
|
p = os.path.join(MAPS_DIR, name)
|
||
|
|
ext = os.path.splitext(name)[1].lower()
|
||
|
|
if os.path.isfile(p) and ext in ALLOWED_EXT:
|
||
|
|
maps.append(f"{MAPS_DIR}/{name}")
|
||
|
|
|
||
|
|
data = json.dumps(maps).encode("utf-8")
|
||
|
|
self.send_response(200)
|
||
|
|
self.send_header("Content-Type", "application/json; charset=utf-8")
|
||
|
|
self.send_header("Content-Length", str(len(data)))
|
||
|
|
self.end_headers()
|
||
|
|
self.wfile.write(data)
|
||
|
|
|
||
|
|
def main():
|
||
|
|
host = "0.0.0.0"
|
||
|
|
port = 8000
|
||
|
|
httpd = ThreadingHTTPServer((host, port), Handler)
|
||
|
|
print(f"Serving on http://{host}:{port}")
|
||
|
|
httpd.serve_forever()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|