lolisafe.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const config = require('./config.js');
  2. const api = require('./routes/api.js');
  3. const album = require('./routes/album.js');
  4. const express = require('express');
  5. const helmet = require('helmet');
  6. const bodyParser = require('body-parser');
  7. const RateLimit = require('express-rate-limit');
  8. const db = require('knex')(config.database);
  9. const fs = require('fs');
  10. const exphbs = require('express-handlebars');
  11. const safe = express();
  12. require('./database/db.js')(db);
  13. fs.existsSync('./pages/custom' ) || fs.mkdirSync('./pages/custom');
  14. fs.existsSync('./' + config.logsFolder) || fs.mkdirSync('./' + config.logsFolder);
  15. fs.existsSync('./' + config.uploads.folder) || fs.mkdirSync('./' + config.uploads.folder);
  16. fs.existsSync('./' + config.uploads.folder + '/thumbs') || fs.mkdirSync('./' + config.uploads.folder + '/thumbs');
  17. fs.existsSync('./' + config.uploads.folder + '/zips') || fs.mkdirSync('./' + config.uploads.folder + '/zips')
  18. safe.use(helmet());
  19. safe.set('trust proxy', 1);
  20. safe.engine('handlebars', exphbs({ defaultLayout: 'main' }));
  21. safe.set('view engine', 'handlebars');
  22. safe.enable('view cache');
  23. let limiter = new RateLimit({ windowMs: 5000, max: 2 });
  24. safe.use('/api/login/', limiter);
  25. safe.use('/api/register/', limiter);
  26. safe.use(bodyParser.urlencoded({ extended: true }));
  27. safe.use(bodyParser.json());
  28. if (config.serveFilesWithNode) {
  29. safe.use('/', express.static(config.uploads.folder));
  30. }
  31. safe.use('/', express.static('./public'));
  32. safe.use('/', album);
  33. safe.use('/api', api);
  34. for (let page of config.pages) {
  35. let root = './pages/';
  36. if (fs.existsSync(`./pages/custom/${page}.html`)) {
  37. root = './pages/custom/';
  38. }
  39. if (page === 'home') {
  40. safe.get('/', (req, res, next) => res.sendFile(`${page}.html`, { root: root }));
  41. } else {
  42. safe.get(`/${page}`, (req, res, next) => res.sendFile(`${page}.html`, { root: root }));
  43. }
  44. }
  45. safe.use((req, res, next) => res.status(404).sendFile('404.html', { root: './pages/error/' }));
  46. safe.use((req, res, next) => res.status(500).sendFile('500.html', { root: './pages/error/' }));
  47. safe.listen(config.port, () => console.log(`lolisafe started on port ${config.port}`));