multica/src/console/app.controller.ts
yushen 6deac2f76a Add gateway URL configuration to Hub Console
Allow changing the gateway connection URL at runtime via a new
PUT /hub/gateway endpoint and a corresponding input form in the
console UI.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 11:01:06 +08:00

55 lines
1.1 KiB
TypeScript

import {
Controller,
Get,
Post,
Put,
Delete,
Param,
Body,
Inject,
} from "@nestjs/common";
import { Hub } from "../hub/hub.js";
@Controller("api")
export class AppController {
constructor(@Inject("HUB") private readonly hub: Hub) {}
@Get("hub")
getHub() {
return {
deviceId: this.hub.deviceId,
url: this.hub.url,
connectionState: this.hub.connectionState,
agentCount: this.hub.listAgents().length,
};
}
@Put("hub/gateway")
updateGateway(@Body() body: { url: string }) {
this.hub.reconnect(body.url);
return {
url: this.hub.url,
connectionState: this.hub.connectionState,
};
}
@Get("agents")
listAgents() {
return this.hub.listAgents().map((id) => {
const agent = this.hub.getAgent(id);
return { id, closed: agent?.closed ?? true };
});
}
@Post("agents")
createAgent(@Body() body?: { id?: string }) {
const agent = this.hub.createAgent(body?.id);
return { id: agent.id };
}
@Delete("agents/:id")
deleteAgent(@Param("id") id: string) {
const ok = this.hub.closeAgent(id);
return { ok };
}
}