Scaffold dd0c/drift SaaS backend: Fastify, RLS, ingestion, dashboard API
- Fastify server with Zod validation, pino logging, CORS/helmet - Drift report ingestion endpoint with nonce replay prevention - Dashboard API: stacks list, drift history, report detail, summary stats - PostgreSQL schema with RLS: tenants, users, agent_keys, drift_reports, remediation_actions - withTenant() helper for safe connection pool tenant context management - Config via Zod-validated env vars
This commit is contained in:
48
products/02-iac-drift-detection/saas/src/index.ts
Normal file
48
products/02-iac-drift-detection/saas/src/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import Fastify from 'fastify';
|
||||
import cors from '@fastify/cors';
|
||||
import helmet from '@fastify/helmet';
|
||||
import { config } from './config/index.js';
|
||||
import { registerProcessorRoutes } from './processor/routes.js';
|
||||
import { registerApiRoutes } from './api/routes.js';
|
||||
import { createPool } from './data/db.js';
|
||||
import { createRedis } from './data/redis.js';
|
||||
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
level: config.logLevel,
|
||||
transport: config.nodeEnv === 'development'
|
||||
? { target: 'pino-pretty' }
|
||||
: undefined,
|
||||
},
|
||||
});
|
||||
|
||||
async function start() {
|
||||
await app.register(cors, { origin: config.corsOrigin });
|
||||
await app.register(helmet);
|
||||
|
||||
// Data layer
|
||||
const pool = createPool(config.databaseUrl);
|
||||
const redis = createRedis(config.redisUrl);
|
||||
|
||||
// Decorate fastify instance
|
||||
app.decorate('pool', pool);
|
||||
app.decorate('redis', redis);
|
||||
app.decorate('config', config);
|
||||
|
||||
// Routes
|
||||
await registerProcessorRoutes(app);
|
||||
await registerApiRoutes(app);
|
||||
|
||||
// Health
|
||||
app.get('/health', async () => ({ status: 'ok' }));
|
||||
|
||||
await app.listen({ port: config.port, host: '0.0.0.0' });
|
||||
app.log.info(`dd0c/drift SaaS listening on :${config.port}`);
|
||||
}
|
||||
|
||||
start().catch((err) => {
|
||||
console.error('Failed to start:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
export { app };
|
||||
Reference in New Issue
Block a user