-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
128 lines (116 loc) · 3.75 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const express = require('express')
const cors = require('cors')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const app = express()
const users = require('./routes/users')
const workspaces = require('./routes/workspaces')
const spaces = require('./routes/spaces')
const boards = require('./routes/boards')
const tasks = require('./routes/tasks')
const attributes = require('./routes/attributes')
const notification = require('./routes/notifications')
const roles = require('./routes/roles')
const comments = require('./routes/comments')
const http = require('http').createServer(app)
const { rateLimit } = require('./utils/rateLimit')
const Board = require('./models/board')
const Workspace = require('./models/workspace')
const { wrapAsync, isLoggedIn } = require('./middleware')
const PermissionError = require('./PermissionError')
const pusher = require('./pusher')
dotenv.config()
mongoose.connect(process.env.DB_CONNECT, {}, () =>
console.log('connected to database')
)
const corsOptions = {
origin: [process.env.FRONTEND_HOST],
credentials: true,
maxAge: 86400
}
// middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.set('trust proxy', 1)
app.use(cookieParser(process.env.COOKIE_SECRET))
app.use(cors(corsOptions))
app.use(rateLimit)
app.use((req, res, next) => {
if (req.method === 'OPTIONS') {
res.setHeader('Cache-Control', 'public, max-age=86400')
res.end()
} else {
next()
}
})
app.use('/api/user', users)
app.use('/api/workspace', workspaces)
app.use('/api/space', spaces)
app.use('/api/board', boards)
app.use('/api/task', tasks)
app.use('/api/attribute', attributes)
app.use('/api/notification', notification)
app.use('/api/role', roles)
app.use('/api/comment', comments)
app.get('/api/status', (req, res) => {
res.json({
success: true,
message: 'api is online'
})
})
app.post(
'/api/pusher/auth',
isLoggedIn,
wrapAsync(async (req, res) => {
const { socket_id, channel_name } = req.body
try {
const type = channel_name.split('-')[1]
const id = channel_name.split('-')[2]
if (type === 'board') {
const board = await Board.findById(id).populate({
path: 'workspace',
populate: 'members'
})
const localUser = board.workspace.members.find(
x => x.user.toString() === req.user._id.toString()
)
if (!localUser) throw new PermissionError('READ:PUBLIC')
} else if (type === 'workspace') {
const workspace = await Workspace.findOne({
id
}).populate('members')
const localUser = workspace.members.find(
x => x.user.toString() === req.user._id.toString()
)
if (!localUser) throw new PermissionError('READ:PUBLIC')
} else {
throw new PermissionError('READ:PUBLIC')
}
const channelAuthResponse = pusher.authorizeChannel(
socket_id,
channel_name
)
res.send(channelAuthResponse)
} catch (err) {
throw new PermissionError('READ:PUBLIC')
}
})
)
app.use((err, req, res, next) => {
const {
status = 500,
message = 'Internal Server Error',
options = {}
} = err
console.log(err)
res.status(status).json({
success: false,
message,
...options
})
})
http.listen(process.env.PORT || 3001, () => {
console.log(`Listening on port ${process.env.PORT || 3001}`)
})