Files
spothole/server/webserver.py
2025-09-28 17:35:54 +01:00

81 lines
2.9 KiB
Python

import json
import logging
from datetime import datetime
from threading import Thread
import bottle
import pytz
from bottle import run, response
from core.utils import serialize_everything
# Provides the public-facing web server.
class WebServer:
# Constructor
def __init__(self, spot_list, status_data, port):
self.last_page_access_time = None
self.last_api_access_time = None
self.spot_list = spot_list
self.status_data = status_data
self.port = port
self.thread = Thread(target=self.run)
self.thread.daemon = True
self.status = "Starting"
# Set up routing
bottle.get("/api/spots")(self.serve_api_spots)
bottle.get("/api/status")(self.serve_api_status)
bottle.get("/")(self.serve_index)
bottle.get("/<filepath:path>")(self.serve_static_file)
# Start the web server
def start(self):
self.thread.start()
# Run the web server itself. This blocks until the server is shut down, so it runs in a separate thread.
def run(self):
logging.info("Starting web server on port " + str(self.port) + "...")
self.status = "Waiting"
run(host='localhost', port=self.port)
# Main spots API
def serve_api_spots(self):
self.last_api_access_time = datetime.now(pytz.UTC)
self.status = "OK"
spots_json = json.dumps(self.get_spot_list_with_filters(bottle.request.query), default=serialize_everything)
response.content_type = 'application/json'
return spots_json
# Server status API
def serve_api_status(self):
self.last_api_access_time = datetime.now(pytz.UTC)
self.status = "OK"
status_json = json.dumps(self.status_data, default=serialize_everything)
response.content_type = 'application/json'
return status_json
# Serve the home page. This would be accessible as /index.html but we need this workaround to make it available as /
def serve_index(self):
self.last_page_access_time = datetime.now(pytz.UTC)
self.status = "OK"
return bottle.static_file("index.html", root="webassets")
# Serve general static files from "webassets" directory
def serve_static_file(self, filepath):
return bottle.static_file(filepath, root="webassets")
# Utility method to apply filters to the overall spot list and return only a subset. Enables query parameters in
# the main "spots" GET call. The "query" parameter should be the result of bottle's request.query, and is a MultiDict
def get_spot_list_with_filters(self, query):
spot_subset = self.spot_list[:]
for k in query.keys():
print(k + ": " + query.get(k))
return spot_subset
# Todo serve Server-Sent Events to frontend - see https://medium.com/@tdenton8772/streaming-api-design-using-python-and-javascript-1b0ce8adb703
# Todo serve apidocs