This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (48 loc) · 1.6 KB
/
index.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
const NodeMailer = require('nodemailer');
const Path = require('path');
const Consola = require('consola');
const Express = require('express');
const MailManager = require('./lib/MailManager');
require('dotenv').config();
if (!(process.env.MAIL_SERVER && process.env.MAIL_SERVER_PORT && process.env.PORT)) {
console.error('MAIL_SERVER, MAIL_SERVER_PORT or PORT not set!');
process.exit(1);
}
if (process.env.MAIL_USERNAME && !process.env.MAIL_PASSWORD) {
console.error('MAIL_USERNAME is set but MAIL_PASSWORD is not!');
process.exit(1);
}
const MAIL_SERVER_PORT = parseInt(process.env.MAIL_SERVER_PORT);
const MAIL_SERVER_TLS = process.env.MAIL_SERVER_TLS === 'true' || process.env.MAIL_SERVER_TLS === '1';
const API_PORT = parseInt(process.env.PORT) || 22003;
if (isNaN(MAIL_SERVER_PORT)) {
console.error('MAIL_SERVER_PORT is not a number!');
process.exit(1);
}
if (isNaN(API_PORT)) {
console.error('API_PORT is not a number!');
process.exit(1);
}
const transport = NodeMailer.createTransport({
host: process.env.MAIL_SERVER,
port: MAIL_SERVER_PORT,
secure: MAIL_SERVER_TLS,
...(process.env.MAIL_USERNAME && process.env.MAIL_PASSWORD
? {
auth: {
user: process.env.MAIL_USERNAME,
pass: process.env.MAIL_PASSWORD
}
}
: {})
});
const mailManager = MailManager.getInstance(Path.join(__dirname, 'mails'), transport);
const server = Express();
server.use(Express.json());
const main = async () => {
mailManager.load();
server.use(require('./lib/routes'));
await new Promise((res) => server.listen(API_PORT, () => res()));
Consola.success(`Listening on port ${API_PORT}`);
};
main();