mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-21 14:57:42 +00:00
Autogenerated type safety parameterisation of all methods
This commit is contained in:
+14
-11
@@ -1,10 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
from typing import Any
|
||||
|
||||
import tornado
|
||||
from tornado.web import StaticFileHandler
|
||||
from tornado.web import RequestHandler, StaticFileHandler
|
||||
|
||||
from core.config import (
|
||||
ALLOW_SPOTTING,
|
||||
@@ -44,7 +47,7 @@ _HERE = os.path.dirname(__file__ or "")
|
||||
class WebServer:
|
||||
"""Provides the public-facing web server."""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
"""Constructor"""
|
||||
|
||||
self._data_store = DATA_STORE
|
||||
@@ -54,11 +57,11 @@ class WebServer:
|
||||
self._port = WEB_SERVER_PORT
|
||||
self._api_only_mode = API_ONLY_MODE
|
||||
self._shutdown_event = asyncio.Event()
|
||||
self._loop = None
|
||||
self._thread = None
|
||||
self._loop: asyncio.AbstractEventLoop | None = None
|
||||
self._thread: threading.Thread | None = None
|
||||
self.web_server_metrics = WebServerMetrics()
|
||||
|
||||
def setup(self):
|
||||
def setup(self) -> None:
|
||||
# Listen for new spots and alerts being added to the cache, so we can notify SSE clients immediately
|
||||
DATA_STORE.spots.add_listener(self._spot_broadcaster.publish)
|
||||
DATA_STORE.alerts.add_listener(self._alert_broadcaster.publish)
|
||||
@@ -69,13 +72,13 @@ class WebServer:
|
||||
|
||||
return self._spot_broadcaster.client_count + self._alert_broadcaster.client_count
|
||||
|
||||
def start(self):
|
||||
def start(self) -> None:
|
||||
"""Start the web server"""
|
||||
|
||||
self._thread = threading.Thread(target=asyncio.run, args=(self._start_inner(),), name="WebServer", daemon=True)
|
||||
self._thread.start()
|
||||
|
||||
def stop(self):
|
||||
def stop(self) -> None:
|
||||
"""Stop the web server"""
|
||||
|
||||
if self._loop and self._loop.is_running():
|
||||
@@ -85,7 +88,7 @@ class WebServer:
|
||||
if self._thread.is_alive():
|
||||
logger.warning("Web server background thread did not exit on time and will be killed.")
|
||||
|
||||
def _handle_loop_exception(self, loop, context):
|
||||
def _handle_loop_exception(self, loop: asyncio.AbstractEventLoop, context: dict[str, Any]) -> None:
|
||||
"""Ignore "cancelled" exceptions from the asyncio loop to avoid printing exceptions to the log on shutdown when
|
||||
we cancel the SSE handler threads"""
|
||||
|
||||
@@ -94,7 +97,7 @@ class WebServer:
|
||||
return
|
||||
loop.default_exception_handler(context)
|
||||
|
||||
async def _start_inner(self):
|
||||
async def _start_inner(self) -> None:
|
||||
"""Start method (async). Sets up the Tornado application."""
|
||||
|
||||
self._loop = asyncio.get_running_loop()
|
||||
@@ -154,7 +157,7 @@ class WebServer:
|
||||
APISpotHandler,
|
||||
{
|
||||
"spots": self._data_store.spots,
|
||||
"spot_providers": self._data_providers,
|
||||
"spot_providers": self._data_providers.spot_providers,
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -268,7 +271,7 @@ class WebServer:
|
||||
await self._shutdown_event.wait()
|
||||
|
||||
|
||||
def request_log(handler):
|
||||
def request_log(handler: RequestHandler) -> None:
|
||||
"""Custom log function to provide more data about requests when enabled, and to provide the ability to turn off
|
||||
web request logging altogetether. Also records the time of the request and status in the webserver metrics. Probably
|
||||
not what this method is supposed to be used for but it's a convenient thing that gets called on every request, so
|
||||
|
||||
Reference in New Issue
Block a user