2017-09-07 18:30:52 +00:00
|
|
|
import connectSession from 'connect-redis'
|
2017-08-02 21:24:01 +00:00
|
|
|
import session from 'express-session'
|
2017-09-07 18:30:52 +00:00
|
|
|
import favicon from 'serve-favicon'
|
2017-08-02 21:24:01 +00:00
|
|
|
import bodyParser from 'body-parser'
|
2017-09-07 18:30:52 +00:00
|
|
|
import express from 'express'
|
2017-08-02 21:24:01 +00:00
|
|
|
import crypto from 'crypto'
|
2017-09-07 18:30:52 +00:00
|
|
|
import path from 'path'
|
2017-08-02 21:24:01 +00:00
|
|
|
|
|
|
|
import routes from './routes'
|
|
|
|
import flash from '../scripts/flash'
|
|
|
|
import config from '../scripts/load-config'
|
2017-08-24 13:42:57 +00:00
|
|
|
import email from './api/emailer'
|
2017-08-02 21:24:01 +00:00
|
|
|
|
|
|
|
let app = express()
|
|
|
|
let SessionStore = connectSession(session)
|
|
|
|
|
|
|
|
app.enable('trust proxy', 1)
|
|
|
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
|
|
|
app.use(flash())
|
|
|
|
|
|
|
|
app.disable('x-powered-by')
|
|
|
|
|
|
|
|
app.use(session({
|
|
|
|
key: config.server.session_key,
|
|
|
|
secret: config.server.session_secret,
|
|
|
|
store: new SessionStore(config.redis),
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: true
|
|
|
|
}))
|
|
|
|
|
|
|
|
app.use((req, res, next) => {
|
2017-08-27 11:48:47 +00:00
|
|
|
// Inject a cleaner version of the user's IP Address into the request
|
2017-08-02 21:24:01 +00:00
|
|
|
let ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress
|
|
|
|
|
|
|
|
if (ipAddr.indexOf('::ffff:') !== -1) {
|
|
|
|
ipAddr = ipAddr.replace('::ffff:', '')
|
|
|
|
}
|
|
|
|
|
2017-08-27 11:48:47 +00:00
|
|
|
req.realIP = ipAddr
|
|
|
|
|
|
|
|
// Make sure CSRF token is present in the session
|
2017-08-02 21:24:01 +00:00
|
|
|
if (!req.session.csrf) {
|
|
|
|
req.session.csrf = crypto.randomBytes(12).toString('hex')
|
|
|
|
}
|
|
|
|
|
2017-08-27 11:48:47 +00:00
|
|
|
// Add user and csrf token into rendering information
|
2017-08-02 21:24:01 +00:00
|
|
|
res.locals = Object.assign(res.locals, {
|
|
|
|
user: req.session.user || null,
|
|
|
|
csrf: req.session.csrf
|
|
|
|
})
|
|
|
|
|
2017-08-31 12:32:00 +00:00
|
|
|
// Add Piwik tracker if configured
|
2018-01-27 10:19:06 +00:00
|
|
|
if (config.matomo && config.matomo.site_id) {
|
|
|
|
res.locals.matomo = config.matomo
|
2017-08-31 12:32:00 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:24:01 +00:00
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2017-09-07 18:30:52 +00:00
|
|
|
app.use(favicon(path.join(__dirname, '..', 'static', 'image', 'icynet.ico')))
|
|
|
|
|
2017-08-02 21:24:01 +00:00
|
|
|
module.exports = (args) => {
|
|
|
|
app.set('view options', {layout: false})
|
|
|
|
app.set('view engine', 'pug')
|
|
|
|
app.set('views', path.join(__dirname, '../views'))
|
|
|
|
|
2017-11-07 16:52:12 +00:00
|
|
|
if (args.dev) {
|
2017-12-01 11:35:47 +00:00
|
|
|
console.warn('Worker is in development mode')
|
2017-11-07 16:52:12 +00:00
|
|
|
|
|
|
|
// Dev logger
|
|
|
|
const morgan = require('morgan')
|
|
|
|
app.use(morgan('dev'))
|
|
|
|
}
|
|
|
|
|
2017-08-27 11:48:47 +00:00
|
|
|
let staticAge = args.dev ? 1000 : 7 * 24 * 60 * 60 * 1000 // 1 week of cache in production
|
2017-08-02 21:24:01 +00:00
|
|
|
|
2017-08-27 11:48:47 +00:00
|
|
|
// Static content directories, cache these requests.
|
|
|
|
// It is also a good idea to use nginx to serve these directories in order to save on computing power
|
2017-08-02 21:24:01 +00:00
|
|
|
app.use('/style', express.static(path.join(__dirname, '../build/style'), { maxAge: staticAge }))
|
|
|
|
app.use('/script', express.static(path.join(__dirname, '../build/script'), { maxAge: staticAge }))
|
|
|
|
app.use('/static', express.static(path.join(__dirname, '../static'), { maxAge: staticAge }))
|
2017-08-25 16:42:30 +00:00
|
|
|
app.use('/usercontent', express.static(path.join(__dirname, '../usercontent'), { maxAge: staticAge }))
|
2017-08-02 21:24:01 +00:00
|
|
|
|
|
|
|
app.use(routes)
|
|
|
|
|
|
|
|
app.listen(args.port, () => {
|
|
|
|
console.log('Listening on 0.0.0.0:' + args.port)
|
2017-08-27 11:48:47 +00:00
|
|
|
|
|
|
|
// Initialize the email transporter (if configured)
|
2017-08-24 13:42:57 +00:00
|
|
|
email.init()
|
2017-08-02 21:24:01 +00:00
|
|
|
})
|
|
|
|
}
|