Refactor: Initial code split into includes directory for modularity
- Reorganized backend logic by moving API, authentication, Docker, status, and WebSocket handling into separate modules (api.js, auth.js, docker.js, status.js, websocket.js) within ./includes/ - Converted codebase to ES modules with import/export syntax for modern JavaScript - Updated index.js to serve as main entry point, importing from ./includes/ - Reduced code duplication and improved readability with modularized functions - Ensured full functionality preservation, including Docker stats and WebSocket communication - Updated README to reflect new folder structure and ES module setup
This commit is contained in:
49
includes/status.js
Normal file
49
includes/status.js
Normal file
@ -0,0 +1,49 @@
|
||||
import { promisify } from 'util';
|
||||
import { exec } from 'child_process';
|
||||
import { Socket } from 'net';
|
||||
const execPromise = promisify(exec);
|
||||
|
||||
export async function checkConnectionStatus(hostname, port) {
|
||||
try {
|
||||
const { stdout, stderr } = await execPromise(`${process.env.STATUS_CHECK_PATH} -host ${hostname} -port ${port}`);
|
||||
if (stderr) return { isOnline: false, error: stderr };
|
||||
return { isOnline: true, data: JSON.parse(stdout) };
|
||||
} catch (error) {
|
||||
return { isOnline: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
export async function checkGeyserStatus(hostname, port) {
|
||||
try {
|
||||
const { stdout, stderr } = await execPromise(`${process.env.GEYSER_STATUS_CHECK_PATH} -host ${hostname} -port ${port}`);
|
||||
if (stderr) return { isOnline: false, error: stderr };
|
||||
return { isOnline: true, data: JSON.parse(stdout) };
|
||||
} catch (error) {
|
||||
return { isOnline: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
export async function checkSftpStatus(hostname, port) {
|
||||
return new Promise((resolve) => {
|
||||
const socket = new Socket();
|
||||
const timeout = parseInt(process.env.SFTP_CONNECTION_TIMEOUT_MS, 10);
|
||||
socket.setTimeout(timeout);
|
||||
|
||||
socket.on('connect', () => {
|
||||
socket.destroy();
|
||||
resolve({ isOnline: true });
|
||||
});
|
||||
|
||||
socket.on('timeout', () => {
|
||||
socket.destroy();
|
||||
resolve({ isOnline: false, error: 'Connection timed out' });
|
||||
});
|
||||
|
||||
socket.on('error', (error) => {
|
||||
socket.destroy();
|
||||
resolve({ isOnline: false, error: error.message });
|
||||
});
|
||||
|
||||
socket.connect(port, process.env.SFTP_HOSTNAME);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user