add /api/tunnels/:id/status endpoint

Provides connected_sockets information for a tunnel.
This commit is contained in:
Roman Shtylman
2018-05-16 16:41:05 -04:00
parent 317db73bdc
commit cf070d4ce9
7 changed files with 89 additions and 11 deletions

View File

@@ -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;