v3.0.0/lolisafe.js

59 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-10-04 00:48:26 +02:00
const config = require('./config.js');
const api = require('./routes/api.js');
const album = require('./routes/album.js');
const express = require('express');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const RateLimit = require('express-rate-limit');
const db = require('knex')(config.database);
const fs = require('fs');
const exphbs = require('express-handlebars');
const safe = express();
2017-01-13 08:34:21 +01:00
2017-10-04 05:59:18 +02:00
require('./database/db.js')(db);
2017-01-14 09:50:18 +01:00
2017-10-04 00:48:26 +02:00
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);
fs.existsSync('./' + config.uploads.folder + '/thumbs') || fs.mkdirSync('./' + config.uploads.folder + '/thumbs');
fs.existsSync('./' + config.uploads.folder + '/zips') || fs.mkdirSync('./' + config.uploads.folder + '/zips')
2017-01-13 08:34:21 +01:00
2017-10-04 00:48:26 +02:00
safe.use(helmet());
safe.set('trust proxy', 1);
2017-02-01 23:46:10 +01:00
2017-10-04 00:48:26 +02:00
safe.engine('handlebars', exphbs({ defaultLayout: 'main' }));
safe.set('view engine', 'handlebars');
safe.enable('view cache');
2017-10-04 00:48:26 +02:00
let limiter = new RateLimit({ windowMs: 5000, max: 2 });
safe.use('/api/login/', limiter);
safe.use('/api/register/', limiter);
2017-02-01 23:56:47 +01:00
2017-10-04 00:48:26 +02:00
safe.use(bodyParser.urlencoded({ extended: true }));
safe.use(bodyParser.json());
2017-10-04 00:48:26 +02:00
if (config.serveFilesWithNode) {
safe.use('/', express.static(config.uploads.folder));
2017-10-04 00:48:26 +02:00
}
2017-10-04 00:48:26 +02:00
safe.use('/', express.static('./public'));
safe.use('/', album);
safe.use('/api', api);
2017-01-14 07:01:23 +01:00
2017-03-17 05:14:24 +01:00
for (let page of config.pages) {
2017-10-04 00:48:26 +02:00
let root = './pages/';
2017-03-17 05:14:24 +01:00
if (fs.existsSync(`./pages/custom/${page}.html`)) {
2017-10-04 00:48:26 +02:00
root = './pages/custom/';
2017-03-17 05:14:24 +01:00
}
if (page === 'home') {
2017-10-04 00:48:26 +02:00
safe.get('/', (req, res, next) => res.sendFile(`${page}.html`, { root: root }));
2017-03-17 05:14:24 +01:00
} else {
2017-10-04 00:48:26 +02:00
safe.get(`/${page}`, (req, res, next) => res.sendFile(`${page}.html`, { root: root }));
2017-03-17 05:14:24 +01:00
}
}
2017-10-04 00:48:26 +02: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 08:34:21 +01:00
2017-10-04 02:13:15 +02:00
safe.listen(config.port, () => console.log(`lolisafe started on port ${config.port}`));