mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-14 07:39:29 -07:00
Add STATIC environment variable for browser-only deployment mode
Implement STATIC=1 environment variable to enable browser-only deployment without proxy server infrastructure. Uses WS4KP_SERVER_AVAILABLE flag to distinguish between server-backed and static deployments for proper URL rewriting and User-Agent header handling. - Add STATIC env var to skip proxy route registration at startup - Inject WS4KP_SERVER_AVAILABLE flag via EJS template based on STATIC mode - Update fetch.mjs to conditionally send User-Agent headers based on server availability - Update url-rewrite.mjs to skip proxy rewriting when server is unavailable - Use renderIndex helper for consistent template data across dev/prod modes - Improve music playlist logging Benefits of integrated approach: - Single environment variable controls both server and client behavior - Flag injection happens once at render time, not on every request - No runtime HTML string manipulation overhead - Clean separation between server-backed and static deployment logic - Same codebase supports both deployment modes without duplication Static mode (STATIC=1): Direct API calls to external services, no caching Server mode (default): Local proxy with caching and API request observability
This commit is contained in:
@@ -40,21 +40,32 @@ const scanMusicDirectory = async () => {
|
||||
};
|
||||
|
||||
const getMedia = async () => {
|
||||
let playlistSource = '';
|
||||
|
||||
try {
|
||||
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");
|
||||
playlistSource = 'from server';
|
||||
} else if (response.status === 404 && response.headers.get('X-Weatherstar') === 'true') {
|
||||
// Expected behavior in static deployment mode
|
||||
playlist = await scanMusicDirectory();
|
||||
playlistSource = 'via directory scan (static deployment)';
|
||||
} else {
|
||||
console.warn(`Couldn't get playlist.json: ${response.status} ${response.statusText}`);
|
||||
playlist = { availableFiles: [] };
|
||||
playlistSource = `failed (${response.status} ${response.statusText})`;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Couldn't get playlist.json, falling back to directory scan");
|
||||
} catch (_e) {
|
||||
// Network error or other fetch failure - fall back to directory scanning
|
||||
playlist = await scanMusicDirectory();
|
||||
playlistSource = 'via directory scan (after fetch failed)';
|
||||
}
|
||||
|
||||
const fileCount = playlist?.availableFiles?.length || 0;
|
||||
if (fileCount > 0) {
|
||||
console.log(`Loaded playlist ${playlistSource} - found ${fileCount} music file${fileCount === 1 ? '' : 's'}`);
|
||||
} else {
|
||||
console.log(`No music files found ${playlistSource}`);
|
||||
}
|
||||
|
||||
enableMediaPlayer();
|
||||
|
||||
@@ -10,7 +10,7 @@ const safeJson = async (url, params) => {
|
||||
}
|
||||
// If caller didn't specify returnUrl, result is the raw API response
|
||||
return result;
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
// Error already logged in fetchAsync; return null to be "safe"
|
||||
return null;
|
||||
}
|
||||
@@ -25,7 +25,7 @@ const safeText = async (url, params) => {
|
||||
}
|
||||
// If caller didn't specify returnUrl, result is the raw API response
|
||||
return result;
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
// Error already logged in fetchAsync; return null to be "safe"
|
||||
return null;
|
||||
}
|
||||
@@ -40,7 +40,7 @@ const safeBlob = async (url, params) => {
|
||||
}
|
||||
// If caller didn't specify returnUrl, result is the raw API response
|
||||
return result;
|
||||
} catch (error) {
|
||||
} catch (_error) {
|
||||
// Error already logged in fetchAsync; return null to be "safe"
|
||||
return null;
|
||||
}
|
||||
@@ -83,7 +83,11 @@ const fetchAsync = async (_url, responseType, _params = {}) => {
|
||||
const checkUrl = new URL(_url, window.location.origin);
|
||||
const shouldExcludeUserAgent = USER_AGENT_EXCLUDED_HOSTS.some((host) => checkUrl.hostname.includes(host));
|
||||
|
||||
if (!shouldExcludeUserAgent) {
|
||||
// User-Agent handling:
|
||||
// - Server mode (with caching proxy): Add User-Agent for all requests except excluded hosts
|
||||
// - Static mode (direct requests): Only add User-Agent for api.weather.gov, avoiding CORS preflight issues with other services
|
||||
const shouldAddUserAgent = !shouldExcludeUserAgent && (window.WS4KP_SERVER_AVAILABLE || _url.toString().match(/api\.weather\.gov/));
|
||||
if (shouldAddUserAgent) {
|
||||
headers['user-agent'] = 'Weatherstar 4000+; weatherstar@netbymatt.com';
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,10 @@ const rewriteUrl = (_url) => {
|
||||
// Handle both string URLs and URL objects
|
||||
const url = typeof _url === 'string' ? new URL(_url) : new URL(_url.toString());
|
||||
|
||||
if (!window.WS4KP_SERVER_AVAILABLE) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// Rewrite the origin to use local proxy server
|
||||
if (url.origin === 'https://api.weather.gov') {
|
||||
url.protocol = window.location.protocol;
|
||||
|
||||
Reference in New Issue
Block a user