mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-06-23 21:25:12 +00:00
General refactor to remove stuff being passed around the Tornado handlers when it doesn't need to be. Conditions provider presence is how handled on the JS side without needing special booleans sent to every page handler.
This commit is contained in:
@@ -11,12 +11,9 @@ from core.prometheus_metrics_handler import page_requests_counter
|
||||
class PageTemplateHandler(tornado.web.RequestHandler):
|
||||
"""Handler for all HTML pages generated from templates"""
|
||||
|
||||
def initialize(self, template_name, web_server_metrics, has_hamqsl=False, has_noaa_forecast=False, has_giro_ionosonde=False):
|
||||
def initialize(self, template_name, web_server_metrics):
|
||||
self._template_name = template_name
|
||||
self._web_server_metrics = web_server_metrics
|
||||
self._has_hamqsl = has_hamqsl
|
||||
self._has_noaa_forecast = has_noaa_forecast
|
||||
self._has_giro_ionosonde = has_giro_ionosonde
|
||||
|
||||
def get(self):
|
||||
# Metrics
|
||||
@@ -28,6 +25,4 @@ class PageTemplateHandler(tornado.web.RequestHandler):
|
||||
# Load named template, and provide variables used in templates
|
||||
self.render(self._template_name + ".html", software_version=SOFTWARE_VERSION,
|
||||
server_owner_callsign=SERVER_OWNER_CALLSIGN, allow_spotting=ALLOW_SPOTTING,
|
||||
web_ui_options=WEB_UI_OPTIONS, baseurl=BASE_URL, current_path=self.request.path,
|
||||
has_hamqsl=self._has_hamqsl, has_noaa_forecast=self._has_noaa_forecast,
|
||||
has_giro_ionosonde=self._has_giro_ionosonde)
|
||||
web_ui_options=WEB_UI_OPTIONS, baseurl=BASE_URL, current_path=self.request.path)
|
||||
@@ -5,12 +5,11 @@ import os
|
||||
import tornado
|
||||
from tornado.web import StaticFileHandler
|
||||
|
||||
from core.config import SERVER_OWNER_CALLSIGN, ALLOW_SPOTTING
|
||||
from core.constants import SOFTWARE_VERSION
|
||||
from core.config import ALLOW_SPOTTING, WEB_SERVER_PORT, API_ONLY_MODE
|
||||
from core.utils import empty_queue
|
||||
from server.handlers.api.addspot import APISpotHandler
|
||||
from server.handlers.api.dxstats import APIDxStatsHandler
|
||||
from server.handlers.api.alerts import APIAlertsHandler, APIAlertsStreamHandler
|
||||
from server.handlers.api.dxstats import APIDxStatsHandler
|
||||
from server.handlers.api.lookups import APILookupCallHandler, APILookupSIGRefHandler, APILookupGridHandler
|
||||
from server.handlers.api.options import APIOptionsHandler
|
||||
from server.handlers.api.solar_conditions import APISolarConditionsHandler
|
||||
@@ -23,7 +22,7 @@ from server.handlers.pagetemplate import PageTemplateHandler
|
||||
class WebServer:
|
||||
"""Provides the public-facing web server."""
|
||||
|
||||
def __init__(self, spots, alerts, solar_conditions, status_data, solar_condition_providers, port, api_only_mode=False):
|
||||
def __init__(self, spots, alerts, solar_conditions, status_data):
|
||||
"""Constructor"""
|
||||
|
||||
self._spots = spots
|
||||
@@ -32,9 +31,8 @@ class WebServer:
|
||||
self._sse_spot_queues = []
|
||||
self._sse_alert_queues = []
|
||||
self._status_data = status_data
|
||||
self._solar_condition_providers = solar_condition_providers
|
||||
self._port = port
|
||||
self._api_only_mode = api_only_mode
|
||||
self._port = WEB_SERVER_PORT
|
||||
self._api_only_mode = API_ONLY_MODE
|
||||
self._shutdown_event = asyncio.Event()
|
||||
self.web_server_metrics = {
|
||||
"last_page_access_time": None,
|
||||
@@ -57,66 +55,60 @@ class WebServer:
|
||||
async def _start_inner(self):
|
||||
"""Start method (async). Sets up the Tornado application."""
|
||||
|
||||
provider_classes = [type(p).__name__ for p in self._solar_condition_providers if p.enabled]
|
||||
has_hamqsl = "HamQSL" in provider_classes
|
||||
has_noaa_forecast = "NOAA3dayForecast" in provider_classes
|
||||
has_giro_ionosonde = "GIROIonosonde" in provider_classes or "KC2GProp" in provider_classes
|
||||
page_opts = {"web_server_metrics": self.web_server_metrics, "has_hamqsl": has_hamqsl,
|
||||
"has_noaa_forecast": has_noaa_forecast, "has_giro_ionosonde": has_giro_ionosonde}
|
||||
# Prepare a list of common arguments that are passed in to every API & page handler. This is just a basic thing
|
||||
# to avoid copy-pasting the same thing to every route declaration below.
|
||||
handler_opts = {"web_server_metrics": self.web_server_metrics}
|
||||
|
||||
# API endpoints are always enabled
|
||||
api_routes = [
|
||||
(r"/api/v1/spots", APISpotsHandler, {"spots": self._spots, "web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/alerts", APIAlertsHandler,
|
||||
{"alerts": self._alerts, "web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/spots", APISpotsHandler, {"spots": self._spots, **handler_opts}),
|
||||
(r"/api/v1/alerts", APIAlertsHandler, {"alerts": self._alerts, **handler_opts}),
|
||||
(r"/api/v1/spots/stream", APISpotsStreamHandler,
|
||||
{"sse_spot_queues": self._sse_spot_queues, "web_server_metrics": self.web_server_metrics}),
|
||||
{"sse_spot_queues": self._sse_spot_queues, **handler_opts}),
|
||||
(r"/api/v1/alerts/stream", APIAlertsStreamHandler,
|
||||
{"sse_alert_queues": self._sse_alert_queues, "web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/solar", APISolarConditionsHandler,
|
||||
{"solar_conditions": self._solar_conditions, "web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/dxstats", APIDxStatsHandler, {"spots": self._spots, "web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/options", APIOptionsHandler,
|
||||
{"status_data": self._status_data, "web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/status", APIStatusHandler,
|
||||
{"status_data": self._status_data, "web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/lookup/call", APILookupCallHandler, {"web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/lookup/sigref", APILookupSIGRefHandler, {"web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/lookup/grid", APILookupGridHandler, {"web_server_metrics": self.web_server_metrics}),
|
||||
(r"/api/v1/spot", APISpotHandler, {"spots": self._spots, "web_server_metrics": self.web_server_metrics}),
|
||||
{"sse_alert_queues": self._sse_alert_queues, **handler_opts}),
|
||||
(r"/api/v1/solar", APISolarConditionsHandler, {"solar_conditions": self._solar_conditions, **handler_opts}),
|
||||
(r"/api/v1/dxstats", APIDxStatsHandler, {"spots": self._spots, **handler_opts}),
|
||||
(r"/api/v1/options", APIOptionsHandler, {"status_data": self._status_data, **handler_opts}),
|
||||
(r"/api/v1/status", APIStatusHandler, {"status_data": self._status_data, **handler_opts}),
|
||||
(r"/api/v1/lookup/call", APILookupCallHandler, {**handler_opts}),
|
||||
(r"/api/v1/lookup/sigref", APILookupSIGRefHandler, {**handler_opts}),
|
||||
(r"/api/v1/lookup/grid", APILookupGridHandler, {**handler_opts}),
|
||||
(r"/api/v1/spot", APISpotHandler, {"spots": self._spots, **handler_opts}),
|
||||
]
|
||||
|
||||
# If in API-only mode, serve a basic homepage; in normal mode, serve the usual UI routes
|
||||
if self._api_only_mode:
|
||||
logging.info("API-only mode is enabled. Web UI will not be served.")
|
||||
ui_routes = [
|
||||
(r"/", PageTemplateHandler, {"template_name": "api_only_home", **page_opts})
|
||||
(r"/", PageTemplateHandler, {"template_name": "api_only_home", **handler_opts})
|
||||
]
|
||||
else:
|
||||
ui_routes = [
|
||||
(r"/", PageTemplateHandler, {"template_name": "spots", **page_opts}),
|
||||
(r"/map", PageTemplateHandler, {"template_name": "map", **page_opts}),
|
||||
(r"/bands", PageTemplateHandler, {"template_name": "bands", **page_opts}),
|
||||
(r"/alerts", PageTemplateHandler, {"template_name": "alerts", **page_opts}),
|
||||
(r"/conditions", PageTemplateHandler, {"template_name": "conditions", **page_opts}),
|
||||
(r"/status", PageTemplateHandler, {"template_name": "status", **page_opts}),
|
||||
(r"/about", PageTemplateHandler, {"template_name": "about", **page_opts})
|
||||
(r"/", PageTemplateHandler, {"template_name": "spots", **handler_opts}),
|
||||
(r"/map", PageTemplateHandler, {"template_name": "map", **handler_opts}),
|
||||
(r"/bands", PageTemplateHandler, {"template_name": "bands", **handler_opts}),
|
||||
(r"/alerts", PageTemplateHandler, {"template_name": "alerts", **handler_opts}),
|
||||
(r"/conditions", PageTemplateHandler, {"template_name": "conditions", **handler_opts}),
|
||||
(r"/status", PageTemplateHandler, {"template_name": "status", **handler_opts}),
|
||||
(r"/about", PageTemplateHandler, {"template_name": "about", **handler_opts})
|
||||
]
|
||||
# Only allow the Add Spot page if spotting is allowed
|
||||
if ALLOW_SPOTTING:
|
||||
ui_routes += [(r"/add-spot", PageTemplateHandler, {"template_name": "add_spot", **page_opts})]
|
||||
ui_routes += [(r"/add-spot", PageTemplateHandler, {"template_name": "add_spot", **handler_opts})]
|
||||
|
||||
# API docs, Prometheus metrics, and finally static assets are always available regardless of API-only mode.
|
||||
misc_routes = [
|
||||
(r"/apidocs", PageTemplateHandler, {"template_name": "apidocs", **page_opts}),
|
||||
(r"/apidocs", PageTemplateHandler, {"template_name": "apidocs", **handler_opts}),
|
||||
(r"/metrics", PrometheusMetricsHandler),
|
||||
(r"/(.*)", StaticFileHandler, {"path": os.path.join(os.path.dirname(__file__), "../webassets")})
|
||||
]
|
||||
|
||||
app = tornado.web.Application(api_routes + ui_routes + misc_routes,
|
||||
template_path=os.path.join(os.path.dirname(__file__), "../templates"),
|
||||
debug=False)
|
||||
template_path=os.path.join(os.path.dirname(__file__), "../templates"),
|
||||
debug=False)
|
||||
app.listen(self._port)
|
||||
logging.info("Web server running on port " + str(WEB_SERVER_PORT))
|
||||
await self._shutdown_event.wait()
|
||||
|
||||
def notify_new_spot(self, spot):
|
||||
|
||||
Reference in New Issue
Block a user