2018-04-13 16:20:57 +00:00
|
|
|
const config = require('./config')
|
|
|
|
const api = require('./routes/api')
|
|
|
|
const album = require('./routes/album')
|
|
|
|
const nojs = require('./routes/nojs')
|
2018-01-23 20:06:30 +00:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const db = require('knex')(config.database)
|
|
|
|
const fs = require('fs')
|
2018-04-18 21:00:36 +00:00
|
|
|
const helmet = require('helmet')
|
|
|
|
const nunjucks = require('nunjucks')
|
|
|
|
const RateLimit = require('express-rate-limit')
|
2018-01-23 20:06:30 +00:00
|
|
|
const safe = express()
|
|
|
|
|
|
|
|
require('./database/db.js')(db)
|
|
|
|
|
|
|
|
fs.existsSync('./pages/custom') || fs.mkdirSync('./pages/custom')
|
|
|
|
fs.existsSync('./' + config.logsFolder) || fs.mkdirSync('./' + config.logsFolder)
|
|
|
|
fs.existsSync('./' + config.uploads.folder) || fs.mkdirSync('./' + config.uploads.folder)
|
2018-04-08 18:30:25 +00:00
|
|
|
fs.existsSync('./' + config.uploads.folder + '/chunks') || fs.mkdirSync('./' + config.uploads.folder + '/chunks')
|
2018-01-23 20:06:30 +00:00
|
|
|
fs.existsSync('./' + config.uploads.folder + '/thumbs') || fs.mkdirSync('./' + config.uploads.folder + '/thumbs')
|
|
|
|
fs.existsSync('./' + config.uploads.folder + '/zips') || fs.mkdirSync('./' + config.uploads.folder + '/zips')
|
|
|
|
|
|
|
|
safe.use(helmet())
|
|
|
|
safe.set('trust proxy', 1)
|
|
|
|
|
2018-04-18 21:00:36 +00:00
|
|
|
nunjucks.configure('views', {
|
|
|
|
autoescape: true,
|
|
|
|
express: safe
|
|
|
|
})
|
|
|
|
safe.set('view engine', 'njk')
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.enable('view cache')
|
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
const limiter = new RateLimit({ windowMs: 5000, max: 2 })
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.use('/api/login/', limiter)
|
|
|
|
safe.use('/api/register/', limiter)
|
|
|
|
|
|
|
|
safe.use(bodyParser.urlencoded({ extended: true }))
|
|
|
|
safe.use(bodyParser.json())
|
2017-01-19 05:37:35 +00:00
|
|
|
|
2018-04-25 07:30:38 +00:00
|
|
|
const setHeaders = res => {
|
|
|
|
// Apply Cache-Control to all static files
|
|
|
|
res.set('Access-Control-Allow-Origin', '*')
|
|
|
|
res.set('Cache-Control', 'public, max-age=2592000, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800') // max-age: 30 days
|
2018-01-24 16:04:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 22:48:26 +00:00
|
|
|
if (config.serveFilesWithNode) {
|
2018-01-24 16:04:21 +00:00
|
|
|
safe.use('/', express.static(config.uploads.folder, { setHeaders }))
|
2017-10-03 22:48:26 +00:00
|
|
|
}
|
2017-09-20 06:03:31 +00:00
|
|
|
|
2018-01-24 16:04:21 +00:00
|
|
|
safe.use('/', express.static('./public', { setHeaders }))
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.use('/', album)
|
2018-04-12 14:37:42 +00:00
|
|
|
safe.use('/', nojs)
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.use('/api', api)
|
2017-01-14 06:01:23 +00:00
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
for (const page of config.pages) {
|
2018-01-23 20:06:30 +00:00
|
|
|
if (fs.existsSync(`./pages/custom/${page}.html`)) {
|
2018-04-18 21:00:36 +00:00
|
|
|
safe.get(`/${page}`, (req, res, next) => res.sendFile(`${page}.html`, { root: './pages/custom/' }))
|
2018-04-20 21:39:06 +00:00
|
|
|
} else if (page === 'home') {
|
2018-05-11 14:34:13 +00:00
|
|
|
safe.get('/', (req, res, next) => res.render('home', { urlMaxSize: config.uploads.urlMaxSize }))
|
2018-01-23 20:06:30 +00:00
|
|
|
} else {
|
2018-04-20 21:39:06 +00:00
|
|
|
safe.get(`/${page}`, (req, res, next) => res.render(page))
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-02-06 03:06:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 18:13:17 +00:00
|
|
|
safe.use((req, res, next) => {
|
2018-04-28 17:26:39 +00:00
|
|
|
res.status(404).sendFile('404.html', { root: './pages/error/' })
|
2018-01-24 18:31:31 +00:00
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
safe.use((error, req, res, next) => {
|
|
|
|
console.error(error)
|
2018-04-28 17:26:39 +00:00
|
|
|
res.status(500).sendFile('500.html', { root: './pages/error/' })
|
2018-01-24 18:13:17 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
safe.listen(config.port, () => console.log(`lolisafe started on port ${config.port}`))
|
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
process.on('uncaughtException', error => {
|
2018-05-10 17:25:52 +00:00
|
|
|
console.error('Uncaught Exception:')
|
|
|
|
console.error(error)
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
process.on('unhandledRejection', error => {
|
2018-05-10 17:25:52 +00:00
|
|
|
console.error('Unhandled Rejection (Promise):')
|
|
|
|
console.error(error)
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|