mirror of
https://github.com/bitinflow/server.git
synced 2026-03-13 13:35:53 +00:00
add /api/tunnels/:id/status endpoint
Provides connected_sockets information for a tunnel.
This commit is contained in:
29
server.js
29
server.js
@@ -4,6 +4,7 @@ import tldjs from 'tldjs';
|
||||
import Debug from 'debug';
|
||||
import http from 'http';
|
||||
import { hri } from 'human-readable-ids';
|
||||
import Router from 'koa-router';
|
||||
|
||||
import ClientManager from './lib/ClientManager';
|
||||
|
||||
@@ -25,23 +26,33 @@ export default function(opt) {
|
||||
const schema = opt.secure ? 'https' : 'http';
|
||||
|
||||
const app = new Koa();
|
||||
const router = new Router();
|
||||
|
||||
// api status endpoint
|
||||
app.use(async (ctx, next) => {
|
||||
const path = ctx.request.path;
|
||||
if (path !== '/api/status') {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
router.get('/api/status', async (ctx, next) => {
|
||||
const stats = manager.stats;
|
||||
|
||||
ctx.body = {
|
||||
tunnels: stats.tunnels,
|
||||
mem: process.memoryUsage(),
|
||||
};
|
||||
});
|
||||
|
||||
router.get('/api/tunnels/:id/status', async (ctx, next) => {
|
||||
const clientId = ctx.params.id;
|
||||
const client = manager.getClient(clientId);
|
||||
if (!client) {
|
||||
ctx.throw(404);
|
||||
return;
|
||||
}
|
||||
|
||||
const stats = client.stats();
|
||||
ctx.body = {
|
||||
connected_sockets: stats.connectedSockets,
|
||||
};
|
||||
});
|
||||
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
|
||||
// root endpoint
|
||||
app.use(async (ctx, next) => {
|
||||
const path = ctx.request.path;
|
||||
|
||||
Reference in New Issue
Block a user