This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
212 lines (176 loc) · 7.44 KB
/
server.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
require('dotenv').config({ silent: process.env.NODE_ENV === 'production' })
const express = require('express')
const bodyParser = require('body-parser')
const port = process.env.PORT || 3000
const app = express()
const morgan = require('morgan')
const mockMiddleware = function (req, res, next) {
next()
}
const localMode = (process.env.LOCAL_MODE === 'true')
const w3id = localMode ? mockMiddleware : require('w3id-middleware')
const whitelist = localMode ? mockMiddleware : require('./whitelist.js')
const keyProtect = localMode ? mockMiddleware : require('./checkAPIKey.js')
// parsing application/json
app.use(bodyParser.json())
// logging
app.use(morgan('dev'))
// load the actions
const getUser = require('./getUser.js')
const postUser = require('./postUser.js')
const postUserLogin = require('./postUserLogin.js')
const getUserChoirs = require('./getUserChoirs.js')
const getUserByEmail = require('./getUserByEmail.js')
const getChoir = require('./getChoir.js')
const getChoirMembers = require('./getChoirMembers.js')
const getChoirSong = require('./getChoirSong.js')
const deleteChoirSong = require('./deleteChoirSong.js')
const getChoirSongs = require('./getChoirSongs.js')
const postChoir = require('./postChoir.js')
const postChoirJoin = require('./postChoirJoin.js')
const deleteChoirJoin = require('./deleteChoirJoin.js')
const postChoirSong = require('./postChoirSong.js')
const postChoirSongPartName = require('./postChoirSongPartName.js')
const deleteChoirSongPartName = require('./deleteChoirSongPartName.js')
const postChoirSongPart = require('./postChoirSongPart.js')
const postChoirSongPartUpload = require('./postChoirSongPartUpload.js')
const postChoirSongPartDownload = require('./postChoirSongPartDownload.js')
const getChoirSongPart = require('./getChoirSongPart.js')
const deleteChoirSongPart = require('./deleteChoirSongPart.js')
const getChoirSongParts = require('./getChoirSongParts.js')
const postInvitation = require('./postInvitation.js')
const getInvitation = require('./getInvitation.js')
const getInvitationList = require('./getInvitationList.js')
const deleteInvitation = require('./deleteInvitation.js')
const postRender = require('./postRender.js')
const getRender = require('./getRender.js')
const getRenderDone = require('./getRenderDone.js')
// Health endpoint
app.get('/__gtg', async (req, res) => {
res.end()
})
// API Key Management Endpoints
app.use('/keys', [w3id, whitelist], require('./keyManagement.js'))
app.all('/__auth', w3id)
// API endpoints
app.get('/user', [keyProtect], async (req, res) => {
const response = await getUser(req.query)
res.status(response.statusCode).send(response.body)
})
app.post('/user', [keyProtect], async (req, res) => {
const response = await postUser(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/user/login', [keyProtect], async (req, res) => {
const response = await postUserLogin(req.body)
res.status(response.statusCode).send(response.body)
})
app.get('/user/choirs', [keyProtect], async (req, res) => {
const response = await getUserChoirs(req.query)
res.status(response.statusCode).send(response.body)
})
app.get('/user/byemail', [keyProtect], async (req, res) => {
const response = await getUserByEmail(req.query)
res.status(response.statusCode).send(response.body)
})
app.get('/choir', [keyProtect], async (req, res) => {
const response = await getChoir(req.query)
res.status(response.statusCode).send(response.body)
})
app.get('/choir/members', [keyProtect], async (req, res) => {
const response = await getChoirMembers(req.query)
res.status(response.statusCode).send(response.body)
})
app.get('/choir/songs', [keyProtect], async (req, res) => {
const response = await getChoirSongs(req.query)
res.status(response.statusCode).send(response.body)
})
app.get('/choir/song', [keyProtect], async (req, res) => {
const response = await getChoirSong(req.query)
res.status(response.statusCode).send(response.body)
})
app.delete('/choir/song', [keyProtect], async (req, res) => {
const response = await deleteChoirSong(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/choir', [keyProtect], async (req, res) => {
const response = await postChoir(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/choir/join', [keyProtect], async (req, res) => {
const response = await postChoirJoin(req.body)
res.status(response.statusCode).send(response.body)
})
app.delete('/choir/join', [keyProtect], async (req, res) => {
const response = await deleteChoirJoin(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/choir/song', [keyProtect], async (req, res) => {
const response = await postChoirSong(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/choir/songPartName', [keyProtect], async (req, res) => {
const response = await postChoirSongPartName(req.body)
res.status(response.statusCode).send(response.body)
})
app.delete('/choir/songPartName', [keyProtect], async (req, res) => {
const response = await deleteChoirSongPartName(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/choir/songpart', [keyProtect], async (req, res) => {
const response = await postChoirSongPart(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/choir/songpart/upload', [keyProtect], async (req, res) => {
const response = await postChoirSongPartUpload(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/choir/songpart/download', [keyProtect], async (req, res) => {
const response = await postChoirSongPartDownload(req.body)
res.status(response.statusCode).send(response.body)
})
app.get('/choir/songparts', [keyProtect], async (req, res) => {
const response = await getChoirSongParts(req.query)
res.status(response.statusCode).send(response.body)
})
app.get('/choir/songpart', [keyProtect], async (req, res) => {
const response = await getChoirSongPart(req.query)
res.status(response.statusCode).send(response.body)
})
app.delete('/choir/songpart', [keyProtect], async (req, res) => {
const response = await deleteChoirSongPart(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/invitation', [keyProtect], async (req, res) => {
const response = await postInvitation(req.body)
res.status(response.statusCode).send(response.body)
})
app.get('/invitation/list', [keyProtect], async (req, res) => {
const response = await getInvitationList()
res.status(response.statusCode).send(response.body)
})
app.get('/invitation', [keyProtect], async (req, res) => {
const response = await getInvitation(req.query)
res.status(response.statusCode).send(response.body)
})
app.delete('/invitation', [keyProtect], async (req, res) => {
const response = await deleteInvitation(req.body)
res.status(response.statusCode).send(response.body)
})
app.post('/render', [keyProtect], async (req, res) => {
const response = await postRender(req.body)
res.status(response.statusCode).send(response.body)
})
app.get('/render', [keyProtect], async (req, res) => {
const response = await getRender(req.query)
res.status(response.statusCode).send(response.body)
})
app.get('/render/done', [keyProtect], async (req, res) => {
const response = await getRenderDone(req.query)
res.status(response.statusCode).send(response.body)
})
// 404 everything else
app.use((req, res, next) => {
res.status(404).send({ ok: false })
})
app.listen(port, () => console.log(`Choirless API listening at http://localhost:${port}`))