Complete stage 7
This commit is contained in:
parent
3e7b89bcad
commit
1f88e291a9
9 changed files with 784 additions and 26 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue