lolisafe.js 1.8 KB

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