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

101 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-09-07 18:30:52 +00:00
import connectSession from 'connect-redis'
2019-10-23 08:44:21 +00:00
import redis from '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,
2019-10-23 08:44:21 +00:00
store: new SessionStore({ client: redis.createClient(config.redis) }),
2017-08-02 21:24:01 +00:00
resave: false,
2019-02-12 20:04:16 +00:00
saveUninitialized: true,
cookie: {
secure: process.env.NODE_ENV !== 'development',
2019-03-10 11:25:09 +00:00
maxAge: 2678400000 // 1 month
}
2017-08-02 21:24:01 +00:00
}))
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
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) {
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
})
}