Complete stage 7

This commit is contained in:
Joey Yakimowich-Payne 2026-01-13 20:21:47 -07:00
commit 1f88e291a9
No known key found for this signature in database
GPG key ID: 6BFE655FA5ABD1E1
9 changed files with 784 additions and 26 deletions

View file

@ -12,6 +12,19 @@ app.use(cors({
credentials: true,
}));
const LOG_REQUESTS = process.env.LOG_REQUESTS === 'true';
app.use((req: Request, res: Response, next: NextFunction) => {
if (LOG_REQUESTS && req.path !== '/health') {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
console.log(`${req.method} ${req.path} ${res.statusCode} ${duration}ms`);
});
}
next();
});
app.use((req: Request, res: Response, next: NextFunction) => {
express.json({ limit: '10mb' })(req, res, (err) => {
if (err instanceof SyntaxError && 'body' in err) {
@ -27,7 +40,20 @@ app.use((req: Request, res: Response, next: NextFunction) => {
});
app.get('/health', (_req: Request, res: Response) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
try {
db.prepare('SELECT 1').get();
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
database: 'connected'
});
} catch {
res.status(503).json({
status: 'error',
timestamp: new Date().toISOString(),
database: 'disconnected'
});
}
});
app.use('/api/quizzes', quizzesRouter);