mirror of
https://github.com/shuaiplus/nodewarden.git
synced 2026-06-20 21:00:41 +00:00
feat: refactor setup handling and enhance asset serving with bootstrap integration
This commit is contained in:
@@ -4,11 +4,54 @@ import { handleRequest } from './router';
|
||||
import { StorageService } from './services/storage';
|
||||
import { applyCors, jsonResponse } from './utils/response';
|
||||
import { runScheduledBackupIfDue, seedDefaultBackupSettings } from './handlers/backup';
|
||||
import { buildWebBootstrapResponse } from './router-public';
|
||||
|
||||
let dbInitialized = false;
|
||||
let dbInitError: string | null = null;
|
||||
let dbInitPromise: Promise<void> | null = null;
|
||||
|
||||
function isWorkerHandledPath(path: string): boolean {
|
||||
return (
|
||||
path.startsWith('/api/') ||
|
||||
path.startsWith('/identity/') ||
|
||||
path.startsWith('/icons/') ||
|
||||
path.startsWith('/notifications/') ||
|
||||
path.startsWith('/.well-known/') ||
|
||||
path === '/config' ||
|
||||
path === '/api/config' ||
|
||||
path === '/api/version'
|
||||
);
|
||||
}
|
||||
|
||||
function injectBootstrapIntoHtml(html: string, env: Env): string {
|
||||
const payload = JSON.stringify(buildWebBootstrapResponse(env)).replace(/</g, '\\u003c');
|
||||
const script = `<script>window.__NW_BOOT__=${payload};</script>`;
|
||||
if (html.includes('</head>')) {
|
||||
return html.replace('</head>', `${script}</head>`);
|
||||
}
|
||||
return `${script}${html}`;
|
||||
}
|
||||
|
||||
async function maybeServeAsset(request: Request, env: Env): Promise<Response | null> {
|
||||
if (!env.ASSETS) return null;
|
||||
if (request.method !== 'GET' && request.method !== 'HEAD') return null;
|
||||
const url = new URL(request.url);
|
||||
if (isWorkerHandledPath(url.pathname)) return null;
|
||||
|
||||
const assetResponse = await env.ASSETS.fetch(request);
|
||||
const contentType = String(assetResponse.headers.get('Content-Type') || '').toLowerCase();
|
||||
if (request.method === 'GET' && contentType.includes('text/html')) {
|
||||
const html = await assetResponse.text();
|
||||
const injected = injectBootstrapIntoHtml(html, env);
|
||||
return new Response(injected, {
|
||||
status: assetResponse.status,
|
||||
statusText: assetResponse.statusText,
|
||||
headers: assetResponse.headers,
|
||||
});
|
||||
}
|
||||
return assetResponse;
|
||||
}
|
||||
|
||||
async function ensureDatabaseInitialized(env: Env): Promise<void> {
|
||||
if (dbInitialized) return;
|
||||
|
||||
@@ -35,6 +78,11 @@ async function ensureDatabaseInitialized(env: Env): Promise<void> {
|
||||
export default {
|
||||
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
||||
void ctx;
|
||||
const assetResponse = await maybeServeAsset(request, env);
|
||||
if (assetResponse) {
|
||||
return applyCors(request, assetResponse);
|
||||
}
|
||||
|
||||
await ensureDatabaseInitialized(env);
|
||||
if (dbInitError) {
|
||||
// Log full error server-side, return generic message to client.
|
||||
|
||||
Reference in New Issue
Block a user