UBS/routes/api.js

115 lines
4.3 KiB
JavaScript

const express = require('express');
const router = express.Router();
const mariadb = require('mariadb');
const reasonFlags = JSON.parse(process.env.REASON_FLAGS)
const { execSync } = require('child_process');
// Create a MariaDB connection pool
const pool = mariadb.createPool({
host: process.env.DB_HOST, // Replace with your database host
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER, // Replace with your database username
password: process.env.DB_PASS, // Replace with your database password
database: process.env.DB_DATABASE, // Replace with your database name
connectionLimit: 5 // Adjust connection limit as needed
});
// Route to fetch all bans
router.get('/v1/bans', async (req, res) => {
try {
// Get a connection from the pool
const connection = await pool.getConnection();
try {
// Execute the query to fetch all rows from the `bans` table
const rows = await connection.query('SELECT * FROM bans');
// Convert all timestamps into epoch
rows.forEach(row => {
row.expiresTimestamp = row.expiresTimestamp ? new Date(row.expiresTimestamp).getTime() : null
row.banTimestamp = row.banTimestamp ? new Date(row.banTimestamp).getTime() : null
row.currentServerTime = new Date().getTime();
});
// Send the results as a JSON response
res.json(rows);
} finally {
// Release the connection back to the pool
connection.release();
}
} catch (err) {
console.error('Error fetching bans:', err);
// Respond with a 500 Internal Server Error status if something goes wrong
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
}
});
router.get("/v1/ban/roblox/:uid", async (req, res) => {
if (!req.params.uid) return res.status(400).json({error: "Specify user ID!"});
try {
// Get a connection from the pool
const connection = await pool.getConnection();
try {
// Execute the query to fetch all rows from the `bans` table
const rows = await connection.query('SELECT * FROM bans WHERE robloxId = ?', [req.params.uid]);
// Convert all timestamps into epoch
rows.forEach(row => {
row.expiresTimestamp = row.expiresTimestamp ? new Date(row.expiresTimestamp).getTime() : null
row.banTimestamp = row.banTimestamp ? new Date(row.banTimestamp).getTime() : null
row.currentServerTime = new Date().getTime();
});
// Send the results as a JSON response
res.json(rows);
} finally {
// Release the connection back to the pool
connection.release();
}
} catch (err) {
console.error('Error fetching bans:', err);
// Respond with a 500 Internal Server Error status if something goes wrong
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
}
})
router.get("/v1/ban/discord/:uid", async (req, res) => {
if (!req.params.uid) return res.status(400).json({error: "Specify user ID!"});
try {
// Get a connection from the pool
const connection = await pool.getConnection();
try {
// Execute the query to fetch all rows from the `bans` table
const rows = await connection.query('SELECT * FROM bans WHERE discordId = ?', [req.params.uid]);
// Convert all timestamps into epoch
rows.forEach(row => {
row.expiresTimestamp = row.expiresTimestamp ? new Date(row.expiresTimestamp).getTime() : null
row.banTimestamp = row.banTimestamp ? new Date(row.banTimestamp).getTime() : null
row.currentServerTime = new Date().getTime();
});
// Send the results as a JSON response
res.json(rows);
} finally {
// Release the connection back to the pool
connection.release();
}
} catch (err) {
console.error('Error fetching bans:', err);
// Respond with a 500 Internal Server Error status if something goes wrong
res.status(500).json({ error: 'An error occurred while fetching the bans.' });
}
})
router.get("/v1/info", (req,res) => {
res.json({
commit_hash: execSync('git rev-parse HEAD').toString().trim(),
reasonFlags,
currentServerTime: new Date().getTime()
})
})
module.exports = router;