"use strict"; const express = require("express"); const path = require("path"); const { getDb } = require("./database"); const authRoutes = require("./routes/auth"); const profileRoutes = require("./routes/profile"); const programRoutes = require("./routes/program"); const app = express(); const PORT = parseInt(process.env.PORT, 10) || 3000; const startTime = Date.now(); app.disable("x-powered-by"); // Body parsing app.use(express.json()); app.use(express.urlencoded({ extended: true })); // Static files app.use(express.static(path.join(__dirname, "public"))); // Initialize database on startup getDb(); // Health & readiness probes (Kubernetes) app.get("/health", (_req, res) => { res.json({ status: "healthy", uptime: Math.floor((Date.now() - startTime) / 1000), timestamp: new Date().toISOString(), memoryUsage: process.memoryUsage().rss, }); }); app.get("/ready", (_req, res) => { try { getDb(); res.json({ ready: true }); } catch (err) { res.status(503).json({ ready: false, error: err.message }); } }); // API routes app.use("/api/auth", authRoutes); app.use("/api/profile", profileRoutes); app.use("/api/program", programRoutes); // SPA fallback - serve index.html for non-API routes app.get("*", (req, res) => { if (req.path.startsWith("/api/")) { return res.status(404).json({ error: "API endpoint bulunamadı" }); } res.sendFile(path.join(__dirname, "public", "index.html")); }); // 404 handler for API app.use((req, res) => { res.status(404).json({ error: "Bulunamadı" }); }); // Error handler app.use((err, _req, res, _next) => { console.error("Unhandled error:", err.message); res.status(500).json({ error: "Sunucu hatası" }); }); const server = app.listen(PORT, "0.0.0.0", () => { console.log(`health-app listening on port ${PORT}`); }); function shutdown(signal) { console.log(`${signal} received, shutting down gracefully`); server.close(() => process.exit(0)); setTimeout(() => process.exit(1), 10000); } process.on("SIGTERM", () => shutdown("SIGTERM")); process.on("SIGINT", () => shutdown("SIGINT"));