filesafe/lolisafe.js

56 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-01-13 07:34:21 +00:00
const config = require('./config.js')
const api = require('./routes/api.js')
const album = require('./routes/album.js')
2017-01-13 07:34:21 +00:00
const express = require('express')
2017-04-27 07:04:19 +00:00
const helmet = require('helmet')
const bodyParser = require('body-parser')
2017-03-17 04:14:24 +00:00
const RateLimit = require('express-rate-limit')
2017-01-13 07:34:21 +00:00
const db = require('knex')(config.database)
const fs = require('fs')
const exphbs = require('express-handlebars')
2017-01-13 07:34:21 +00:00
const safe = express()
2017-01-29 07:19:14 +00:00
require('./database/db.js')(db)
2017-01-14 08:50:18 +00:00
fs.existsSync('./pages/custom' ) || fs.mkdirSync('./pages/custom')
2017-01-13 07:34:21 +00:00
fs.existsSync('./' + config.logsFolder) || fs.mkdirSync('./' + config.logsFolder)
2017-01-22 21:01:39 +00:00
fs.existsSync('./' + config.uploads.folder) || fs.mkdirSync('./' + config.uploads.folder)
fs.existsSync('./' + config.uploads.folder + '/thumbs') || fs.mkdirSync('./' + config.uploads.folder + '/thumbs')
2017-01-13 07:34:21 +00:00
2017-04-27 07:04:19 +00:00
safe.use(helmet())
2017-02-01 22:52:29 +00:00
safe.set('trust proxy', 1)
2017-02-01 22:46:10 +00:00
safe.engine('handlebars', exphbs({defaultLayout: 'main'}))
safe.set('view engine', 'handlebars')
safe.enable('view cache')
2017-03-17 04:14:24 +00:00
let limiter = new RateLimit({ windowMs: 5000, max: 2 })
2017-02-01 22:56:47 +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 00:35:31 +00:00
safe.use('/', express.static('./uploads'))
2017-01-14 06:01:23 +00:00
safe.use('/', express.static('./public'))
safe.use('/', album)
safe.use('/api', api)
2017-01-14 06:01:23 +00:00
2017-03-17 04:14:24 +00:00
for (let page of config.pages) {
let root = './pages/'
2017-03-17 04:14:24 +00:00
if (fs.existsSync(`./pages/custom/${page}.html`)) {
root = './pages/custom/'
2017-03-17 04:14:24 +00:00
}
if (page === 'home') {
safe.get('/', (req, res, next) => res.sendFile(`${page}.html`, { root: root }))
} else {
safe.get(`/${page}`, (req, res, next) => res.sendFile(`${page}.html`, { root: root }))
}
}
2017-01-17 23:12:58 +00:00
safe.use((req, res, next) => res.status(404).sendFile('404.html', { root: './pages/error/' }))
safe.use((req, res, next) => res.status(500).sendFile('500.html', { root: './pages/error/' }))
2017-01-13 07:34:21 +00:00
safe.listen(config.port, () => console.log(`loli-safe started on port ${config.port}`))