This repository has been archived on 2022-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
IcyNet.eu/server/server.js

81 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-08-02 21:24:01 +00:00
import express from 'express'
import session from 'express-session'
import bodyParser from 'body-parser'
import connectSession from 'connect-redis'
import path from 'path'
import crypto from 'crypto'
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
})
next()
})
module.exports = (args) => {
app.set('view options', {layout: false})
app.set('view engine', 'pug')
app.set('views', path.join(__dirname, '../views'))
if (args.dev) console.log('Worker is in development mode')
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
})
}