-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
74 lines (62 loc) · 2.03 KB
/
app.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
#!/usr/bin/env node
/* eslint unicorn/no-process-exit: 0 */
const config = require('./application/config')
// Until node 11 adds flatmap, we use this:
require('array.prototype.flatmap').shim()
const {app, io, server} = require('./infrastructure/web/web')
const ClientNotification = require('./infrastructure/web/client-notification')
const ImapService = require('./application/imap-service')
const MailProcessingService = require('./application/mail-processing-service')
const MailRepository = require('./domain/mail-repository')
const clientNotification = new ClientNotification()
clientNotification.use(io)
const imapService = new ImapService(config)
const mailProcessingService = new MailProcessingService(
new MailRepository(),
imapService,
clientNotification,
config
)
// Put everything together:
imapService.on(ImapService.EVENT_NEW_MAIL, mail =>
mailProcessingService.onNewMail(mail)
)
imapService.on(ImapService.EVENT_INITIAL_LOAD_DONE, () =>
mailProcessingService.onInitialLoadDone()
)
imapService.on(ImapService.EVENT_DELETED_MAIL, mail =>
mailProcessingService.onMailDeleted(mail)
)
mailProcessingService.on('error', err => {
console.error('error from mailProcessingService, stopping.', err)
process.exit(1)
})
imapService.on(ImapService.EVENT_ERROR, error => {
console.error('fatal error from imap service', error)
process.exit(1)
})
app.set('mailProcessingService', mailProcessingService)
imapService.connectAndLoadMessages().catch(error => {
console.error('fatal error from imap service', error)
process.exit(1)
})
server.on('error', error => {
if (error.syscall !== 'listen') {
console.error('fatal web server error', error)
return
}
// Handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(
'Port ' + config.http.port + ' requires elevated privileges'
)
process.exit(1)
case 'EADDRINUSE':
console.error('Port ' + config.http.port + ' is already in use')
process.exit(1)
default:
console.error('fatal web server error', error)
process.exit(1)
}
})