forked from ufosc/Club_Website_2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (78 loc) · 2.66 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require('dotenv').config()
require('./auth/passport')
const bodyParser = require('body-parser')
const config = require('./utils/config')
const express = require('express')
const db = require('./utils/database')
const rateLimit = require('express-rate-limit')
const CacheModule = require('./utils/cache')
const passport = require('passport')
const auth = require('./auth/auth')
const session = require('express-session')
const cookieParser = require('cookie-parser')
const { UserModel } = require('./model/users')
const { BlogModel } = require('./model/blog')
const { ImageModel } = require('./model/images')
const { callbacks } = require('./utils/callbacks')
const app = express()
const http = require('http').createServer(app)
const apiRoute = require('./routes/api')
const cache = new CacheModule()
cache.register(...callbacks)
cache.start(config.cache_interval)
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())
app.use(rateLimit(config.limiter))
app.use(express.static('public'))
app.use(express.json())
app.use(session({ secret: config.secret }))
app.use(passport.initialize())
app.use(passport.session())
app.use('/api', apiRoute)
app.get('/', (req, res) => {
res.send(cache.cache().indexPage)
})
app.post('/auth/login', (req, res) => {
passport.authenticate('login', { session: false }, (error, user, info) => {
if (error || !user) {
app.set('loginError', info.message)
return res.redirect(`/${config.admin_route}`)
}
req.login(user, { session: false }, (loginError) => {
if (loginError) res.send(loginError)
return res.cookie('jwt', auth.tokenizeUser(user)).redirect(`/${config.admin_route}`)
})
})(req, res)
})
app.get(`/${config.admin_route}`, (req, res) => {
passport.authenticate('loggedIn', { session: false }, async (error, user, info) => {
if (error || !user) {
res.render('login', { loginError: app.get('loginError'), version: config.VERSION })
app.set('loginError', null)
return
}
const users = await UserModel.find().sort({ isAdmin: -1 }).exec()
const blog = await BlogModel.find()
const images = await ImageModel.find().sort({ date: -1 })
return res.render('admin', {
users: (users) || [],
blog: (blog) || [],
images: (images) || [],
version: config.VERSION
})
})(req, res)
})
app.get('/blog', (req, res) => {
res.send(cache.cache().blogPage)
})
app.get('/*', (req, res) => {
res.render('404', { version: config.VERSION })
})
db.connect()
http.listen(config.port, () => console.log(`Server running on port ${config.port}`))
const stop = () => {
http.close()
db.disconnect()
}
module.exports = { app, stop }