mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 09:27:50 +00:00
Modify healthcheck and server on-close to close database connection
This commit is contained in:
@@ -1,24 +1,21 @@
|
|||||||
import { Server } from 'http';
|
import { Server } from 'http';
|
||||||
import main from '../src';
|
import main from '../src';
|
||||||
import { describe, expect, it, beforeAll, afterAll } from '@jest/globals';
|
import { describe, expect, it, beforeAll, afterAll } from '@jest/globals';
|
||||||
import supertest from 'supertest';
|
import request from 'supertest';
|
||||||
import { setUpHealthEndpoint } from '../src/services/health';
|
|
||||||
|
|
||||||
let requestWithSupertest: supertest.SuperTest<supertest.Test>;
|
|
||||||
let server: Server;
|
let server: Server;
|
||||||
|
|
||||||
describe('Healthcheck endpoint', () => {
|
beforeAll(async () => {
|
||||||
beforeAll(async () => {
|
server = await main;
|
||||||
server = await main;
|
});
|
||||||
requestWithSupertest = supertest(server);
|
|
||||||
setUpHealthEndpoint(server);
|
|
||||||
});
|
|
||||||
afterAll(async () => {
|
|
||||||
server.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Healthcheck endpoint', () => {
|
||||||
it('GET /healthcheck should return OK', async () => {
|
it('GET /healthcheck should return OK', async () => {
|
||||||
const res = await requestWithSupertest.get('/healthcheck');
|
const res = await request(server).get('/healthcheck');
|
||||||
expect(res.status).toEqual(200);
|
expect(res.status).toEqual(200);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -29,6 +29,23 @@ const initDatabaseHelper = async ({
|
|||||||
return mongoose.connection;
|
return mongoose.connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close database conection
|
||||||
|
*/
|
||||||
|
const closeDatabaseHelper = async () => {
|
||||||
|
return Promise.all([
|
||||||
|
new Promise((resolve) => {
|
||||||
|
if (mongoose.connection && mongoose.connection.readyState == 1) {
|
||||||
|
mongoose.connection.close()
|
||||||
|
.then(() => resolve('Database connection closed'));
|
||||||
|
} else {
|
||||||
|
resolve('Database connection already closed');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
initDatabaseHelper
|
initDatabaseHelper,
|
||||||
|
closeDatabaseHelper
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import mongoose from 'mongoose';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
import infisical from 'infisical-node';
|
import infisical from 'infisical-node';
|
||||||
@@ -158,17 +159,22 @@ const main = async () => {
|
|||||||
//* Handle unrouted requests and respond with proper error message as well as status code
|
//* Handle unrouted requests and respond with proper error message as well as status code
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
if (res.headersSent) return next();
|
if (res.headersSent) return next();
|
||||||
next(RouteNotFoundError({ message: `The requested source '(${req.method})${req.url}' was not found` }))
|
next(RouteNotFoundError({ message: `The requested source '(${req.method})${req.url}' was not found` }))
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use(requestErrorHandler)
|
app.use(requestErrorHandler)
|
||||||
|
|
||||||
const server = app.listen(getPort(), () => {
|
const server = app.listen(getPort(), () => {
|
||||||
createTestUserForDevelopment();
|
|
||||||
getLogger("backend-main").info(`Server started listening at port ${getPort()}`)
|
getLogger("backend-main").info(`Server started listening at port ${getPort()}`)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
createTestUserForDevelopment();
|
||||||
setUpHealthEndpoint(server);
|
setUpHealthEndpoint(server);
|
||||||
|
|
||||||
|
server.on('close', async () => {
|
||||||
|
await DatabaseService.closeDatabase();
|
||||||
|
})
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,32 @@
|
|||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
import { getLogger } from '../utils/logger';
|
import { getLogger } from '../utils/logger';
|
||||||
import { initDatabaseHelper } from '../helpers/database';
|
import {
|
||||||
|
initDatabaseHelper,
|
||||||
|
closeDatabaseHelper
|
||||||
|
} from '../helpers/database';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to handle database actions
|
* Class to handle database actions
|
||||||
*/
|
*/
|
||||||
class DatabaseService {
|
class DatabaseService {
|
||||||
|
/**
|
||||||
|
* Initialize database connection
|
||||||
|
* @param {Object} obj
|
||||||
|
* @param {String} obj.mongoURL - mongo connection string
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
static async initDatabase(MONGO_URL: string) {
|
static async initDatabase(MONGO_URL: string) {
|
||||||
return await initDatabaseHelper({
|
return await initDatabaseHelper({
|
||||||
mongoURL: MONGO_URL
|
mongoURL: MONGO_URL
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close database conection
|
||||||
|
*/
|
||||||
|
static async closeDatabase() {
|
||||||
|
return await closeDatabaseHelper();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default DatabaseService;
|
export default DatabaseService;
|
||||||
Reference in New Issue
Block a user