Skip to content

Commit df46888

Browse files
committed
PEER-208 Add health check and helmet
Signed-off-by: SeeuSim <[email protected]>
1 parent c03bd52 commit df46888

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

backend/user/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"drizzle-orm": "^0.33.0",
2222
"express": "^4.21.0",
2323
"express-rate-limit": "^7.4.0",
24+
"helmet": "^7.1.0",
2425
"http-status-codes": "^2.3.0",
2526
"jsonwebtoken": "^9.0.2",
2627
"pino": "^9.4.0",

backend/user/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import app from './server';
1+
import app, { dbHealthCheck } from './server';
22
import { logger } from './lib/utils';
33

44
const port = process.env.PORT || 8001;
55

66
const listenMessage = `App listening on port: ${port}`;
7-
app.listen(port, () => {
7+
app.listen(port, async () => {
8+
await dbHealthCheck();
89
logger.info(listenMessage);
910
});

backend/user/src/server.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
import express, { json } from 'express';
22
import pino from 'pino-http';
3-
import { db, users } from './lib/db';
3+
import { db } from './lib/db';
44
import authRoutes from './routes/auth';
5+
import helmet from 'helmet';
6+
import { sql } from 'drizzle-orm';
7+
import { logger } from './lib/utils';
8+
import { exit } from 'process';
59

610
const app = express();
711
app.use(pino());
812
app.use(json());
13+
app.use(helmet());
914
app.use('/auth', authRoutes);
1015
app.get('/', async (_req, res) => {
1116
res.json({
1217
message: 'OK',
1318
});
1419
});
1520

21+
export const dbHealthCheck = async () => {
22+
try {
23+
await db.execute(sql`SELECT 1`);
24+
logger.info('Connected to DB');
25+
} catch (error) {
26+
const { message } = error as Error;
27+
logger.error('Cannot connect to DB: ' + message);
28+
exit(1);
29+
}
30+
};
31+
1632
// Ensure DB service is up before running.
1733
app.get('/test-db', async (_req, res) => {
18-
await db.select().from(users);
34+
await dbHealthCheck();
1935
res.json({ message: 'OK ' });
2036
});
2137

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)