-
Notifications
You must be signed in to change notification settings - Fork 122
/
smtp.js
67 lines (58 loc) · 1.71 KB
/
smtp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
const process = require('node:process');
// eslint-disable-next-line import/no-unassigned-import
require('#config/env');
// eslint-disable-next-line import/no-unassigned-import
require('#config/mongoose');
const { setTimeout } = require('node:timers/promises');
const Graceful = require('@ladjs/graceful');
const Redis = require('@ladjs/redis');
const ip = require('ip');
const mongoose = require('mongoose');
const ms = require('ms');
const sharedConfig = require('@ladjs/shared-config');
const SMTP = require('./smtp-server');
const logger = require('#helpers/logger');
const monitorServer = require('#helpers/monitor-server');
const setupMongoose = require('#helpers/setup-mongoose');
const breeSharedConfig = sharedConfig('BREE');
const client = new Redis(breeSharedConfig.redis, logger);
const smtp = new SMTP({ client });
const graceful = new Graceful({
mongooses: [mongoose],
servers: [smtp.server],
redisClients: [client],
customHandlers: [
() => {
smtp.isClosing = true;
},
async () => {
// wait for connection rate limiter to finish
// (since `onClose` is run in the background)
await setTimeout(ms('3s'));
}
],
logger
});
graceful.listen();
monitorServer();
(async () => {
try {
await smtp.listen();
if (process.send) process.send('ready');
logger.info(
`SMTP server listening on ${
smtp.server.address().port
} (LAN: ${ip.address()}:${smtp.server.address().port})`,
{ hide_meta: true }
);
await setupMongoose(logger);
} catch (err) {
await logger.error(err);
process.exit(1);
}
})();
logger.info('SMTP server started', { hide_meta: true });