diff --git a/src/cli/commands/index.ts b/src/cli/commands/index.ts index b4b3e7d..e1a48a5 100644 --- a/src/cli/commands/index.ts +++ b/src/cli/commands/index.ts @@ -16,3 +16,4 @@ export { unshareApp, } from './manage' export { metricsApp } from './metrics' +export { perfToggle } from './perf' diff --git a/src/cli/commands/perf.ts b/src/cli/commands/perf.ts new file mode 100644 index 0000000..2ac389b --- /dev/null +++ b/src/cli/commands/perf.ts @@ -0,0 +1,30 @@ +import color from 'ansis' +import { get, handleError, post } from '../http' + +interface PerfState { + perfTiming: boolean +} + +export async function perfToggle(onOff?: string) { + try { + if (onOff === undefined) { + // Toggle + const res = await post('/api/system/perf') + if (res) console.log(`Perf timing ${res.perfTiming ? color.green('on') : color.red('off')}`) + } else if (onOff === 'on') { + const res = await post('/api/system/perf', { on: true }) + if (res) console.log(`Perf timing ${color.green('on')}`) + } else if (onOff === 'off') { + const res = await post('/api/system/perf', { on: false }) + if (res) console.log(`Perf timing ${color.red('off')}`) + } else if (onOff === 'status') { + const res = await get('/api/system/perf') + if (res) console.log(`Perf timing is ${res.perfTiming ? color.green('on') : color.red('off')}`) + } else { + console.error('Usage: toes perf [on|off|status]') + process.exit(1) + } + } catch (error) { + handleError(error) + } +} diff --git a/src/cli/setup.ts b/src/cli/setup.ts index 55a24e8..fd167f7 100644 --- a/src/cli/setup.ts +++ b/src/cli/setup.ts @@ -24,6 +24,7 @@ import { shareApp, startApp, metricsApp, + perfToggle, stopApp, unshareApp, } from './commands' @@ -232,6 +233,13 @@ env .option('-g, --global', 'remove a global variable') .action(envRm) +program + .command('perf') + .helpGroup('Config:') + .description('Toggle request timing for proxied app requests') + .argument('[on|off|status]', 'enable, disable, or check status (toggles if omitted)') + .action(perfToggle) + // Shell program diff --git a/src/server/api/system.ts b/src/server/api/system.ts index c1db948..b156fc0 100644 --- a/src/server/api/system.ts +++ b/src/server/api/system.ts @@ -1,4 +1,5 @@ import { allApps, APPS_DIR, onChange } from '$apps' +import { perfTiming, setPerfTiming } from '../proxy' import { onHostLog } from '../tui' import { Hype } from '@because/hype' import { cpus, freemem, platform, totalmem } from 'os' @@ -283,6 +284,16 @@ onChange(collectLogs) // Subscribe to host-level log messages onHostLog(pushHostLog) +// Perf timing toggle +router.get('/perf', c => c.json({ perfTiming })) +router.post('/perf', async c => { + const body = await c.req.json<{ on?: boolean }>() + const on = body.on ?? !perfTiming + setPerfTiming(on) + console.log(`[perf] timing ${on ? 'enabled' : 'disabled'}`) + return c.json({ perfTiming: on }) +}) + // Restart server (systemd brings it back) router.post('/restart', c => { setTimeout(() => process.exit(0), 100) diff --git a/src/server/proxy.ts b/src/server/proxy.ts index 881ac47..9c50f0b 100644 --- a/src/server/proxy.ts +++ b/src/server/proxy.ts @@ -3,6 +3,9 @@ import { getAppBySubdomain } from '$apps' export type { WsData } +export let perfTiming = false +export const setPerfTiming = (on: boolean) => { perfTiming = on } + const pendingMessages = new Map, (string | ArrayBuffer | Uint8Array)[]>() const upstreams = new Map, WebSocket>() @@ -53,12 +56,18 @@ export async function proxySubdomain(subdomain: string, req: Request): Promise