Skip to content

Commit

Permalink
Merge pull request #131 from GeneralMagicio/feat/health-check-db
Browse files Browse the repository at this point in the history
Added db connection test to health check
  • Loading branch information
aminlatifi authored Nov 4, 2024
2 parents dea77e0 + 3361ab9 commit 6c72097
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
"test:bootstrap": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/server/bootstrap.test.ts",
"test:utils": "NODE_ENV=test mocha ./src/utils/utils.test.ts",
"test:inverterScript": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/scripts/syncDataWithInverter.test.ts",
"test:healthCheck": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/server/bootstrap.test.ts",
"start": "NODE_ENV=development ts-node-dev --project ./tsconfig.json --respawn ./src/index.ts",
"start:test": "NODE_ENV=development ts-node-dev --project ./tsconfig.json --respawn ./test.ts",
"serve": "pm2 startOrRestart ecosystem.config.js --node-args='--max-old-space-size=8192'",
Expand Down
2 changes: 1 addition & 1 deletion src/server/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ describe('health check test case', healthCheckTestCases);
function healthCheckTestCases() {
it('should return empty array for now startTimestamp', async () => {
const result = await axios.get('http://localhost:4000/health');
assert.equal(result.data, 'Hi every thing seems ok');
assert.equal(result.data, 'OK');
});
}
16 changes: 12 additions & 4 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginSchemaReporting } from '@apollo/server/plugin/schemaReporting';
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
import express, { json, Request } from 'express';
import express, { json, Request, RequestHandler } from 'express';
import { Container } from 'typedi';
import { Resource } from '@adminjs/typeorm';
import { validate } from 'class-validator';
Expand Down Expand Up @@ -424,9 +424,7 @@ export async function bootstrap() {
bodyParser.raw({ type: 'application/json' }),
handleStripeWebhook,
);
app.get('/health', (_req, res) => {
res.send('Hi every thing seems ok');
});
app.get('/health', healthCheck);
app.post('/transak_webhook', webhookHandler);

const httpServer = http.createServer(app);
Expand Down Expand Up @@ -472,3 +470,13 @@ async function setPgTrgmParameters(ds: DataSource) {
`SET pg_trgm.word_similarity_threshold TO ${similarityThreshold};`,
);
}

const healthCheck: RequestHandler = async (_req, res) => {
const ds = AppDataSource.getDataSource();
const result = (await ds.query('select version()')) || [];
const version = result[0]?.version || '';
if (!version.startsWith('PostgreSQL')) {
throw new Error('Unexpected db result');
}
res.sendStatus(200);
};

0 comments on commit 6c72097

Please sign in to comment.