2017-03-11 23:21:08 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-16 19:37:33 +08:00
|
|
|
const config = require('wild-config');
|
2017-06-03 14:51:58 +08:00
|
|
|
const log = require('npmlog');
|
|
|
|
const imap = require('./imap');
|
|
|
|
const pop3 = require('./pop3');
|
|
|
|
const lmtp = require('./lmtp');
|
|
|
|
const api = require('./api');
|
2021-06-15 15:47:18 +08:00
|
|
|
const acme = require('./acme');
|
2018-10-10 21:19:20 +08:00
|
|
|
const tasks = require('./tasks');
|
2020-10-09 16:08:33 +08:00
|
|
|
const webhooks = require('./webhooks');
|
2023-01-19 16:46:12 +08:00
|
|
|
const indexer = require('./indexer');
|
2017-10-26 15:21:32 +08:00
|
|
|
const plugins = require('./lib/plugins');
|
2017-06-03 14:51:58 +08:00
|
|
|
const db = require('./lib/db');
|
2017-08-29 15:36:45 +08:00
|
|
|
const errors = require('./lib/errors');
|
2017-03-11 23:21:08 +08:00
|
|
|
|
2017-07-16 19:37:33 +08:00
|
|
|
// preload certificate files
|
|
|
|
require('./lib/certs');
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
// Initialize database connection
|
|
|
|
db.connect(err => {
|
2017-03-11 23:21:08 +08:00
|
|
|
if (err) {
|
2017-03-27 15:36:45 +08:00
|
|
|
log.error('Db', 'Failed to setup database connection');
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2018-10-10 21:19:20 +08:00
|
|
|
|
|
|
|
tasks.start(err => {
|
2017-03-11 23:21:08 +08:00
|
|
|
if (err) {
|
2018-10-10 21:19:20 +08:00
|
|
|
log.error('App', 'Failed to start task runner. %s', err.message);
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2018-10-10 21:19:20 +08:00
|
|
|
|
2020-10-09 16:08:33 +08:00
|
|
|
webhooks.start(err => {
|
2017-03-11 23:21:08 +08:00
|
|
|
if (err) {
|
2020-10-09 16:08:33 +08:00
|
|
|
log.error('App', 'Failed to start webhook runner. %s', err.message);
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2020-10-09 16:08:33 +08:00
|
|
|
|
2023-01-19 16:46:12 +08:00
|
|
|
indexer.start(err => {
|
2017-03-11 23:21:08 +08:00
|
|
|
if (err) {
|
2023-01-19 16:46:12 +08:00
|
|
|
log.error('App', 'Failed to start indexer process. %s', err.message);
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2023-01-19 16:46:12 +08:00
|
|
|
|
|
|
|
// Start IMAP server
|
|
|
|
imap(err => {
|
2017-04-08 02:29:14 +08:00
|
|
|
if (err) {
|
2023-01-19 16:46:12 +08:00
|
|
|
log.error('App', 'Failed to start IMAP server. %s', err.message);
|
2017-08-29 15:36:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
2017-03-11 23:21:08 +08:00
|
|
|
}
|
2023-01-19 16:46:12 +08:00
|
|
|
// Start POP3 server
|
|
|
|
pop3(err => {
|
2017-11-06 23:32:45 +08:00
|
|
|
if (err) {
|
2023-01-19 16:46:12 +08:00
|
|
|
log.error('App', 'Failed to start POP3 server');
|
2017-11-06 23:32:45 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
|
|
|
}
|
2023-01-19 16:46:12 +08:00
|
|
|
// Start LMTP maildrop server
|
|
|
|
lmtp(err => {
|
2018-10-10 21:19:20 +08:00
|
|
|
if (err) {
|
2023-01-19 16:46:12 +08:00
|
|
|
log.error('App', 'Failed to start LMTP server');
|
2018-10-10 21:19:20 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:46:12 +08:00
|
|
|
// Start HTTP API server
|
|
|
|
api(err => {
|
2020-10-09 16:08:33 +08:00
|
|
|
if (err) {
|
2023-01-19 16:46:12 +08:00
|
|
|
log.error('App', 'Failed to start API server');
|
2020-10-09 16:08:33 +08:00
|
|
|
errors.notify(err);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:46:12 +08:00
|
|
|
// Start HTTP ACME server
|
|
|
|
acme(err => {
|
|
|
|
if (err) {
|
|
|
|
log.error('App', 'Failed to start ACME server');
|
|
|
|
errors.notify(err);
|
2021-06-15 15:47:18 +08:00
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
|
|
|
}
|
2023-01-19 16:46:12 +08:00
|
|
|
|
|
|
|
// downgrade user and group if needed
|
|
|
|
if (config.group) {
|
|
|
|
try {
|
|
|
|
process.setgid(config.group);
|
|
|
|
log.info('App', 'Changed group to "%s" (%s)', config.group, process.getgid());
|
|
|
|
} catch (E) {
|
|
|
|
log.error('App', 'Failed to change group to "%s" (%s)', config.group, E.message);
|
|
|
|
errors.notify(E);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (config.user) {
|
|
|
|
try {
|
|
|
|
process.setuid(config.user);
|
|
|
|
log.info('App', 'Changed user to "%s" (%s)', config.user, process.getuid());
|
|
|
|
} catch (E) {
|
|
|
|
log.error('App', 'Failed to change user to "%s" (%s)', config.user, E.message);
|
|
|
|
errors.notify(E);
|
|
|
|
return setTimeout(() => process.exit(1), 3000);
|
|
|
|
}
|
2021-06-15 15:47:18 +08:00
|
|
|
}
|
|
|
|
|
2023-01-19 16:46:12 +08:00
|
|
|
plugins.init('receiver');
|
|
|
|
plugins.handler.load(() => {
|
|
|
|
log.verbose('Plugins', 'Plugins loaded');
|
|
|
|
plugins.handler.runHooks('init', [], () => {
|
|
|
|
log.info('App', 'All servers started, ready to process some mail');
|
|
|
|
});
|
2021-06-15 15:47:18 +08:00
|
|
|
});
|
2020-10-09 16:08:33 +08:00
|
|
|
});
|
2018-10-10 21:19:20 +08:00
|
|
|
});
|
2017-10-26 15:21:32 +08:00
|
|
|
});
|
2017-09-17 18:31:02 +08:00
|
|
|
});
|
2017-04-08 02:29:14 +08:00
|
|
|
});
|
2017-03-11 23:21:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|