Files
Deep-Package-Inspection/scripts/start-mongo.js
T
rafif dd4c8f6876 feat(source2): push all latest files - device labeling, help system, proxy docs, database isolation fix
- Added DOKUMENTASI-FILTER-PER-SITE.md (site isolation docs)
- Fixed start-with-env.js to force-load .env.production
- Fixed MONGODB_URI hostname from mongodb-netify to mongodb.prod.proit.id
- Updated .gitignore to exclude sensitive scripts and credential files
- Minor UI and labeling improvements
2026-07-29 14:14:29 +07:00

60 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const net = require('net');
const { MongoMemoryServer } = require('mongodb-memory-server');
function checkPort(port, host) {
return new Promise((resolve) => {
const socket = new net.Socket();
socket.setTimeout(1000);
socket.on('connect', () => {
socket.destroy();
resolve(true);
});
socket.on('timeout', () => {
socket.destroy();
resolve(false);
});
socket.on('error', () => {
socket.destroy();
resolve(false);
});
socket.connect(port, host);
});
}
async function start() {
try {
const isLocalMongoRunning = await checkPort(27017, '127.0.0.1') || await checkPort(27017, 'localhost');
if (isLocalMongoRunning) {
console.log('[MONGO] ℹ️ Local persistent MongoDB is already running on port 27017. Skipping in-memory MongoDB launch.');
// Keep process alive so concurrently doesn't show it as exited/failed
setInterval(() => {}, 100000);
return;
}
const mongod = await MongoMemoryServer.create({
instance: {
port: 27017,
dbName: 'backone_inspect_0'
}
});
console.log('[MONGO] 🚀 Local In-Memory MongoDB running at: mongodb://127.0.0.1:27017/backone_inspect_0');
const cleanup = async () => {
await mongod.stop();
process.exit(0);
};
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
} catch (err) {
if (err.code === 'EADDRINUSE' || (err.message && err.message.includes('EADDRINUSE'))) {
console.log('[MONGO] ℹ️ Port 27017 is already in use. Skipping in-memory MongoDB launch.');
setInterval(() => {}, 100000);
} else {
console.error('[MONGO] ❌ Error starting MongoMemoryServer:', err.message);
}
}
}
start();