add /stats API endpoint
This commit is contained in:
parent
f830e99a64
commit
03b2ec5f66
3 changed files with 30 additions and 0 deletions
|
@ -27,6 +27,7 @@ export { logger, app, db, PORT, SESSION_LIFETIME }
|
|||
|
||||
import('./routes/internal/ws'),
|
||||
import('./routes/root'),
|
||||
import('./routes/stats'),
|
||||
import('./routes/login'),
|
||||
import('./routes/dash/servers'),
|
||||
import('./routes/dash/server'),
|
||||
|
|
24
api/src/routes/stats.ts
Normal file
24
api/src/routes/stats.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { app, logger } from '..';
|
||||
import { Request, Response } from 'express';
|
||||
import { botReq } from './internal/ws';
|
||||
|
||||
let SERVER_COUNT = 0;
|
||||
|
||||
const fetchStats = async () => {
|
||||
try {
|
||||
const res = await botReq('stats');
|
||||
if (!res.success) return logger.warn(`Failed to fetch bot stats: ${res.statusCode} / ${res.error}`);
|
||||
if (res.servers) SERVER_COUNT = Number(res.servers);
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
fetchStats();
|
||||
setInterval(() => fetchStats(), 10000);
|
||||
|
||||
app.get('/stats', async (req: Request, res: Response) => {
|
||||
res.send({
|
||||
servers: SERVER_COUNT,
|
||||
});
|
||||
});
|
|
@ -136,6 +136,11 @@ wsEvents.on('req:requestLogin', async (data: any, cb: (data: WSResponse) => void
|
|||
}
|
||||
});
|
||||
|
||||
wsEvents.on('req:stats', async (_data: any, cb: (data: { servers: number }) => void) => {
|
||||
const servers = bot.servers.size;
|
||||
cb({ servers });
|
||||
});
|
||||
|
||||
export { wsEvents, wsSend, WSResponse }
|
||||
|
||||
import('./api/servers');
|
||||
|
|
Loading…
Reference in a new issue