Skip to content

Commit

Permalink
Merge pull request #17 from WXYC/fix-hanging-requests-cognito
Browse files Browse the repository at this point in the history
Fix hanging requests cognito
  • Loading branch information
AyBruno authored May 29, 2024
2 parents ab3ba0c + 4c2bb92 commit 99f56cf
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 56 deletions.
4 changes: 3 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ jwtVerifier
console.error(`Failed to hydrate JWT verifier : ${e}`);
})
.then(() => {
app.listen(port, () => {
const server = app.listen(port, () => {
console.log(`listening on port: ${port}!`);
});

server.setTimeout(5000);
});
53 changes: 29 additions & 24 deletions src/controllers/djs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ export const update: RequestHandler<object, unknown, DJQueryParams> = async (req
console.error(e);
next(e);
}

}
}
};

export const getDJInfo: RequestHandler<object, unknown, object, DJQueryParams> = async (req, res, next) => {
const { query } = req;
Expand All @@ -80,14 +79,14 @@ export const getDJInfo: RequestHandler<object, unknown, object, DJQueryParams> =
}
};

export type binQuery = {
export type binBody = {
dj_id: number;
bin_entry_id?: number;
album_id?: number;
track_title?: string;
};

export const addToBin: RequestHandler<object, unknown, binQuery> = async (req, res, next) => {
export const addToBin: RequestHandler<object, unknown, binBody> = async (req, res, next) => {
if (req.body.album_id === undefined || req.body.dj_id === undefined) {
console.error('Bad Request, Missing Album Identifier: album_id');
res.status(400).send('Bad Request, Missing DJ or album identifier: album_id');
Expand All @@ -108,14 +107,21 @@ export const addToBin: RequestHandler<object, unknown, binQuery> = async (req, r
}
};

export const deleteFromBin: RequestHandler<object, unknown, object, binQuery> = async (req, res, next) => {
export type binQuery = {
dj_id: string;
bin_entry_id?: string;
album_id?: string;
track_title?: string;
};

export const deleteFromBin: RequestHandler<object, unknown, unknown, binQuery> = async (req, res, next) => {
if (req.query.album_id === undefined || req.query.dj_id === undefined) {
console.error('Bad Request, Missing Bin Entry Identifier: album_id or dj_id');
res.status(400).send('Bad Request, Missing Bin Entry Identifier: album_id or dj_id');
} else {
try {
//check that the dj_id === dj_id of bin entry
const removed_bin_item = await DJService.removeFromBin(req.query.album_id, req.query.dj_id);
const removed_bin_item = await DJService.removeFromBin(parseInt(req.query.album_id), parseInt(req.query.dj_id));
res.status(200).json(removed_bin_item);
} catch (e) {
console.error(e);
Expand All @@ -124,13 +130,13 @@ export const deleteFromBin: RequestHandler<object, unknown, object, binQuery> =
}
};

export const getBin: RequestHandler<object, unknown, object, { dj_id: number }> = async (req, res, next) => {
export const getBin: RequestHandler<object, unknown, object, { dj_id: string }> = async (req, res, next) => {
if (req.query.dj_id === undefined) {
console.error('Bad Request, Missing DJ Identifier: dj_id');
res.status(400).send('Bad Request, Missing DJ Identifier: dj_id');
} else {
try {
const dj_bin = await DJService.getBinFromDB(req.query.dj_id);
const dj_bin = await DJService.getBinFromDB(parseInt(req.query.dj_id));
res.status(200).json(dj_bin);
} catch (e) {
console.error("Error: Failed to retrieve dj's bin");
Expand All @@ -140,35 +146,34 @@ export const getBin: RequestHandler<object, unknown, object, { dj_id: number }>
}
};


export const getPlaylistsForDJ: RequestHandler<object, unknown, object, { dj_id: number }> = async (req, res, next) => {
export const getPlaylistsForDJ: RequestHandler<object, unknown, object, { dj_id: string }> = async (req, res, next) => {
if (req.query.dj_id === undefined) {
console.error('Bad Request, Missing DJ Identifier: dj_id');
res.status(400).send('Bad Request, Missing DJ Identifier: dj_id');
console.error('Bad Request, Missing DJ Identifier: dj_id');
res.status(400).send('Bad Request, Missing DJ Identifier: dj_id');
} else {
try {
const playlists = await DJService.getPlaylistsForDJ(req.query.dj_id);
try {
const playlists = await DJService.getPlaylistsForDJ(parseInt(req.query.dj_id));
res.status(200).json(playlists);
} catch (e) {
} catch (e) {
console.error('Error: Failed to retrieve playlists');
console.error(e);
next(e);
}
}
}
};

export const getPlaylist: RequestHandler<object, unknown, object, { playlist_id: number }> = async (req, res, next) => {
export const getPlaylist: RequestHandler<object, unknown, object, { playlist_id: string }> = async (req, res, next) => {
if (req.query.playlist_id === undefined) {
console.error('Bad Request, Missing Playlist Identifier: playlist_id');
res.status(400).send('Bad Request, Missing Playlist Identifier: playlist_id');
console.error('Bad Request, Missing Playlist Identifier: playlist_id');
res.status(400).send('Bad Request, Missing Playlist Identifier: playlist_id');
} else {
try {
const playlist = await DJService.getPlaylist(req.query.playlist_id);
try {
const playlist = await DJService.getPlaylist(parseInt(req.query.playlist_id));
res.status(200).json(playlist);
} catch (e) {
} catch (e) {
console.error('Error: Failed to retrieve playlist');
console.error(e);
next(e);
}
}
}
}
};
7 changes: 5 additions & 2 deletions src/controllers/flowsheet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const getLatest: RequestHandler = async (req, res, next) => {
try {
const latest: FSEntry[] = await flowsheet_service.getEntriesByPage(0, 1);
if (latest.length) {
console.log(latest[0]);
res.status(200);
res.json(latest[0]);
} else {
Expand Down Expand Up @@ -287,7 +286,11 @@ export const getOnAir: RequestHandler = async (req, res, next) => {
// entry_id is the id of the entry to be moved
// new_position is the new position of the entry
// Positions are serialized starting at 1 and define the play order of the tracks per show
export const changeOrder: RequestHandler<object, unknown, { entry_id: number; new_position: number }> = async (req, res, next) => {
export const changeOrder: RequestHandler<object, unknown, { entry_id: number; new_position: number }> = async (
req,
res,
next
) => {
const { entry_id, new_position } = req.body;

if (entry_id === undefined || new_position === undefined) {
Expand Down
28 changes: 18 additions & 10 deletions src/controllers/library.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Request, RequestHandler } from 'express';
import { Album, Artist, NewAlbum, NewAlbumFormat, NewArtist, NewGenre, NewRotationRelease, RotationRelease } from '../db/schema';
import {
Album,
Artist,
NewAlbum,
NewAlbumFormat,
NewArtist,
NewGenre,
NewRotationRelease,
RotationRelease,
} from '../db/schema';
import * as libraryService from '../services/library.service';

type NewAlbumRequest = {
Expand Down Expand Up @@ -210,7 +219,6 @@ export const getFormats: RequestHandler = async (req, res, next) => {
};

export const addFormat: RequestHandler = async (req, res, next) => {

const { body } = req;
if (body.name === undefined) {
res.status(400).send('Bad Request, Missing Parameter: name');
Expand All @@ -228,17 +236,16 @@ export const addFormat: RequestHandler = async (req, res, next) => {
next(e);
}
}
}
};

export const getGenres: RequestHandler = async (req, res, next) => {
export const getGenres: RequestHandler = async (req, res) => {
const genres = await libraryService.getGenresFromDB();
res.status(200).json(genres);
};

export const addGenre: RequestHandler = async (req, res, next) => {
const { body } = req;
if (body.name === undefined ||
body.description === undefined) {
if (body.name === undefined || body.description === undefined) {
res.status(400).send('Bad Request, Parameters name and description are required.');
} else {
try {
Expand All @@ -247,7 +254,7 @@ export const addGenre: RequestHandler = async (req, res, next) => {
description: body.description,
plays: 0,
add_date: new Date().toISOString(),
last_modified: new Date()
last_modified: new Date(),
};

const insertion = await libraryService.insertGenre(newGenre);
Expand All @@ -261,13 +268,14 @@ export const addGenre: RequestHandler = async (req, res, next) => {
}
};

export const getAlbum: RequestHandler<object, unknown, unknown, { album_id: number }> = async (req, res, next) => {
if (req.query.album_id === undefined) {
export const getAlbum: RequestHandler<object, unknown, unknown, { album_id: string }> = async (req, res, next) => {
const { query } = req;
if (query.album_id === undefined) {
console.error('Bad Request, missing album identifier: album_id');
res.status(400).send('Bad Request, missing album identifier: album_id');
} else {
try {
const album = await libraryService.getAlbumFromDB(req.query.album_id);
const album = await libraryService.getAlbumFromDB(parseInt(query.album_id));
res.status(200).json(album);
} catch (e) {
console.error('Failed to retrieve album');
Expand Down
14 changes: 7 additions & 7 deletions src/routes/djs.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { cognitoMiddleware } from '../middleware/cognito.auth';
export const dj_route = Router();

//secure: mgmt & individual dj
dj_route.get('/', cognitoMiddleware, djsController.getDJInfo);
dj_route.get('/', cognitoMiddleware(), djsController.getDJInfo);

//secure: mgmt
dj_route.post('/register', cognitoMiddleware('station-management'), djsController.register);

dj_route.patch('/register', cognitoMiddleware, djsController.update);
dj_route.patch('/register', cognitoMiddleware(), djsController.update);

dj_route.post('/bin', cognitoMiddleware, djsController.addToBin);
dj_route.post('/bin', cognitoMiddleware(), djsController.addToBin);

dj_route.delete('/bin', cognitoMiddleware, djsController.deleteFromBin);
dj_route.delete('/bin', cognitoMiddleware(), djsController.deleteFromBin);

dj_route.get('/bin', cognitoMiddleware, djsController.getBin);
dj_route.get('/bin', cognitoMiddleware(), djsController.getBin);

dj_route.get('/playlists', cognitoMiddleware, djsController.getPlaylistsForDJ);
dj_route.get('/playlists', cognitoMiddleware(), djsController.getPlaylistsForDJ);

dj_route.get('/playlist', cognitoMiddleware, djsController.getPlaylist);
dj_route.get('/playlist', cognitoMiddleware(), djsController.getPlaylist);
10 changes: 5 additions & 5 deletions src/routes/flowsheet.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export const flowsheet_route = Router();

flowsheet_route.get('/', flowsheetController.getEntries);

flowsheet_route.post('/', cognitoMiddleware, flowsheetController.addEntry);
flowsheet_route.post('/', cognitoMiddleware(), flowsheetController.addEntry);

flowsheet_route.patch('/', cognitoMiddleware, flowsheetController.updateEntry);
flowsheet_route.patch('/', cognitoMiddleware(), flowsheetController.updateEntry);

flowsheet_route.delete('/', cognitoMiddleware, flowsheetController.deleteEntry);
flowsheet_route.delete('/', cognitoMiddleware(), flowsheetController.deleteEntry);

flowsheet_route.get('/latest', flowsheetController.getLatest);

flowsheet_route.post('/join', cognitoMiddleware, flowsheetController.joinShow);
flowsheet_route.post('/join', cognitoMiddleware(), flowsheetController.joinShow);

flowsheet_route.post('/end', cognitoMiddleware, flowsheetController.leaveShow);
flowsheet_route.post('/end', cognitoMiddleware(), flowsheetController.leaveShow);

flowsheet_route.get('/djs-on-air', flowsheetController.getDJList);

Expand Down
13 changes: 6 additions & 7 deletions src/routes/library.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { cognitoMiddleware } from '../middleware/cognito.auth';

export const library_route = Router();

//secure: discuss, should we lock down the catalog?
library_route.get('/', cognitoMiddleware, libraryController.searchForAlbum);
library_route.get('/', cognitoMiddleware(), libraryController.searchForAlbum);

//secure: mgmt
// secure: mgmt
library_route.post('/', cognitoMiddleware('station-management'), libraryController.addAlbum);

//secure: mgmt
library_route.get('/rotation', cognitoMiddleware, libraryController.getRotation);
library_route.get('/rotation', cognitoMiddleware(), libraryController.getRotation);

library_route.post('/rotation', cognitoMiddleware('station-management'), libraryController.addRotation);

Expand All @@ -20,12 +19,12 @@ library_route.patch('/rotation', cognitoMiddleware('station-management'), librar
//secure: mgmt
library_route.post('/artists', cognitoMiddleware('station-management'), libraryController.addArtist);

library_route.get('/formats', cognitoMiddleware, libraryController.getFormats);
library_route.get('/formats', cognitoMiddleware(), libraryController.getFormats);

library_route.post('/formats', cognitoMiddleware('station-management'), libraryController.addFormat);

library_route.get('/genres', cognitoMiddleware, libraryController.getGenres);
library_route.get('/genres', cognitoMiddleware(), libraryController.getGenres);

library_route.post('/genres', cognitoMiddleware('station-management'), libraryController.addGenre);

library_route.get('/info', cognitoMiddleware, libraryController.getAlbum);
library_route.get('/info', cognitoMiddleware(), libraryController.getAlbum);

0 comments on commit 99f56cf

Please sign in to comment.