Skip to content

Commit

Permalink
Merge pull request #129 from PhlexPlexico/refactor-db-calls
Browse files Browse the repository at this point in the history
Refactor Database Calls
  • Loading branch information
PhlexPlexico authored Dec 10, 2021
2 parents f883169 + 8a0d8c5 commit 01789d5
Show file tree
Hide file tree
Showing 28 changed files with 1,412 additions and 1,462 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# G5API - API Backend for Get5
_**Status: Under active development.**_

G5API is a replacement for the get5-webpanel. This is the backend only, as it will allow the plugin to interface with a database and Steam OAuth, as well as make various calls to functionality that is seen in the [get5-webpanel](https://github.com/phlexplexico/get5-webpanel).
G5API is a replacement for the get5-webpanel. This is the backend only, as it will allow the plugin to interface with a database and Steam OAuth, as well as make various calls to functionality that is seen in the [get5-web](https://github.com/phlexplexico/get5-web).

If you would like a supplemental front-end, please see [Get5Vue](https://github.com/phlexplexico/g5v).
If you would like a supplemental front-end, please see [Get5Vue](https://github.com/phlexplexico/g5v) or use the [docker-compose](https://github.com/PhlexPlexico/G5API/blob/master/docker-compose.yml) located in the repo!


## What does it do?
G5API is an API that will allow users to create, manage, and control Counter-Strike: Global Offensive matches. Add teams, create matches, and most importantly, track statistics across the entire use of the platform, and create Seasons/Tournaments and track on those.
G5API is an API that will allow users to create, manage, and control Counter-Strike: Global Offensive matches. Add teams, create matches, and most importantly, track statistics across matches, and create Seasons/Tournaments to track stats within date ranges.

This API should be complete enough to provide the most functionality out of [get5](https://github.com/splewis/get5).
This API is complete enough to provide the most functionality out of [get5](https://github.com/splewis/get5).

For the [get5_api plugin](https://github.com/PhlexPlexico/get5-webapi), the routes currently put into place located in the `./routes/legacy/` and still point to `/match/` on this app.
For the plugin [G5WS](https://github.com/PhlexPlexico/G5WS), the routes currently put into place located in the `./routes/legacy/` and still point to `/match/` on this app.

Game server interaction will still take place under the `/matches/:match_id` directive, but the logic can be found under `./matches/matchserver.js`.

The webapi plugin for this can be downloaded from [here](https://github.com/PhlexPlexico/get5-webapi) to include the use of vetoes being recorded, as well as demoes being uploaded to the API once the match is complete.
The webapi plugin for this can be downloaded from [here](https://github.com/PhlexPlexico/G5WS) to include the use of vetoes being recorded, as well as demoes being uploaded to the API server once the match is complete.

## What does it NOT do?
This is simply a back-end to get myself used to JavaScript and Node. You will need a [front end](https://github.com/phlexplexico/g5v) or create something that can make it work!

## Why?
[Get5-webpanel](https://github.com/phlexplexico/get5-webpanel) is a now out-dated webpanel, with python2.7 being officially EOL. Being built all on Flask, with ORM (SQLAlchemy), and Jinja2, its tech spans more than a few years old. While it works really well for now, it is becoming increasingly harder to deploy to more modern hardware/software (such as Ubuntu 19) to ensure easy setup.
[Get5-web](https://github.com/phlexplexico/get5-web) is a now out-dated webpanel, with python2.7 being officially EOL. Being built all on Flask, with ORM (SQLAlchemy), and Jinja2, its tech spans more than a few years old. While it works really well for now, it is becoming increasingly harder to deploy to more modern hardware/software (such as Ubuntu 19) to ensure easy setup.

The intent will to be provide similar functionality with the use of NodeJS and Express, and this API will take care of session authentication as well, via the use of [`passport-steam`](https://github.com/liamcurry/passport-steam), and rcon server commands via [`rcon`](https://github.com/pushrax/node-rcon), as well as more normalization in the database.

Expand All @@ -47,7 +47,7 @@ This will attempt to update a production database by creating any tables that do
### Build and run:
```yarn start```

Spins up a development server (by default, please use the `NODE_ENV` variable to change this) where you can make all your calls. Since steam authentication is enabled, you will need to auth with steam first before making any calls that would modify any data.
Spins up a development server (by default, please use the `NODE_ENV` variable to change this) where you can make all your calls. Since steam authentication is enabled, you will need to auth with Steam first before making any calls that would modify any data.

### Docker Build Instructions:
This guide assumes you have a MariaDB or MySQL compatible server running. You can deploy one in docker following the [MariaDB guide](https://hub.docker.com/_/mariadb/).
Expand Down Expand Up @@ -83,7 +83,7 @@ This guide will get you to setup a running instance for a reverse proxy (Caddy),
Provided in this repository is a `docker-compose.yml` file. Much like the above Docker run commands, all those fields are required in the docker file, as well as a few additional parameters.
- `CADDY_URL` is the URL that you wish to host everything.
- `CADDY_REVERSE_PROXY_PORT` is the port that the API is serving to. By default, this is set to 3301.
- `CADDY_API_ENDPOINT` is the end point that you wish to route from. By default, this is `api`.
- `CADDY_API_ENDPOINT` is the end point that you wish to route from. By default, this is `api`. This should only be swapped if you have a different G5V build you are pointing to.
- And then your mysql information is required in that file as well.

Once those are all filled in, you need to run two more commands. The first being the network bridge, which is done via `docker network create -d bridge get5`. After this, you can simply run `docker-compose up -d`.This will spin up a production instance of Get5Vue, as well as Get5API, and a reverse proxy to link everything together!
Expand All @@ -102,7 +102,7 @@ Steam OAuth will be mocked in order to check if a user is "logged in", and creat
Will *require* `test.json` to exist in projects `config` folder. It will grab the value from `./utility/mockProfile.js` to set it as a `super_admin` temporarily, then remove it after. These tests are mainly meant for CI, and will be the go-to to test if any changes break the application.

## Contribution
Sure! If you have a knack for APIs and a penchant for JavaScript, I could always use help! Create a fork of this application, make your changes, and submit a PR. I will be using the [Issues](https://github.com/g5api/issues) page to track what calls still need to be completed. This project won't be finished anytime soon, as I would like to make sure there is a proper handle on authentication with the API, as well as proper security implemented to prevent any unwanted uses with the application.
If you have a knack for APIs and a penchant for JavaScript, I could always use help! Create a fork of this application, make your changes, and submit a PR. I will be using the [Issues](https://github.com/phlexplexico/G5API/issues) page to track what calls still need to be completed. Even though this project is "complete" in a sense of it does what is on the tin, I wouldn't be opposed to new features or suggestions on how to make the API better!

If you so choose to contribute, please make sure you include documentation for the API calls, as it is how I am keeping track of all the functionality. I'm using [JSDoc](https://devdocs.io/jsdoc/) as well as [Swagger](https://swagger.io) to provide documentation. Please read over some of the files to get accustomed to usage.

Expand Down
51 changes: 9 additions & 42 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ class Database {

async query(sql, args) {
try {
const connection = await this.getConnection();
let result;
await this.withNewTransaction(connection, async () => {
result = await connection.query(sql, args);
});
result = await connPool.query(sql, args);
return result[0];
} catch (error) {
console.log("SQL ERROR SQL ERROR SQL ERROR SQL ERROR SQL ERROR\n" + error);
Expand All @@ -38,47 +35,17 @@ class Database {
return objValues;
}

async getConnection () {
return await connPool.getConnection();
}
/** Inner function - boilerplate transaction call.
* @name withNewTransaction
* @function
* @inner
* @memberof module:routes/vetoes
* @param {*} singleCall - The connection from the database pool.
* @param {*} callback - The callback function that is operated on, usually a db.query()
*/
async withNewTransaction(singleCall, callback) {
await singleCall.beginTransaction();
let isDestroyed = false;
try {
await callback();
await singleCall.commit();
} catch (err) {
await singleCall.rollback();
isDestroyed = true;
throw err;
} finally {
if (isDestroyed) await singleCall.destroy();
else await singleCall.release();
}
}

async setupAdmins() {
try {
let singleConn = await this.getConnection();
await this.withNewTransaction(singleConn, async () => {
let listOfAdmins = config.get("admins.steam_ids").split(',');
let listofSuperAdmins = config.get("super_admins.steam_ids").split(',');
// Get list of admins from database and compare list and add new admins.
let updateAdmins = "UPDATE user SET admin = 1 WHERE steam_id IN (?)";
let updateSuperAdmins = "UPDATE user SET super_admin = 1 WHERE steam_id in(?)";
await singleConn.query(updateAdmins, [listOfAdmins]);
await singleConn.query(updateSuperAdmins, [listofSuperAdmins]);
});
let listOfAdmins = config.get("admins.steam_ids").split(',');
let listofSuperAdmins = config.get("super_admins.steam_ids").split(',');
// Get list of admins from database and compare list and add new admins.
let updateAdmins = "UPDATE user SET admin = 1 WHERE steam_id IN (?)";
let updateSuperAdmins = "UPDATE user SET super_admin = 1 WHERE steam_id in(?)";
await connPool.query(updateAdmins, [listOfAdmins]);
await connPool.query(updateSuperAdmins, [listofSuperAdmins]);
} catch (err) {
console.log("Failed to import users. Error: " + err);
console.log("Failed to import users. " + err);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion migrations/development/20210124162658-get5db.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ exports.down = function(db, callback) {
};

exports._meta = {
"version": 14
"version": 13
};
2 changes: 1 addition & 1 deletion migrations/development/20210201015955-get5db.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ exports.down = function(db, callback) {
};

exports._meta = {
"version": 13
"version": 14
};
32 changes: 32 additions & 0 deletions migrations/development/20211125150429-get5db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var dbm;
var type;
var seed;
var async = require('async');

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function(db, callback) {
async.series([
db.runSql('ALTER TABLE player_stats ADD COLUMN team_name varchar(64) AFTER mvp;')
], callback());
};

exports.down = function(db, callback) {
async.series([
db.removeColumn('player_stats', 'team_name'),
], callback());
};

exports._meta = {
"version": 15
};
2 changes: 1 addition & 1 deletion migrations/production/20210124162658-get5db.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ exports.down = function(db, callback) {
};

exports._meta = {
"version": 14
"version": 13
};
2 changes: 1 addition & 1 deletion migrations/production/20210201015955-get5db.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ exports.down = function(db, callback) {
};

exports._meta = {
"version": 13
"version": 14
};
32 changes: 32 additions & 0 deletions migrations/production/20211125150429-get5db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var dbm;
var type;
var seed;
var async = require('async');

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function(db, callback) {
async.series([
db.runSql('ALTER TABLE player_stats ADD COLUMN team_name varchar(64) AFTER mvp;')
], callback());
};

exports.down = function(db, callback) {
async.series([
db.removeColumn('player_stats', 'team_name'),
], callback());
};

exports._meta = {
"version": 15
};
2 changes: 1 addition & 1 deletion migrations/test/20210124162658-get5db.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ exports.down = function(db, callback) {
};

exports._meta = {
"version": 14
"version": 13
};
2 changes: 1 addition & 1 deletion migrations/test/20210201015955-get5db.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ exports.down = function(db, callback) {
};

exports._meta = {
"version": 13
"version": 14
};
32 changes: 32 additions & 0 deletions migrations/test/20211125150429-get5db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var dbm;
var type;
var seed;
var async = require('async');

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function(db, callback) {
async.series([
db.runSql('ALTER TABLE player_stats ADD COLUMN team_name varchar(64) AFTER mvp;')
], callback());
};

exports.down = function(db, callback) {
async.series([
db.removeColumn('player_stats', 'team_name'),
], callback());
};

exports._meta = {
"version": 15
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "g5api",
"version": "1.3.0.0",
"version": "1.4.0.0",
"private": true,
"licenses": [
{
Expand Down
2 changes: 1 addition & 1 deletion routes/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,4 @@
return allPlayers;
};
module.exports = router;


Loading

0 comments on commit 01789d5

Please sign in to comment.