-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (68 loc) · 2.26 KB
/
app.js
File metadata and controls
84 lines (68 loc) · 2.26 KB
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
76
77
78
79
80
81
82
83
84
/* eslint unicorn/no-process-exit: 0 */
const path = require('path')
const http = require('http')
const express = require('express')
const logger = require('morgan')
const Twig = require('twig')
const compression = require('compression')
const helmet = require('helmet')
const socketio = require('socket.io')
// Until node 11 adds flatmap, we use this:
require('array.prototype.flatmap').shim()
const {sanitizeHtmlTwigFilter} = require('./views/twig-filters')
const EmailManager = require('./mailbox/email-manager')
const inboxRouter = require('./routes/inbox')
const loginRouter = require('./routes/login')
const ClientNotification = require('./helper/client-notification')
const config = require('./helper/config')
// Init express middleware
const app = express()
app.use(helmet())
app.use(compression())
app.set('config', config)
const server = http.createServer(app)
const io = socketio(server)
app.set('socketio', io)
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({extended: false}))
// View engine setup
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'twig')
app.set('twig options', {
autoescape: true
})
// Application code:
app.use(express.static(path.join(__dirname, 'public')))
Twig.extendFilter('sanitizeHtml', sanitizeHtmlTwigFilter)
const clientNotification = new ClientNotification()
clientNotification.use(io)
const emailManager = new EmailManager(config, clientNotification)
app.set('emailManager', emailManager)
app.get('/', (req, res, _next) => {
res.redirect('/login')
})
app.use('/login', loginRouter)
app.use('/', inboxRouter)
// Catch 404 and forward to error handler
app.use((req, res, next) => {
next({message: 'page not found', status: 404})
})
// Error handler
app.use((err, req, res, _next) => {
// Set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
// Render the error page
res.status(err.status || 500)
res.render('error')
})
emailManager.connectImapAndAutorefresh().catch(error => {
console.error('fatal error from email manager', error)
return process.exit(1)
})
emailManager.on('error', err => {
console.error('error from emailManager, stopping.', err)
process.exit(1)
})
module.exports = {app, server}