-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
676 lines (618 loc) · 21.9 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
// Import dei moduli necessari utilizzando il sistema di moduli ES6
import express from "express";
import cors from "cors";
import { config } from "./src/config/prefs.js";
import {
serve as swaggerUiServe,
setup as swaggerUiSetup,
} from "swagger-ui-express";
import 'dotenv/config'
import { login,authuser } from "./src/lib/login.js";
import { getUsers, getUser, updateUser, deleteUser } from "./src/lib/user.js";
import { Db } from "./src/lib/database.js";
import { join } from "path";
import { register } from "./src/lib/register.js";
import { search, getGenres, getRecommended, getTrack } from "./src/lib/spotify/fetch.js"
import * as playlist from "./src/lib/playlist.js";
import * as community from "./src/lib/community.js";
import * as utils from "./src/lib/utils.js";
import swaggerUi from 'swagger-ui-express';
import swaggerDocument from './src/api/docs/swagger_output.json'assert { type: 'json' };; // Specifica il percorso al tuo file Swagger JSON generato
// Creazione di un'istanza di Express per l'applicazione
const app = express();
utils.createLogFolder();
// Middleware per il parsing dei dati JSON e abilitazione del CORS
app.use(express.json());
const corsOptions = {
origin: 'http://localhost:3000', // Indirizzo del tuo frontend
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true, // Consenti l'invio dei cookie
};
// Usa il middleware cors
app.use(cors(corsOptions));
// Middleware per servire la documentazione API tramite Swagger UI
app.use('/api-docs', swaggerUiServe, swaggerUiSetup(swaggerDocument));
// Middleware per servire file statici
app.use(express.static(config.__dirname));
app.use(express.static(join(config.__dirname, "/src/")));
app.use(express.static(join(config.__dirname, "/src/config/")));
app.use(express.static(join(config.__dirname, "/src/public/")));
app.use(express.static(join(config.__dirname, "/src/html/")));
/* -------------------- FETCH ------------------- */
app.get("/playlist", async (_, res) => {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to fetch the playlists.html file'
res.sendFile(config.__dirname + "/src/html/playlists.html");
});
// Login Endpoint
app.get("/login", async (req, res) => {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to fetch the login page'
res.sendFile(config.__dirname + "/src/html/login.html");
});
// Register Endpoints
app.get("/register", async (req, res) => {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to fetch the register page'
res.sendFile(config.__dirname + "/src/html/register.html");
});
app.get("/explore", async function (_, res) {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to fetch the explore.html file'
res.sendFile(config.__dirname + "/src/html/explore.html");
});
app.get("/createplaylist", async (req, res) => {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to obtain createplaylist.html page'
res.sendFile(config.__dirname + "/src/html/createplaylist.html");
});
app.get("/community", async (_, res) => {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to obtain community.html page'
res.sendFile(config.__dirname + "/src/html/community.html");
});
app.get("/createcommunity", async (req, res) => {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to obtain createcommunity.html page'
res.sendFile(config.__dirname + "/src/html/createcommunity.html");
});
// Endpoint per la pagina principale
app.get("/", async (_, res) => {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to obtain index.html page'
res.sendFile(config.__dirname + "/src/public/index.html");
});
app.get("/profile", async function (_, res) {
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to fetch the profile page'
res.sendFile(config.__dirname + "/src/html/profile.html");
});
app.get("/editplaylist/:id", async function (req, res) {
const id = req.params.id;
// #swagger.tags = ['fetch']
// #swagger.description = 'Endpoint that allows to fetch the edit playlist page'
res.sendFile(`${config.__dirname}/src/html/editplaylist.html`);
});
/* ------------------- USERS ------------------- */
// User specific Endpoint
// CALLS FILE: USER.JS
app.get("/users/:id", async function (req, res) {
// #swagger.tags = ['users']
// #swagger.description = 'Endpoint that allows to obtain a specific user given its _id'
// #swagger.parameters['id'] = { description: 'User ID to fetch.' }
/* #swagger.responses[200] = {
schema: { $ref: "#/definitions/user" },
description: 'user found.'
} */
getUser(req, res)
});
// Endpoint for all users
// CALLS FILE: USER.JS
app.get("/users", async function (_, res) {
// #swagger.tags = ['users']
// #swagger.description = 'Endpoint that allows to fetch all users from the database'
/* #swagger.responses[200] = {
description: 'List of users.'
}
#swagger.responses[500] = {
description: 'Internal Error.'
}
*/
getUsers(res)
});
// User update Endpoint
// CALLS FILE: USER.JS
app.put("/users/:id", function (req, res) {
// #swagger.tags = ['users']
// #swagger.description = 'Endpoint that allows to update of a specific user given its _id and the new Data'
// #swagger.parameters['id'] = { description: 'User ID to be updated.' }
/* #swagger.parameters['body'] = {
in: 'body',
description: 'Body that contains updated data to be sent to the DB.',
type: 'object',
schema: { $ref: "#/definitions/updateuser" }
}
*/
/* #swagger.responses[200] = {
description: 'user updated.'
}
#swagger.responses[400] = {
description: 'Missing parameter, Invalid Parameters'
}
#swagger.responses[500] = {
description: 'Generic error'
}
*/
updateUser(res, req.body._id, req.body);
});
// User delete Endpoint
// CALLS FILE: USER.JS
app.delete("/deleteUser/:id", function (req, res) {
// #swagger.tags = ['users']
// #swagger.description = 'Endpoint that allows to delete a specific user from the database'
// #swagger.parameters['id'] = { description: 'User ID to be deleted.' }
/* #swagger.responses[200] = {
description: 'user found.'
}
#swagger.responses[200] = {
description: 'User delted succesfully'
}
#swagger.responses[400] = {
description: 'User does not exist'
}
#swagger.responses[500] = {
description: 'Internal Error'
}
*/
deleteUser(res, req.params.id);
});
/* ------------------- AUTHENTICATION ------- ------------------- */
// Login Endpoint
// CALLS FILE: LOGIN.JS
app.post("/login", async (req, res) => {
// #swagger.tags = ['auth']
// #swagger.description = 'Endpoint that allows to check if user's login data is correct and valid for logging in the application'
/* #swagger.parameters['body'] = {
in: 'body',
description: 'Body to validate login.',
type: 'object',
schema: { $ref: "#/definitions/loginrequest" }
}
*/
/*
#swagger.responses[200] = {
schema: { $ref: "#/definitions/loggeduser" },
description: 'User login data is valid'
}
#swagger.responses[401] = {
description: 'User not authorized'
}
#swagger.responses[400] = {
description: 'Data is not valid, missing parameter'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
login(req, res);
});
// Register endpoint
// CALLS FILE: REGISTER.JS
app.post("/register", function (req, res) {
// #swagger.tags = ['auth']
// #swagger.description = 'Endpoint that allows to register a new user in the database'
/* #swagger.parameters['body'] = {
in: 'body',
description: 'Body to be registered in the DB.',
type: 'object',
schema: { $ref: "#/definitions/registerrequest" }
}
*/
/* #swagger.responses[200] = {
description: 'succesfully registered.'
}
#swagger.responses[400] = {
description: 'User already exists, invalid parameter'
}
#swagger.responses[500] = {
description: 'Generic error'
}
*/
register(res, req.body);
});
// Auth user
// CALLS FILE: LOGIN.JS
app.post("/authuser", async (req, res) => {
// #swagger.tags = ['auth']
// #swagger.description = 'Endpoint that allows to verify if user tuple of _id, email and nickname are valid in the database.'
/* #swagger.parameters['body'] = {
in: 'body',
description: 'tuple used for verification',
type: 'object',
schema: { $ref: "#/definitions/authuser" }
}
*/
/* #swagger.responses[200] = {
schema: { $ref: "#/definitions/user"},
description: 'succesfully authorized.'
}
#swagger.responses[401] = {
description: 'Unauthorized'
}
#swagger.responses[401] = {
description: 'Invalid body parameter'
}
#swagger.responses[500] = {
description: 'Internal Error'
}
*/
authuser(req, res);
});
/* -------------------- PLAYLIST ------------------- */
// CHIEDERE A KEV!!
app.get("/playlists", async (req, res) => {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to obtain all public playlists'
/* #swagger.responses[200] = {
description: 'list of playlists'
}
#swagger.responses[500] = {
description: 'Server error'
}
*/
playlist.getPublicPlaylists(req, res);
});
// Gets playlist from user
// CALLS FILE : PLAYLIST.JS
app.get("/playlist/:id", async (req, res) => {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to obtain user's playlists'
// #swagger.parameters['id'] = { description: 'Id of the user we want to fetch playlists of.' }
/* #swagger.responses[200] = {
description: 'list of playlists'
}
#swagger.responses[500] = {
description: 'Server error'
}
*/
playlist.getUserPlaylists(res, req.params.id);
});
// CHIEDERE A KEV, SERVE DAVVERO?
app.put("/playlist/:id", async (req, res) => {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to edit user's playlists'
// #swagger.parameters['id'] = { description: 'Id of the user we want to edit playlists of.' }
/* #swagger.responses[200] = {
description: 'playlist updated'
}
#swagger.responses[500] = {
description: 'Server error'
}
#swagger.responses[400] = {
description: 'Invalid Parameters, Missing parameters'
}
*/
playlist.addSongToPlaylist(req, res);
});
// Adds song to playlist
// CALLS FILE : PLAYLIST.JS
app.put("/addsongtoplaylist/:id", async (req, res) => {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to add song to playlist'
// #swagger.parameters['id'] = { description: 'Id of the playlist we want to add song to.' }
/**
#swagger.parameters['body'] = {
in: 'body',
description: 'tuple used for verification',
type: 'object',
schema: { $ref: "#/definitions/song" }
}
*/
/* #swagger.responses[200] = {
description: 'playlist updated'
}
#swagger.responses[400] = {
description: 'Invalid Parameters, Missing parameters'
}
#swagger.responses[500] = {
description: 'Server error'
}
*/
playlist.addSongToPlaylist(res,req.params.id,req.body);
});
// Fetches song by song id
// CALLS FILE : PLAYLIST.JS
app.get("/getplaylist/:id", async (req, res) => {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to obtain a specific playlist from given id'
// #swagger.parameters['playlistid'] = { description: 'Id of the playlist we want to fetch.' }
/* #swagger.responses[200] = {
description: 'playlist',
schema: { $ref: "#/definitions/playlists" }
}
#swagger.responses[500] = {
description: 'Server error'
}
#swagger.responses[400] = {
description: 'Invalid parameters, Missing parameters'
}
#swagger.responses[404] = {
description: 'Playlist Not Found'
}
*/
playlist.getPlaylistFromId(res, req.params.id);
});
app.post("/getplaylist", async (req, res) => {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to obtain a specific playlist'
// #swagger.parameters['id'] = { description: 'Id of the owner.' }
// #swagger.parameters['playlistid'] = { description: 'Id of the playlist we want to fetch.' }
/* #swagger.responses[200] = {
description: 'playlist',
schema: { $ref: "#/definitions/playlists" }
}
#swagger.responses[500] = {
description: 'Server error'
}
#swagger.responses[404] = {
description: 'Playlist Not Found'
}
*/
playlist.getPlaylist(res, req.body.owner_id,req.body.id);
});
// creates palylist
// CALLS FILE : PLAYLIST.JS
app.post("/createplaylist", function (req, res) {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to create a new playlist'
/* #swagger.parameters['body'] = {
in: 'body',
description: 'tuple used for verification',
type: 'object',
schema: { $ref: "#/definitions/playlists" }
}
*/
/* #swagger.responses[200] = {
description: 'playlist created.'
}
#swagger.responses[400] = {
description: 'Error while creating the playlist or missing parameter'
}
#swagger.responses[500] = {
description: 'Server error'
}
*/
playlist.createplaylist(res, req.body);
});
// deletes palylist
// CALLS FILE : PLAYLIST.JS
app.delete("/deleteplaylist/:id", function (req, res) {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to delete a playlist given the ID'
/* #swagger.responses[200] = {
description: 'playlist deleted.'
}
#swagger.responses[400] = {
description: 'Missing parameter'
}
#swagger.responses[404] = {
description: 'Playlist not found or not valid owner'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
playlist.deletePlaylist(res,req.params.id, req.body._id);
});
// deletes song from playlist
// CALLS FILE : PLAYLIST.JS
app.delete("/deleteSongFromPlaylist", function (req, res) {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to delete song from a playlist'
/**#swagger.parameters['body'] = {
in: 'body',
description: 'parameters used to identify playlist of the user where the song to be deleted is in',
type: 'object',
schema: { $ref: "#/definitions/removesong" }
} */
/* #swagger.responses[200] = {
description: 'Song removed.'
}
#swagger.responses[400] = {
description: 'Missing parameter, Invalid parameter'
}
#swagger.responses[404] = {
description: 'Song not found'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
playlist.removeSongFromPlaylist(res,req.body.playlist_id,req.body.track_id,req.body.owner_id);
});
// updates title, description, tags and privacy
// CALLS FILE : PLAYLIST.JS
app.put("/updateplaylist/:id", function (req, res) {
// #swagger.tags = ['playlist']
// #swagger.description = 'Endpoint that allows to update some data from a playlist'
/* #swagger.responses[200] = {
description: 'playlist updated.'
}
#swagger.responses[400] = {
description: 'Missing parameter'
}
#swagger.responses[404] = {
description: 'playlist not found or not valid'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
playlist.updatePlaylist(res,req.params.id,req.body);
});
/* ------------------- TRACKS ------------------- */
app.get("/search", async function (req, res) {
// #swagger.tags = ['tracks']
// #swagger.description = 'ADD DESCRIPTION'
search(req.query, res);
})
app.get("/tracks", async function (req, res) {
// #swagger.tags = ['tracks']
// #swagger.description = 'ADD DESCRIPTION'
getRecommended(req.params, res);
})
app.get("/tracks/:id", async function (req, res) {
// #swagger.tags = ['tracks']
// #swagger.description = 'ADD DESCRIPTION'
getTrack(req.params.id, res);
})
/* -------------------- COMMUNITY ------------------- */
// fetches community data given its creator id
// CALLS FILE : COMMUNITY.JS
app.get("/community/:id", async (req, res) => {
let id = req.params.id;
// #swagger.tags = ['community']
// #swagger.description = 'Endpoint that Fetches community Data given its creator ID'
/**#swagger.parameters['id'] = {description: 'ID of the creator of the community we want to fetch',} */
/* #swagger.responses[200] = {
description: 'community fetched. returns the community data'
}
#swagger.responses[400] = {
description: 'Missing parameter, Invalid parameter'
}
#swagger.responses[404] = {
description: 'Community not found'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
community.getCommunity(id, res);
});
// fetches community into array given creator id
// CALLS FILE: COMMUNITY.JS
app.get("/communities/:id", async (req, res) => {
let id = req.params.id;
// #swagger.tags = ['community']
// #swagger.description = 'Endpoint that Fetches community Data given its creator ID'
/**#swagger.parameters['id'] = {description: 'ID of the creator of the community we want to fetch',} */
/* #swagger.responses[200] = {
description: 'community fetched. returns the community data on array format'
}
#swagger.responses[400] = {
description: 'Missing parameter, Invalid parameter'
}
#swagger.responses[404] = {
description: 'Community not found'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
community.getCommunities(id, res);
});
// fetches add playlist to community
// CALLS FILE: COMMUNITY.JS
app.put("/addplaylisttocommunity/:id", async (req, res) => {
// #swagger.tags = ['community']
// #swagger.description = 'Endpoint that adds playlist to community'
/**#swagger.parameters['id'] = {description: 'ID of the creator of the community we want to add the playlist to',} */
/* #swagger.responses[200] = {
description: 'playlist added'
}
#swagger.responses[400] = {
description: 'Missing parameter, Invalid parameter, playlist already in community'
}
#swagger.responses[404] = {
description: 'Community not found'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
community.addPlaylistToCommunity(req.body.playlist_id,req.params.id,req.body.owner_id, res);
});
// deleted the community
// CALLS FILE: COMMUNITY.JS
app.delete("/community/:id", async (req, res) => {
// #swagger.tags = ['community']
// #swagger.description = 'Endpoint that deletes community given the id'
/**#swagger.parameters['id'] = {description: 'ID of the community to be deleted',} */
/* #swagger.responses[200] = {
description: 'playlist deleted'
}
#swagger.responses[400] = {
description: 'Missing parameter, Invalid parameter'
}
#swagger.responses[404] = {
description: 'Community not found'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
community.deleteCommunity(req, res)
});
app.put("/community/:id", async (req, res) => {
// #swagger.tags = ['community']
// #swagger.description = 'Endpoint that updates the community given the community ID'
/**#swagger.parameters['id'] = {description: 'ID of the community to be updated',} */
/**#swagger.parameters['body'] = {
in: 'body',
description: 'parameters used update the community',
type: 'object',
}
} */
/* #swagger.responses[200] = {
description: 'playlist updated'
}
#swagger.responses[400] = {
description: 'Missing parameters, Invalid parameter'
}
#swagger.responses[404] = {
description: 'Community not found'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
community.updateCommunity(req, res);
});
app.post("/createcommunity", async (req, res) => {
// #swagger.tags = ['community']
// #swagger.description = 'Endpoint that is used to create a playlist given its data'
/**#swagger.parameters['id'] = {description: 'ID of the community to be updated',} */
/**#swagger.parameters['body'] = {
in: 'body',
description: 'parameters used create the community',
type: 'object',
}
} */
/* #swagger.responses[200] = {
description: 'playlist updated'
}
#swagger.responses[400] = {
description: 'Missing parameters, Invalid parameter'
}
#swagger.responses[404] = {
description: 'Community not found'
}
#swagger.responses[500] = {
description: 'Internal error'
}
*/
community.createCommunity(req, res);
});
/* ------------------- MISC ENDPOINTS ----------------- */
app.get("/getGenres", async function (_, res) {
// #swagger.tags = ['misc']
// #swagger.description = 'Endpoint that allows to fetch all genres from spotify's APIs'
getGenres(res);
});
/* ------------------- DB AND SERVER START ------------------- */
export const db = Db();
app.listen(config.port, config.host, () => {
console.log(`🟢 Server listening on port: ${config.port}`);
utils.logonly(`🟢 Server listening on port: ${config.port}`)
});
// export default app