slopsite/api/index.ts
2025-02-28 22:30:19 +02:00

99 lines
No EOL
3 KiB
TypeScript

/*
Ziedu Vija, armija, Ziedu Vija leģions
Pagāju garām es tavai kucei, zivju paviljons
Ēdu sēnes, šampinjons
Tu neēd neko, tu ir bomzis
Nomirs badā paļubom
Nomirs badā paļubom
2025 sad.ovh dev
*/
import express, { type Request, type Response, type NextFunction } from 'express';
import { InstantVector, PrometheusDriver, ResponseType, SampleValue } from 'prometheus-query';
import { Rcon } from 'rconts';
if(!process.env.prometheusHost) {
throw new Error("missing prometheus host")
}
if(process.env.prometheusUsername || process.env.prometheusPassword) {
if(!process.env.prometheusUsername) {
throw new Error("missing prometheus username")
}
if(!process.env.prometheusPassword) {
throw new Error("missing prometheus password")
}
}
if(!process.env.rconHost || !process.env.rconPassword) {
throw new Error("missing rcon host or rcon password")
}
const rcon = new Rcon({
host: process.env.rconHost,
password: process.env.rconPassword
});
let lastRequested = 0;
let whitelistedPlayers: string[] = [];
rcon.on("connected", () => {
console.log("RCON connected.");
});
const app = express();
//@ts-expect-error
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
const host = process.env.host || "localhost";
const port = +(process.env.port || 3000);
const prom = new PrometheusDriver({
endpoint: process.env.prometheusHost,
auth: process.env.prometheusUsername ? {
username: process.env.prometheusUsername!,
password: process.env.prometheusPassword!
} : undefined
})
app.get("/api/getTps", async (req, res) => {
const query = await prom.instantQuery(`minecraft_tps`);
const value = (query.result[0] as InstantVector).value as SampleValue;
res.json(value.value)
})
app.get("/api/getPlayersOnline", async (req, res) => {
const query = await prom.instantQuery(`sum(minecraft_players_online{instance="172.18.0.1:25585", job="prometheus"})`);
const value = (query.result[0] as InstantVector).value as SampleValue;
res.json(value.value)
})
app.get("/api/uptime", async (req, res) => {
const query = await prom.instantQuery(`process_start_time_seconds`);
const value = (query.result[0] as InstantVector).value as SampleValue;
res.json(Date.now() - (value.value*1000))
})
app.get("/api/whitelist", async (req, res) => {
if ( Date.now() - lastRequested > 60000) {
lastRequested = Date.now();
const list = await rcon.exec("whitelist list");
whitelistedPlayers = list.replace(/There are \d* whitelisted player\(s\): /gm, "").split(", ");
}
res.json(whitelistedPlayers);
})
app.listen(port, host, () => {
rcon.connect()
console.log(`Server is running on http://${host}:${port}`);
});