From 51bb9696b02bbf61a3e9e30be446f560a36909e2 Mon Sep 17 00:00:00 2001 From: Mitchell Scott <10804314+rmitchellscott@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:05:37 -0600 Subject: [PATCH] add x-weatherstar header for specific 404 detection --- README.md | 7 +++++++ nginx.conf | 2 ++ server/scripts/modules/media.mjs | 16 ++++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5d3ef60..8683319 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,13 @@ Simply bind mount your music folder and the playlist will be created automatically. If no files are found in `/music`, the built in tracks located in `/music/default/` will be used instead. +The nginx configuration also sets the `X-Weatherstar: true` header on all +responses. This uses `add_header ... always` so the header is sent even for +404 responses. When `playlist.json` returns a 404 with this header present, the +browser falls back to scanning the `/music` directory. If you host the static +files elsewhere, be sure to include this header so the playlist can be generated +automatically. + ## What's different I've made several changes to this Weather Star 4000 simulation compared to the original hardware unit and the code that this was forked from. diff --git a/nginx.conf b/nginx.conf index c197c90..a770d7e 100644 --- a/nginx.conf +++ b/nginx.conf @@ -4,6 +4,8 @@ server { root /usr/share/nginx/html; + add_header X-Weatherstar true always; + location / { index redirect.html index.html index.htm; try_files $uri $uri/ =404; diff --git a/server/scripts/modules/media.mjs b/server/scripts/modules/media.mjs index b837ef5..46e1395 100644 --- a/server/scripts/modules/media.mjs +++ b/server/scripts/modules/media.mjs @@ -1,4 +1,4 @@ -import { json, text } from './utils/fetch.mjs'; +import { text } from './utils/fetch.mjs'; import Setting from './utils/setting.mjs'; let playlist; @@ -42,9 +42,17 @@ const scanMusicDirectory = async () => { const getMedia = async () => { try { - // fetch the playlist - const rawPlaylist = await json('playlist.json'); - playlist = rawPlaylist; + const response = await fetch('playlist.json'); + if (response.ok) { + playlist = await response.json(); + } else if (response.status === 404 + && response.headers.get('X-Weatherstar') === 'true') { + console.warn("Couldn't get playlist.json, falling back to directory scan"); + playlist = await scanMusicDirectory(); + } else { + console.warn(`Couldn't get playlist.json: ${response.status} ${response.statusText}`); + playlist = { availableFiles: [] }; + } } catch (e) { console.warn("Couldn't get playlist.json, falling back to directory scan"); playlist = await scanMusicDirectory();