2015-07-20 00:09:47 -07:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2022-02-09 17:28:46 -08:00
|
|
|
const fs = require('fs'),
|
2019-07-26 15:01:47 -07:00
|
|
|
ldap = require('./src/ldap.js'),
|
2020-08-04 09:34:03 -07:00
|
|
|
paths = require('./src/paths.js'),
|
2020-11-10 09:59:28 -08:00
|
|
|
proxyAuth = require('./src/proxyauth.js'),
|
2021-09-07 09:57:49 -07:00
|
|
|
safe = require('safetydance'),
|
2021-11-24 17:59:21 +01:00
|
|
|
server = require('./src/server.js'),
|
2022-01-07 14:06:13 +01:00
|
|
|
settings = require('./src/settings.js'),
|
|
|
|
|
userdirectory = require('./src/userdirectory.js');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-09-07 11:23:54 -07:00
|
|
|
let logFd;
|
|
|
|
|
|
|
|
|
|
async function setupLogging() {
|
2021-09-07 09:57:49 -07:00
|
|
|
if (process.env.BOX_ENV === 'test') return;
|
2020-08-04 22:16:38 -07:00
|
|
|
|
2021-09-07 11:23:54 -07:00
|
|
|
logFd = fs.openSync(paths.BOX_LOG_FILE, 'a');
|
|
|
|
|
// we used to write using a stream before but it caches internally and there is no way to flush it when things crash
|
|
|
|
|
process.stdout.write = process.stderr.write = function (...args) {
|
|
|
|
|
const callback = typeof args[args.length-1] === 'function' ? args.pop() : function () {}; // callback is required for fs.write
|
|
|
|
|
fs.write.apply(fs, [logFd, ...args, callback]);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this is also used as the 'uncaughtException' handler which can only have synchronous functions
|
|
|
|
|
function exitSync(status) {
|
|
|
|
|
if (status.error) fs.write(logFd, status.error.stack + '\n', function () {});
|
|
|
|
|
fs.fsyncSync(logFd);
|
|
|
|
|
fs.closeSync(logFd);
|
|
|
|
|
process.exit(status.code);
|
2021-09-07 09:57:49 -07:00
|
|
|
}
|
2020-08-04 09:34:03 -07:00
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
async function startServers() {
|
2021-09-07 11:23:54 -07:00
|
|
|
await setupLogging();
|
2021-09-07 09:57:49 -07:00
|
|
|
await server.start(); // do this first since it also inits the database
|
|
|
|
|
await proxyAuth.start();
|
|
|
|
|
await ldap.start();
|
2021-11-24 17:59:21 +01:00
|
|
|
|
2022-01-07 14:06:13 +01:00
|
|
|
const conf = await settings.getUserDirectoryConfig();
|
|
|
|
|
if (conf.enabled) await userdirectory.start();
|
2020-08-04 09:34:03 -07:00
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
async function main() {
|
|
|
|
|
const [error] = await safe(startServers());
|
2021-09-07 11:23:54 -07:00
|
|
|
if (error) return exitSync({ error: new Error(`Error starting server: ${JSON.stringify(error)}`), code: 1 });
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2022-02-21 17:34:51 -08:00
|
|
|
// require this here so that logging handler is already setup
|
2020-08-21 09:45:03 +02:00
|
|
|
const debug = require('debug')('box:box');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
process.on('SIGINT', async function () {
|
2020-08-04 09:34:03 -07:00
|
|
|
debug('Received SIGINT. Shutting down.');
|
2018-11-08 14:35:22 +01:00
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
await proxyAuth.stop();
|
|
|
|
|
await server.stop();
|
2022-01-07 14:06:13 +01:00
|
|
|
await userdirectory.stop();
|
2021-09-07 09:57:49 -07:00
|
|
|
await ldap.stop();
|
2020-08-04 09:34:03 -07:00
|
|
|
setTimeout(process.exit.bind(process), 3000);
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
process.on('SIGTERM', async function () {
|
2020-08-04 09:34:03 -07:00
|
|
|
debug('Received SIGTERM. Shutting down.');
|
2015-09-09 16:57:41 -07:00
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
await proxyAuth.stop();
|
|
|
|
|
await server.stop();
|
2022-01-07 14:06:13 +01:00
|
|
|
await userdirectory.stop();
|
2021-09-07 09:57:49 -07:00
|
|
|
await ldap.stop();
|
2020-08-04 09:34:03 -07:00
|
|
|
setTimeout(process.exit.bind(process), 3000);
|
|
|
|
|
});
|
2018-11-08 14:35:22 +01:00
|
|
|
|
2021-09-07 11:23:54 -07:00
|
|
|
process.on('uncaughtException', (error) => exitSync({ error, code: 1 }));
|
2020-08-21 09:45:03 +02:00
|
|
|
|
2020-08-04 09:34:03 -07:00
|
|
|
console.log(`Cloudron is up and running. Logs are at ${paths.BOX_LOG_FILE}`); // this goes to journalctl
|
2021-09-07 09:57:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|