PAPI-7: Rewriting API structure to use routers
This commit is contained in:
		
							parent
							
								
									02939d57a8
								
							
						
					
					
						commit
						242b211d51
					
				
							
								
								
									
										17
									
								
								app.ts
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								app.ts
									
									
									
									
									
								
							| 
						 | 
					@ -10,26 +10,23 @@ if (!process.env.PORT) {
 | 
				
			||||||
    process.exit(1);
 | 
					    process.exit(1);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const port: number = parseInt(process.env.PORT as string, 10);
 | 
					const port: number = parseInt(process.env.PORT, 10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import {CommonRoutesConfig} from './src/common/common.routes.config';
 | 
					 | 
				
			||||||
// DHBW Service
 | 
					// DHBW Service
 | 
				
			||||||
import {GeneralInfoRoutes} from './src/models/dhbw-service/generalInfo.routes.config';
 | 
					import {generalInfoRouter} from "./src/models/dhbw-service/GeneralInfo.router";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Twitch Highlight Marker
 | 
					// Twitch Highlight Marker
 | 
				
			||||||
import {AddHighlightRoutes} from './src/models/twitch-highlight-marker/addHighlight.routes.config';
 | 
					import {addHighlightRouter} from "./src/models/twitch-highlight-marker/AddHighlight.router";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const app: express.Application = express();
 | 
					const app: express.Application = express();
 | 
				
			||||||
const server: http.Server = http.createServer(app);
 | 
					const server: http.Server = http.createServer(app);
 | 
				
			||||||
const routes: Array<CommonRoutesConfig> = [];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// here we are adding middleware to parse all incoming requests as JSON
 | 
					// here we are adding middleware to parse all incoming requests as JSON
 | 
				
			||||||
app.use(bodyparser.json());
 | 
					app.use(bodyparser.json());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// here we are adding the UserRoutes to our array,
 | 
					// Add routers
 | 
				
			||||||
// after sending the Express.js application object to have the routes added to our app!
 | 
					app.use('/dhbw-service/generalInfo', generalInfoRouter);
 | 
				
			||||||
routes.push(new GeneralInfoRoutes(app));
 | 
					app.use('/twitch-highlight-marker/addHighlight', addHighlightRouter);
 | 
				
			||||||
routes.push(new AddHighlightRoutes(app));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// this is a simple route to make sure everything is working properly
 | 
					// this is a simple route to make sure everything is working properly
 | 
				
			||||||
app.get('/', (req: express.Request, res: express.Response) => {
 | 
					app.get('/', (req: express.Request, res: express.Response) => {
 | 
				
			||||||
| 
						 | 
					@ -37,5 +34,5 @@ app.get('/', (req: express.Request, res: express.Response) => {
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
server.listen(port, () => {
 | 
					server.listen(port, () => {
 | 
				
			||||||
    console.log('Server listening on Port 3000');
 | 
					    console.log('Server listening on Port ' + port);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										27
									
								
								src/models/dhbw-service/GeneralInfo.router.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/models/dhbw-service/GeneralInfo.router.ts
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,27 @@
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Required External Modules and Interfaces
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					import express, {Request, Response} from 'express';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Router Definition
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export const generalInfoRouter = express.Router();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					generalInfoRouter.get('/', async (req: Request, res: Response) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        res.status(200).send(`GET generalInfo v2.1`);
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					        console.log('Error handling a request: ' + e.message);
 | 
				
			||||||
 | 
					        res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					generalInfoRouter.post('/', async (req: Request, res: Response) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        res.status(200).send(`GET generalInfo v2.1`);
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					        console.log('Error handling a request: ' + e.message);
 | 
				
			||||||
 | 
					        res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
| 
						 | 
					@ -1,20 +0,0 @@
 | 
				
			||||||
import {CommonRoutesConfig} from '../../common/common.routes.config';
 | 
					 | 
				
			||||||
import express from 'express';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export class GeneralInfoRoutes extends CommonRoutesConfig {
 | 
					 | 
				
			||||||
    constructor(app: express.Application) {
 | 
					 | 
				
			||||||
        super(app, 'GeneralInfoRoutes');
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    configureRoutes() {
 | 
					 | 
				
			||||||
        this.app.route(`/dhbw-service/generalInfo`)
 | 
					 | 
				
			||||||
            .get((req: express.Request, res: express.Response) => {
 | 
					 | 
				
			||||||
                res.status(200).send(`GET generalInfo v2`);
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
            .post((req: express.Request, res: express.Response) => {
 | 
					 | 
				
			||||||
                res.status(200).send(`POST generalInfo v2`);
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return this.app;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										45
									
								
								src/models/twitch-highlight-marker/AddHighlight.router.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/models/twitch-highlight-marker/AddHighlight.router.ts
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,45 @@
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Required External Modules and Interfaces
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					import express, {Request, Response} from 'express';
 | 
				
			||||||
 | 
					import * as AddHighlightService from "./addHighlights.service";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Router Definition
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export const addHighlightRouter = express.Router();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					addHighlightRouter.get('/', (req: Request, res: Response) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        res.status(200).send(`GET endpoint not defined.`);
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					        console.log('Error handling a request: ' + e.message);
 | 
				
			||||||
 | 
					        res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					addHighlightRouter.post('/', (req: Request, res: Response) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        // Check input params
 | 
				
			||||||
 | 
					        const body = req.body;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if(body.access_key !== process.env.TWITCH_HIGHLIGHTS_ACCESS_KEY){
 | 
				
			||||||
 | 
					            // Unauthorized, return error
 | 
				
			||||||
 | 
					            res.type('application/json');
 | 
				
			||||||
 | 
					            res.status(403).send('{"status": "error", "description": "Unauthorized."}');
 | 
				
			||||||
 | 
					        } else if(!body.streamer || !body.stream_id || !body.stream_game || !body.timestamp || !body.description || !body.username){
 | 
				
			||||||
 | 
					            // Missing params, return error
 | 
				
			||||||
 | 
					            res.type('application/json');
 | 
				
			||||||
 | 
					            res.status(400).send('{"status": "error", "description": "Missing parameters."}');
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            // Everything fine, return success
 | 
				
			||||||
 | 
					            AddHighlightService.createHighlightEntry(body);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            res.type('application/json');
 | 
				
			||||||
 | 
					            res.status(200).send('{"status": "success", "description": ""}');
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					        console.log('Error handling a request: ' + e.message);
 | 
				
			||||||
 | 
					        res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
| 
						 | 
					@ -1,41 +0,0 @@
 | 
				
			||||||
import {CommonRoutesConfig} from '../../common/common.routes.config';
 | 
					 | 
				
			||||||
import express from 'express';
 | 
					 | 
				
			||||||
import * as AddHighlightService from './addHighlights.service';
 | 
					 | 
				
			||||||
import * as dotenv from 'dotenv';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
dotenv.config();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export class AddHighlightRoutes extends CommonRoutesConfig {
 | 
					 | 
				
			||||||
    constructor(app: express.Application) {
 | 
					 | 
				
			||||||
        super(app, 'AddHighlightRoutes');
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    configureRoutes() {
 | 
					 | 
				
			||||||
        this.app.route(`/twitch-highlight-marker/addHighlight`)
 | 
					 | 
				
			||||||
            .get((req: express.Request, res: express.Response) => {
 | 
					 | 
				
			||||||
                res.status(200).send(`GET endpoint not defined.`);
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
            .post((req: express.Request, res: express.Response) => {
 | 
					 | 
				
			||||||
                // Check input params
 | 
					 | 
				
			||||||
                const body = req.body;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if(body.access_key !== process.env.TWITCH_HIGHLIGHTS_ACCESS_KEY){
 | 
					 | 
				
			||||||
                    // Unauthorized, return error
 | 
					 | 
				
			||||||
                    res.type('application/json');
 | 
					 | 
				
			||||||
                    res.status(403).send('{"status": "error", "description": "Unauthorized."}');
 | 
					 | 
				
			||||||
                } else if(!body.streamer || !body.stream_id || !body.stream_game || !body.timestamp || !body.description || !body.username){
 | 
					 | 
				
			||||||
                    // Missing params, return error
 | 
					 | 
				
			||||||
                    res.type('application/json');
 | 
					 | 
				
			||||||
                    res.status(400).send('{"status": "error", "description": "Missing parameters."}');
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    // Everything fine, return success
 | 
					 | 
				
			||||||
                    AddHighlightService.createHighlightEntry(body);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    res.type('application/json');
 | 
					 | 
				
			||||||
                    res.status(200).send('{"status": "success", "description": ""}');
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return this.app;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user