mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2025-10-27 16:59:25 +00:00
Implement basic API server
This commit is contained in:
48
server/webserver.py
Normal file
48
server/webserver.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import json
|
||||
import logging
|
||||
from threading import Thread
|
||||
|
||||
import bottle
|
||||
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, port):
|
||||
self.spot_list = spot_list
|
||||
self.port = port
|
||||
self.thread = Thread(target=self.run)
|
||||
self.thread.daemon = True
|
||||
|
||||
# Set up routing
|
||||
bottle.get("/api/spots")(self.serve_api_spots)
|
||||
|
||||
# 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) + "...")
|
||||
run(host='localhost', port=self.port)
|
||||
|
||||
# Main spots API
|
||||
def serve_api_spots(self):
|
||||
spots_json = json.dumps(self.spot_list, default=serialize_everything)
|
||||
response.content_type = 'application/json'
|
||||
return spots_json
|
||||
|
||||
|
||||
# Todo spot API arguments e.g. "since" based on received_time of spots, sources, sigs, dx cont, dxcc, de cont, band, mode, filter out qrt, filter pre-qsy
|
||||
# Todo serve status API
|
||||
# Todo serve apidocs
|
||||
# Todo serve website
|
||||
|
||||
# Examples
|
||||
# @route('/download/<filename>')
|
||||
# def download(filename):
|
||||
# return static_file(filename, root='/path/to/static/files', download=f"download-{filename}")
|
||||
Reference in New Issue
Block a user