diff --git a/README.md b/README.md index 40da6e2..15c9c2d 100644 --- a/README.md +++ b/README.md @@ -318,93 +318,78 @@ To set up nginx as a reverse proxy that sits in front of Spothole, first ensure Create a file at `/etc/nginx/sites-available/` called `spothole`. Give it the following contents, replacing `spothole.app` with the domain name on which you want to run Spothole. If you changed the port on which Spothole runs, -update that on the "proxy_pass" line too. +update that on the "proxy_pass" line, and if you installed Spothole somewhere other than `/home/spothole/spothole`, +adjust the alias location for serving static files. + +(The latter section, configuring the nginx server to serve static files directly, improves efficiency because it saves +Spothole itself from serving JS, CSS etc. files. If you can't do this for some reason, e.g. your nginx and spothole are +on different computers, you can omit the `location /static/ {}` block.) ```nginx server { server_name spothole.app; + # Global proxy settings + proxy_http_version 1.1; + proxy_set_header Connection ""; + proxy_connect_timeout 10s; + proxy_buffering on; + + # 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; + # Wellknown area for Lets Encrypt location /.well-known/ { alias /var/www/html/.well-known/; } + + # Load static assets directly from the Spothole webassets directory + location /static/ { + alias /home/spothole/spothole/webassets/; + expires 1h; + add_header Cache-Control "public, max-age=3600, must-revalidate"; + } # SSE endpoints - location ~ ^/api/v1/(spots|alerts)/stream { + 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 ""; - - # 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 + # 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; - - # 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; + add_header Access-Control-Allow-Origin * always; } # 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 ""; - - # Set up buffering, remove caching, add suitable timeouts for API calls - proxy_buffering on; + # Remove buffering, remove caching, add suitable timeouts for API calls + proxy_buffering off; 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; - - # 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 + # Templated pages location / { proxy_pass http://127.0.0.1:8080; - - # Allow keep-alive - proxy_http_version 1.1; - proxy_set_header Connection ""; - - # 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; + add_header Cache-Control "no-cache, must-revalidate" always; } } ``` @@ -467,7 +452,7 @@ To navigate your way around the source code, this list may help. *HTML/JS/CSS front-end code* -* `/webassets` - Root for static files served by the web server +* `/webassets` - Root for static files served by the web server. These are all served from a path starting `/static/`. * `/webassets/apidocs` - Contains the OpenAPI spec (`openapi.yml`) * `/webassets/css` - CSS files used by the web front-end * `/webassets/img` - image files used by the web front-end diff --git a/server/handlers/manifesthandler.py b/server/handlers/manifesthandler.py new file mode 100644 index 0000000..978b090 --- /dev/null +++ b/server/handlers/manifesthandler.py @@ -0,0 +1,11 @@ +import tornado.web + +from core.config import BASE_URL + + +class ManifestHandler(tornado.web.RequestHandler): + """Handler for manifest.webmanifest, which needs BASE_URL inserted and a custom content-type""" + + def get(self): + self.set_header("Content-Type", "application/manifest+json; charset=UTF-8") + self.render("manifest.webmanifest", baseurl=BASE_URL) diff --git a/server/webserver.py b/server/webserver.py index a23ea73..21228cd 100644 --- a/server/webserver.py +++ b/server/webserver.py @@ -15,6 +15,7 @@ from server.handlers.api.options import APIOptionsHandler from server.handlers.api.solar_conditions import APISolarConditionsHandler from server.handlers.api.spots import APISpotsHandler, APISpotsStreamHandler from server.handlers.api.status import APIStatusHandler +from server.handlers.manifesthandler import ManifestHandler from server.handlers.metrics import PrometheusMetricsHandler from server.handlers.pagetemplate import PageTemplateHandler from server.handlers.quietstaticfilehandler import QuietStaticFileHandler @@ -100,11 +101,12 @@ class WebServer: if ALLOW_SPOTTING: 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. + # API docs, Prometheus metrics, webapp manifest and static assets are always available regardless of API-only mode. misc_routes = [ (r"/apidocs", PageTemplateHandler, {"template_name": "apidocs", **handler_opts}), (r"/metrics", PrometheusMetricsHandler), - (r"/(.*)", QuietStaticFileHandler, {"path": os.path.join(_HERE, "../webassets")}) + (r"/manifest.webmanifest", ManifestHandler), + (r"/static/(.*)", QuietStaticFileHandler, {"path": os.path.join(_HERE, "../webassets")}) ] app = tornado.web.Application(api_routes + ui_routes + misc_routes, diff --git a/templates/add_spot.html b/templates/add_spot.html index 71ff126..c164a6b 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 653a089..832345b 100644 --- a/templates/alerts.html +++ b/templates/alerts.html @@ -75,7 +75,7 @@ - + diff --git a/templates/api_only_home.html b/templates/api_only_home.html index 97139c9..11ed393 100644 --- a/templates/api_only_home.html +++ b/templates/api_only_home.html @@ -7,7 +7,7 @@
- Spothole + Spothole
diff --git a/templates/apidocs.html b/templates/apidocs.html index 3f4325d..9266dcb 100644 --- a/templates/apidocs.html +++ b/templates/apidocs.html @@ -1,5 +1,5 @@ {% extends "skeleton.html" %} {% block body %} - + {% end %} diff --git a/templates/bands.html b/templates/bands.html index bd304e2..5ee8472 100644 --- a/templates/bands.html +++ b/templates/bands.html @@ -75,8 +75,8 @@ - - + + diff --git a/templates/base.html b/templates/base.html index 906aa30..8e27dd3 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,26 +1,26 @@ {% extends "skeleton.html" %} {% block head_extra %} - - - - + + + + - - - - + + + + - - - - + + + + {% end %} {% block body %}
- +