From 5c598f91e6726820f42c391108df8d247ad9d083 Mon Sep 17 00:00:00 2001 From: Ian Renton Date: Sat, 20 Jun 2026 12:17:13 +0100 Subject: [PATCH] Pass through more info from nginx and log it, to help me figure out who's using Spothole and contact them about upcoming API changes. Also an enabler for #101. --- README.md | 48 ++++++++++++++++++++++++++++++++++----- server/webserver.py | 22 +++++++++++++++++- templates/about.html | 2 +- templates/add_spot.html | 2 +- templates/alerts.html | 2 +- templates/bands.html | 4 ++-- templates/base.html | 10 ++++---- templates/conditions.html | 2 +- templates/map.html | 4 ++-- templates/spots.html | 4 ++-- templates/status.html | 2 +- 11 files changed, 79 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 826e4ea..4c15eff 100644 --- a/README.md +++ b/README.md @@ -310,44 +310,80 @@ server { # SSE endpoints location ~ ^/api/v1/(spots|alerts)/stream { + proxy_pass http://127.0.0.1:8080; + + # Allow keep-alive proxy_http_version 1.1; proxy_set_header Connection ""; - proxy_pass http://127.0.0.1:8080; + + # Set correct content type for SSE API calls + add_header Content-Type text/event-stream always; + + # Set remove buffering, remove caching, add suitable timeouts for SSE API calls proxy_buffering off; proxy_cache off; proxy_read_timeout 24h; proxy_connect_timeout 10s; proxy_send_timeout 24h; proxy_set_header X-Accel-Buffering no; + add_header Cache-Control no-store always; + + # Allow cross-origin requests to API proxy_hide_header Access-Control-Allow-Origin; add_header Access-Control-Allow-Origin * always; - add_header Cache-Control no-store always; - add_header Content-Type text/event-stream always; + + # Pass on IP address and host information to Spothole, in case logging this information is required + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_set_header X-Forwarded-Proto $scheme; } # Other API endpoints location /api/ { + proxy_pass http://127.0.0.1:8080; + + # Allow keep-alive proxy_http_version 1.1; proxy_set_header Connection ""; - proxy_pass http://127.0.0.1:8080; + + # Set up buffering, remove caching, add suitable timeouts for API calls proxy_buffering on; proxy_cache off; proxy_read_timeout 30s; proxy_connect_timeout 10s; + add_header Cache-Control no-store always; + + # Allow cross-origin requests to API proxy_hide_header Access-Control-Allow-Origin; add_header Access-Control-Allow-Origin * always; - add_header Cache-Control no-store always; + + # Pass on IP address and host information to Spothole, in case logging this information is required + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_set_header X-Forwarded-Proto $scheme; } # Static assets location / { + proxy_pass http://127.0.0.1:8080; + + # Allow keep-alive proxy_http_version 1.1; proxy_set_header Connection ""; - proxy_pass http://127.0.0.1:8080; + + # Set up buffering and caching, add suitable timeouts for static asset requests proxy_buffering on; proxy_read_timeout 30s; proxy_connect_timeout 10s; add_header Cache-Control "public, max-age=3600, must-revalidate" always; + + # Pass on IP address and host information to Spothole, in case logging this information is required + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_set_header X-Forwarded-Proto $scheme; } } ``` diff --git a/server/webserver.py b/server/webserver.py index be52bba..bf846ec 100644 --- a/server/webserver.py +++ b/server/webserver.py @@ -108,8 +108,9 @@ class WebServer: app = tornado.web.Application(api_routes + ui_routes + misc_routes, template_path=os.path.join(_HERE, "../templates"), + log_function=request_log, debug=False) - app.listen(self._port) + app.listen(self._port, xheaders=True) logging.info("Web server running on port " + str(WEB_SERVER_PORT)) await self._shutdown_event.wait() @@ -162,3 +163,22 @@ class WebServer: # Probably got deleted already on another thread pass pass + +def request_log(handler): + """Custom log function to provide more data about requests.""" + + if handler.get_status() < 500: + log_method = logging.info + else: + log_method = logging.warning + + request = handler.request + client_ip = request.remote_ip + referrer = request.headers.get("Referer", "-") + user_agent = request.headers.get("User-Agent", "-") + + log_method( + f'{client_ip} - "{request.method} {request.uri}" ' + f'{handler.get_status()} {request.request_time():.2f}ms | ' + f'Ref: {referrer} | UA: {user_agent}' + ) diff --git a/templates/about.html b/templates/about.html index f32b775..db9a44f 100644 --- a/templates/about.html +++ b/templates/about.html @@ -13,7 +13,7 @@ href="/apidocs">API documentation. The API delivers spots in a consistent format regardless of the data source, freeing developers from needing to know how each individual data source presents its data.

Spothole itself is also open source, Public Domain licenced code that anyone can take and modify. The source code is here.

+ href="https://git.ianrenton.com/ian/spothole/">The source code is here.

The software was written by Ian Renton, MØTRT and other contributors. Full details are available in the README file.

diff --git a/templates/add_spot.html b/templates/add_spot.html index 05740bf..996063c 100644 --- a/templates/add_spot.html +++ b/templates/add_spot.html @@ -76,7 +76,7 @@ - + diff --git a/templates/alerts.html b/templates/alerts.html index e9d1a48..f34e327 100644 --- a/templates/alerts.html +++ b/templates/alerts.html @@ -75,7 +75,7 @@ - + diff --git a/templates/bands.html b/templates/bands.html index 2eb7a3c..7694ad9 100644 --- a/templates/bands.html +++ b/templates/bands.html @@ -77,8 +77,8 @@ - - + + diff --git a/templates/base.html b/templates/base.html index d3af46c..f456c08 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,6 +1,6 @@ {% extends "skeleton.html" %} {% block head_extra %} - + @@ -10,10 +10,10 @@ - - - - + + + + {% end %} {% block body %}
diff --git a/templates/conditions.html b/templates/conditions.html index 9b29caa..8bc92db 100644 --- a/templates/conditions.html +++ b/templates/conditions.html @@ -284,7 +284,7 @@
- + diff --git a/templates/map.html b/templates/map.html index bb4c0e2..b59ead8 100644 --- a/templates/map.html +++ b/templates/map.html @@ -95,8 +95,8 @@ - - + + diff --git a/templates/spots.html b/templates/spots.html index 25930a9..c59face 100644 --- a/templates/spots.html +++ b/templates/spots.html @@ -116,8 +116,8 @@ - - + + diff --git a/templates/status.html b/templates/status.html index 1afd393..d666c91 100644 --- a/templates/status.html +++ b/templates/status.html @@ -59,7 +59,7 @@ - +