Files
caddy-proxy-manager/src/instrumentation.ts
fuomag9 88b25cee65 Add Caddy restart detection, auto-recovery, and metrics exposure
- Implemented health monitoring service that detects Caddy restarts/crashes
- Automatically reapplies configuration when Caddy restarts
- Added metrics settings UI for enabling Prometheus/Grafana monitoring
- Caddy metrics exposed on separate port (default: 9090) via reverse proxy
- Admin API (port 2019) kept internal-only for security
- Updated docker-compose.yml with metrics port documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 19:36:40 +01:00

52 lines
2.0 KiB
TypeScript

/**
* Next.js instrumentation hook - runs once when the server starts
* https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation
*/
export async function register() {
// Only run on the server side
if (process.env.NEXT_RUNTIME === "nodejs") {
// Validate production configuration early to catch misconfigurations
const { validateProductionConfig } = await import("./lib/config");
try {
validateProductionConfig();
} catch (error) {
console.error("Configuration validation failed:", error);
if (process.env.NODE_ENV === "production") {
// Fail fast in production with bad config
throw error;
}
}
const { ensureAdminUser } = await import("./lib/init-db");
try {
await ensureAdminUser();
console.log("Database initialization complete");
} catch (error) {
console.error("Failed to initialize database:", error);
// Don't throw - let the app start anyway, errors will surface when users try to use features
}
// Apply Caddy configuration from database on startup
const { applyCaddyConfig } = await import("./lib/caddy");
try {
console.log("Applying Caddy configuration from database...");
await applyCaddyConfig();
console.log("Caddy configuration applied successfully");
} catch (error) {
console.error("Failed to apply Caddy configuration on startup:", error);
// Don't throw - Caddy might not be ready yet, or config might be applied later
// This ensures proxy hosts work after container restart
}
// Start Caddy health monitoring to detect restarts and auto-reapply config
const { startCaddyMonitoring } = await import("./lib/caddy-monitor");
try {
startCaddyMonitoring();
console.log("Caddy health monitoring started");
} catch (error) {
console.error("Failed to start Caddy health monitoring:", error);
// Don't throw - monitoring is a nice-to-have feature
}
}
}