2026-03-22 02:46:34 +08:00
|
|
|
import Fastify from "fastify";
|
|
|
|
|
import cors from "@fastify/cors";
|
|
|
|
|
import rateLimit from "@fastify/rate-limit";
|
|
|
|
|
import { env } from "./config.js";
|
|
|
|
|
import { APP_VERSION } from "@stirling-image/shared";
|
2026-03-22 02:51:57 +08:00
|
|
|
import { runMigrations } from "./db/migrate.js";
|
2026-03-22 02:55:10 +08:00
|
|
|
import { ensureDefaultAdmin, authRoutes, authMiddleware } from "./plugins/auth.js";
|
2026-03-22 02:51:57 +08:00
|
|
|
|
|
|
|
|
// Run before anything else
|
|
|
|
|
runMigrations();
|
|
|
|
|
console.log("Database initialized");
|
2026-03-22 02:46:34 +08:00
|
|
|
|
2026-03-22 02:55:10 +08:00
|
|
|
// Create default admin user if no users exist
|
|
|
|
|
await ensureDefaultAdmin();
|
|
|
|
|
|
2026-03-22 02:46:34 +08:00
|
|
|
const app = Fastify({
|
|
|
|
|
logger: true,
|
|
|
|
|
bodyLimit: env.MAX_UPLOAD_SIZE_MB * 1024 * 1024,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Plugins
|
|
|
|
|
await app.register(cors, { origin: true });
|
|
|
|
|
await app.register(rateLimit, {
|
|
|
|
|
max: env.RATE_LIMIT_PER_MIN,
|
|
|
|
|
timeWindow: "1 minute",
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-22 02:55:10 +08:00
|
|
|
// Auth middleware (must be registered before routes it protects)
|
|
|
|
|
await authMiddleware(app);
|
|
|
|
|
|
|
|
|
|
// Auth routes
|
|
|
|
|
await authRoutes(app);
|
|
|
|
|
|
2026-03-22 02:46:34 +08:00
|
|
|
// Health check
|
|
|
|
|
app.get("/api/v1/health", async () => ({
|
|
|
|
|
status: "healthy",
|
|
|
|
|
version: APP_VERSION,
|
|
|
|
|
uptime: process.uptime().toFixed(0) + "s",
|
|
|
|
|
storage: { mode: env.STORAGE_MODE, available: "N/A" },
|
|
|
|
|
queue: { active: 0, pending: 0 },
|
|
|
|
|
ai: {},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Start
|
|
|
|
|
try {
|
|
|
|
|
await app.listen({ port: env.PORT, host: "0.0.0.0" });
|
|
|
|
|
console.log(`Stirling Image API running on port ${env.PORT}`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
app.log.error(err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|