2026-03-22 02:46:34 +08:00
|
|
|
import Fastify from "fastify";
|
|
|
|
|
import cors from "@fastify/cors";
|
|
|
|
|
import rateLimit from "@fastify/rate-limit";
|
2026-03-22 03:09:33 +08:00
|
|
|
import swagger from "@fastify/swagger";
|
|
|
|
|
import swaggerUi from "@fastify/swagger-ui";
|
2026-03-22 02:46:34 +08:00
|
|
|
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 03:47:54 +08:00
|
|
|
import { registerUpload } from "./plugins/upload.js";
|
2026-03-22 03:09:33 +08:00
|
|
|
import { registerStatic } from "./plugins/static.js";
|
|
|
|
|
import { startCleanupCron } from "./lib/cleanup.js";
|
2026-03-22 03:47:54 +08:00
|
|
|
import { fileRoutes } from "./routes/files.js";
|
2026-03-22 03:48:11 +08:00
|
|
|
import { registerToolRoutes } from "./routes/tools/index.js";
|
2026-03-22 04:03:38 +08:00
|
|
|
import { registerBatchRoutes } from "./routes/batch.js";
|
2026-03-22 04:41:51 +08:00
|
|
|
import { registerPipelineRoutes } from "./routes/pipeline.js";
|
2026-03-22 04:03:38 +08:00
|
|
|
import { registerProgressRoutes } from "./routes/progress.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 03:09:33 +08:00
|
|
|
// Swagger / OpenAPI documentation
|
|
|
|
|
await app.register(swagger, {
|
|
|
|
|
openapi: {
|
|
|
|
|
info: {
|
|
|
|
|
title: "Stirling Image API",
|
|
|
|
|
description: "API for Stirling Image — self-hosted image processing suite",
|
|
|
|
|
version: APP_VERSION,
|
|
|
|
|
},
|
2026-03-22 10:31:11 +08:00
|
|
|
servers: [{ url: `http://localhost:1349` }],
|
2026-03-22 03:09:33 +08:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await app.register(swaggerUi, {
|
|
|
|
|
routePrefix: "/api/docs",
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-22 03:47:54 +08:00
|
|
|
// Multipart upload support
|
|
|
|
|
await registerUpload(app);
|
|
|
|
|
|
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 03:47:54 +08:00
|
|
|
// File upload/download routes
|
|
|
|
|
await fileRoutes(app);
|
|
|
|
|
|
2026-03-22 03:48:11 +08:00
|
|
|
// Tool routes (generic factory-based)
|
|
|
|
|
await registerToolRoutes(app);
|
|
|
|
|
|
2026-03-22 04:03:38 +08:00
|
|
|
// Batch processing routes (must be after tool routes so the registry is populated)
|
|
|
|
|
await registerBatchRoutes(app);
|
|
|
|
|
|
2026-03-22 04:41:51 +08:00
|
|
|
// Pipeline routes (must be after tool routes so the registry is populated)
|
|
|
|
|
await registerPipelineRoutes(app);
|
|
|
|
|
|
2026-03-22 04:03:38 +08:00
|
|
|
// Progress SSE routes
|
|
|
|
|
await registerProgressRoutes(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: {},
|
|
|
|
|
}));
|
|
|
|
|
|
2026-03-22 03:09:33 +08:00
|
|
|
// Serve SPA in production
|
|
|
|
|
if (process.env.NODE_ENV === "production") {
|
|
|
|
|
await registerStatic(app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start workspace cleanup cron
|
|
|
|
|
startCleanupCron();
|
|
|
|
|
|
2026-03-22 02:46:34 +08:00
|
|
|
// 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);
|
|
|
|
|
}
|