add x-weatherstar header for specific 404 detection

This commit is contained in:
Mitchell Scott
2025-06-13 22:05:37 -06:00
parent 6ff7122844
commit 51bb9696b0
3 changed files with 21 additions and 4 deletions

View File

@@ -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 automatically. If no files are found in `/music`, the built in tracks located in
`/music/default/` will be used instead. `/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 ## 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. 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.

View File

@@ -4,6 +4,8 @@ server {
root /usr/share/nginx/html; root /usr/share/nginx/html;
add_header X-Weatherstar true always;
location / { location / {
index redirect.html index.html index.htm; index redirect.html index.html index.htm;
try_files $uri $uri/ =404; try_files $uri $uri/ =404;

View File

@@ -1,4 +1,4 @@
import { json, text } from './utils/fetch.mjs'; import { text } from './utils/fetch.mjs';
import Setting from './utils/setting.mjs'; import Setting from './utils/setting.mjs';
let playlist; let playlist;
@@ -42,9 +42,17 @@ const scanMusicDirectory = async () => {
const getMedia = async () => { const getMedia = async () => {
try { try {
// fetch the playlist const response = await fetch('playlist.json');
const rawPlaylist = await json('playlist.json'); if (response.ok) {
playlist = rawPlaylist; 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) { } catch (e) {
console.warn("Couldn't get playlist.json, falling back to directory scan"); console.warn("Couldn't get playlist.json, falling back to directory scan");
playlist = await scanMusicDirectory(); playlist = await scanMusicDirectory();