(self)
| 54 | |
| 55 | class Handler(BaseHTTPRequestHandler): |
| 56 | def do_GET(self): |
| 57 | parsed = urllib.parse.urlparse(self.path) |
| 58 | if parsed.path in ("/", "/index.html"): |
| 59 | self._html(base.build_index(tour)) |
| 60 | return |
| 61 | if parsed.path == "/game": |
| 62 | q = urllib.parse.parse_qs(parsed.query) |
| 63 | try: |
| 64 | idx = int(q["g"][0]) |
| 65 | game = tour.games[idx] |
| 66 | except (KeyError, ValueError, IndexError): |
| 67 | self.send_error(404, "no such game") |
| 68 | return |
| 69 | try: |
| 70 | self._html(render_game(game)) |
| 71 | except Exception as exc: # one bad game shouldn't take down the server |
| 72 | print(f"[replay] failed to render game {idx}: {type(exc).__name__}: {exc}") |
| 73 | self._html(error_page(game, exc), status=500) |
| 74 | return |
| 75 | self.send_error(404) |
| 76 | |
| 77 | def _html(self, page: str, status: int = 200): |
| 78 | body = page.encode() |
nothing calls this directly
no test coverage detected