lolisafe.js 1.7 KB

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