Adding API endpoint for twitch highlights marker
Jenkins Production Deployment

This commit is contained in:
2021-04-20 09:40:15 +02:00
parent 4ce60bccfb
commit 02939d57a8
6 changed files with 262 additions and 9 deletions
@@ -0,0 +1,41 @@
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;
}
}
@@ -0,0 +1,39 @@
import * as dotenv from 'dotenv';
dotenv.config();
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.TWITCH_HIGHLIGHTS_DATABASE,
connectionLimit: 5
});
export const createHighlightEntry = async (req_body: any) => {
let conn;
let price: any;
try {
conn = await pool.getConnection();
const streamers = await conn.query('SELECT streamer_id FROM streamers WHERE username = ?', req_body.streamer);
let streamer_id: number = -1;
for (let row in streamers) {
if (row !== 'meta') {
streamer_id = streamers[row].streamer_id;
}
}
const params = [streamer_id, req_body.stream_id, req_body.description, req_body.timestamp, req_body.username, req_body.stream_game];
const rows = await conn.query('INSERT INTO highlights (streamer_id, stream_id, description, stream_timestamp, issuing_user, game) VALUES (?, ?, ?, ?, ?, ?)', params);
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
}