93 lines
No EOL
2.9 KiB
JavaScript
93 lines
No EOL
2.9 KiB
JavaScript
//Packages
|
|
const http = require('http');
|
|
const url = require('url');
|
|
const mysql = require('mysql2');
|
|
|
|
//MySQL Database Connection
|
|
const con = mysql.createConnection({
|
|
host: "bsu-gimm260-fall-2021.cwtgn0g8zxfm.us-west-2.rds.amazonaws.com",
|
|
user: "KAJFORNEY",
|
|
password: "IR6wvgVwqWDjw7umdcnSRMuD52lAdsBF1e9",
|
|
database: 'KAJFORNEY'
|
|
});
|
|
|
|
//Parameters for server
|
|
const hostname = '127.0.0.1';
|
|
const port = 8888;
|
|
|
|
const server = http.createServer((request, response) => {
|
|
//GET parameters (e.g. www.google.com?search=how-to-programming&)
|
|
const getParameters = url.parse(request.url, true).query;
|
|
|
|
//SELECT statement variables
|
|
let selectSql = 'SELECT game_title, timestamp, verdict, systemInfo_os, systemInfo_cpu, systemInfo_gpu, notes_summary FROM protonReports',
|
|
whereStatements = [],
|
|
orderByStatements = [],
|
|
queryParameters = [];
|
|
|
|
if (typeof getParameters.working !== 'undefined' && parseInt(getParameters.working) === 1) {
|
|
whereStatements.push("verdict != 0");
|
|
}
|
|
|
|
if (typeof getParameters.gameTitle !== 'undefined') {
|
|
const gameTitle = getParameters.gameTitle;
|
|
whereStatements.push('game_title LIKE ?');
|
|
queryParameters.push('%' + gameTitle + '%');
|
|
}
|
|
|
|
if (typeof getParameters.os !== 'undefined') {
|
|
const distro = getParameters.os;
|
|
whereStatements.push('systemInfo_os LIKE ?');
|
|
queryParameters.push('%' + distro + '%');
|
|
}
|
|
|
|
if (typeof getParameters.cpu !== 'undefined') {
|
|
const cpu = getParameters.cpu;
|
|
whereStatements.push('systemInfo_cpu LIKE ?');
|
|
queryParameters.push('%' + cpu + '%');
|
|
}
|
|
|
|
if (typeof getParameters.gpu !== 'undefined') {
|
|
const gpu = getParameters.gpu;
|
|
whereStatements.push('systemInfo_gpu LIKE ?');
|
|
queryParameters.push('%' + gpu + '%');
|
|
}
|
|
|
|
if (typeof getParameters.sortTime !== 'undefined') {
|
|
const sortTime = getParameters.sortTime;
|
|
if (sortTime === 'ASC') {
|
|
orderByStatements.push('timestamp ASC');
|
|
} else if (sortTime === 'DESC') {
|
|
orderByStatements.push('timestamp DESC')
|
|
}
|
|
}
|
|
|
|
//Dynamically add WHERE expressions to SELECT statements if needed
|
|
if (whereStatements.length > 0) {
|
|
selectSql = selectSql + ' WHERE ' + whereStatements.join(' AND ');
|
|
}
|
|
|
|
//Dynamically add ORDER BY expressions to SELECT statements if needed
|
|
if (orderByStatements.length > 0) {
|
|
selectSql = selectSql + ' ORDER BY ' + orderByStatements.join(', ');
|
|
}
|
|
|
|
console.log(selectSql);
|
|
con.query(selectSql, queryParameters, function (err, result) {
|
|
if (err) {
|
|
throw err;
|
|
} else {
|
|
response.writeHead(
|
|
200, { //HTTP Code 200 = Everything is OK
|
|
'Content-Type': 'text/json', //Setting return type of result to JSON
|
|
'Access-Control-Allow-Origin': '*' //Prevent CORS error
|
|
}).end(
|
|
JSON.stringify({
|
|
'result': 200,
|
|
'reports' : result
|
|
}));
|
|
}
|
|
});
|
|
}).listen(port, hostname, () => {
|
|
console.log(`Server running at http://${hostname}:${port}/`);
|
|
}); |