forked from forwardemail/forwardemail.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pop3.js
75 lines (64 loc) · 1.94 KB
/
pop3.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
68
69
70
71
72
73
74
75
/**
* 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 Graceful = require('@ladjs/graceful');
const Redis = require('@ladjs/redis');
const ip = require('ip');
const mongoose = require('mongoose');
const sharedConfig = require('@ladjs/shared-config');
const POP3 = require('./pop3-server');
const createWebSocketAsPromised = require('#helpers/create-websocket-as-promised');
const logger = require('#helpers/logger');
const monitorServer = require('#helpers/monitor-server');
const setupMongoose = require('#helpers/setup-mongoose');
const pop3SharedConfig = sharedConfig('POP3');
const client = new Redis(pop3SharedConfig.redis, logger);
const subscriber = new Redis(pop3SharedConfig.redis, logger);
client.setMaxListeners(0);
subscriber.setMaxListeners(0);
const wsp = createWebSocketAsPromised();
const pop3 = new POP3({ client, subscriber, wsp });
const graceful = new Graceful({
mongooses: [mongoose],
servers: [pop3.server],
redisClients: [client, subscriber],
logger,
customHandlers: [
() => {
pop3.isClosing = true;
},
// <https://github.com/vitalets/websocket-as-promised#wspclosecode-reason--promiseevent>
() => {
try {
wsp.close();
} catch (err) {
logger.fatal(err);
}
}
]
});
graceful.listen();
monitorServer();
(async () => {
try {
await pop3.listen();
if (process.send) process.send('ready');
logger.info(
`POP3 server listening on ${
pop3.server.address().port
} (LAN: ${ip.address()}:${pop3.server.address().port})`,
{ hide_meta: true }
);
await setupMongoose(logger);
} catch (err) {
await logger.error(err);
process.exit(1);
}
})();
logger.info('POP3 server started', { hide_meta: true });