Add Swagger OpenAPI page

This commit is contained in:
mmckeen
2026-04-28 15:53:40 +00:00
parent 225b3c32bf
commit 9317a2bf7b
5 changed files with 527 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
import { NextResponse } from "next/server"
import { buildOpenApiSpec } from "@/app/lib/openapiSpec"
function baseUrlFromRequest(request: Request) {
const forwardedProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim()
const forwardedHost = request.headers.get("x-forwarded-host")?.split(",")[0]?.trim()
const host = forwardedHost || request.headers.get("host")
if (!host) return ""
const protocol = forwardedProto || new URL(request.url).protocol.replace(":", "") || "http"
return `${protocol}://${host}`
}
export async function GET(request: Request) {
return NextResponse.json(buildOpenApiSpec(baseUrlFromRequest(request)))
}
+10
View File
@@ -317,6 +317,16 @@ export default function Sidebar({
<span className="text-sm max-lg:text-[11px]">Help</span>
</button>
<a
href="/swagger"
className="mt-1 flex w-full items-center gap-3 rounded px-3 py-2.5 text-[var(--foreground-dim)] transition-colors hover:bg-[var(--background-tertiary)] hover:text-[var(--foreground)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--nvidia-green)] max-lg:mt-0 max-lg:min-w-24 max-lg:flex-col max-lg:justify-center max-lg:gap-1 max-lg:px-2 max-lg:py-2"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
<path strokeLinecap="square" strokeLinejoin="miter" strokeWidth={2} d="M7 7h10M7 12h10M7 17h6M5 3h14a2 2 0 012 2v14a2 2 0 01-2 2H5a2 2 0 01-2-2V5a2 2 0 012-2z" />
</svg>
<span className="text-sm max-lg:text-[11px]">Swagger</span>
</a>
<a
href="/setup-account"
className="mt-1 flex w-full items-center gap-3 rounded px-3 py-2.5 text-[var(--foreground-dim)] transition-colors hover:bg-[var(--background-tertiary)] hover:text-[var(--foreground)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--nvidia-green)] max-lg:mt-0 max-lg:min-w-24 max-lg:flex-col max-lg:justify-center max-lg:gap-1 max-lg:px-2 max-lg:py-2"
+361
View File
@@ -0,0 +1,361 @@
export function buildOpenApiSpec(baseUrl = "") {
const serverUrl = baseUrl || "http://localhost:3000"
return {
openapi: "3.1.0",
info: {
title: "OpenShell Control API",
version: "0.1.0",
description: "Controller-node deployment and registry APIs for OpenShell Control.",
},
servers: [
{
url: serverUrl,
description: "Current OpenShell Control instance",
},
],
security: [{ sessionCookie: [] }],
tags: [
{
name: "Controller Nodes",
description: "Plan, deploy, register, and rename remote OpenShell Control controller nodes.",
},
],
paths: {
"/api/controller-node/plan": {
post: {
tags: ["Controller Nodes"],
summary: "Generate a remote controller-node launch kit",
description: "Builds a bootstrap script, environment block, systemd unit, and readiness checks for a remote controller node. The generated node is also registered locally.",
operationId: "createControllerNodePlan",
requestBody: {
required: true,
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ControllerPlanRequest" },
examples: {
manual: {
summary: "Manual deploy plan",
value: {
controllerName: "remote-controller-01",
controllerHost: "203.0.113.10",
sshTarget: "ubuntu@203.0.113.10",
installDir: "/opt/openshell-control",
repoUrl: "https://github.com/mmckeen-nv/openshell_controller.git",
dashboardPort: 3000,
terminalPort: 3011,
openClawDashboardUrl: "http://127.0.0.1:18789/",
openshellGateway: "nemoclaw",
parentControllerUrl: "http://localhost:3100",
},
},
},
},
},
},
responses: {
"200": {
description: "Launch kit generated",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ControllerPlanResponse" },
},
},
},
"400": { $ref: "#/components/responses/BadRequest" },
"401": { $ref: "#/components/responses/Unauthorized" },
},
},
},
"/api/controller-node/deploy": {
post: {
tags: ["Controller Nodes"],
summary: "Autodeploy a controller node over SSH",
description: "Connects to a VPS over SSH, optionally elevates with sudo, runs the generated bootstrap, and registers the node locally. The remote password is used once and is not persisted.",
operationId: "autodeployControllerNode",
requestBody: {
required: true,
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ControllerDeployRequest" },
examples: {
sudo: {
summary: "Autodeploy with sudo and pinned host key",
value: {
controllerName: "remote-controller-01",
controllerHost: "203.0.113.10",
parentControllerUrl: "http://localhost:3100",
remoteHost: "203.0.113.10",
remotePort: 22,
remoteUser: "ubuntu",
remotePassword: "one-time-password",
allowSudo: true,
expectedHostKeySha256: "SHA256:replace-with-remote-host-fingerprint",
},
},
},
},
},
},
responses: {
"200": {
description: "Remote deploy completed",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ControllerDeployResponse" },
},
},
},
"400": { $ref: "#/components/responses/BadRequest" },
"401": { $ref: "#/components/responses/Unauthorized" },
},
},
},
"/api/controller-node/registry": {
get: {
tags: ["Controller Nodes"],
summary: "List managed controller nodes",
operationId: "listControllerNodes",
responses: {
"200": {
description: "Managed nodes",
content: {
"application/json": {
schema: {
type: "object",
properties: {
nodes: {
type: "array",
items: { $ref: "#/components/schemas/ControllerNode" },
},
},
required: ["nodes"],
},
},
},
},
"401": { $ref: "#/components/responses/Unauthorized" },
},
},
post: {
tags: ["Controller Nodes"],
summary: "Register or rename a controller node",
description: "Upserts a controller node, or renames an existing node when action is rename.",
operationId: "updateControllerNodeRegistry",
requestBody: {
required: true,
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ControllerRegistryRequest" },
examples: {
rename: {
summary: "Rename a node",
value: {
action: "rename",
nodeId: "remote-controller-01",
name: "Lab VPS East",
},
},
},
},
},
},
responses: {
"200": {
description: "Registry updated",
content: {
"application/json": {
schema: {
type: "object",
properties: {
node: { $ref: "#/components/schemas/ControllerNode" },
nodes: {
type: "array",
items: { $ref: "#/components/schemas/ControllerNode" },
},
},
required: ["node", "nodes"],
},
},
},
},
"400": { $ref: "#/components/responses/BadRequest" },
"401": { $ref: "#/components/responses/Unauthorized" },
},
},
},
"/api/openapi": {
get: {
tags: ["Controller Nodes"],
summary: "Fetch the OpenAPI document",
operationId: "getOpenApiSpec",
responses: {
"200": {
description: "OpenAPI 3.1 document",
content: {
"application/json": {
schema: {
type: "object",
additionalProperties: true,
},
},
},
},
},
},
},
},
components: {
securitySchemes: {
sessionCookie: {
type: "apiKey",
in: "cookie",
name: process.env.OPENSHELL_CONTROL_COOKIE_NAME || "openshell_control_session",
},
},
responses: {
BadRequest: {
description: "Invalid request",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ErrorResponse" },
},
},
},
Unauthorized: {
description: "Authentication required",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ErrorResponse" },
},
},
},
},
schemas: {
ErrorResponse: {
type: "object",
properties: {
ok: { type: "boolean", example: false },
error: { type: "string" },
},
required: ["error"],
},
ControllerNode: {
type: "object",
properties: {
id: { type: "string", example: "remote-controller-01" },
name: { type: "string", example: "Lab VPS East" },
host: { type: "string", example: "203.0.113.10" },
url: { type: "string", format: "uri", example: "http://203.0.113.10:3000" },
role: { type: "string", enum: ["local", "controller-node"] },
status: { type: "string", enum: ["local", "configured"] },
updatedAt: { type: "string", format: "date-time" },
},
required: ["id", "name", "host", "url", "role", "status", "updatedAt"],
},
ControllerPlanRequest: {
type: "object",
properties: {
controllerName: { type: "string", example: "remote-controller-01" },
controllerHost: { type: "string", example: "203.0.113.10" },
sshTarget: { type: "string", example: "ubuntu@203.0.113.10" },
installDir: { type: "string", example: "/opt/openshell-control" },
repoUrl: { type: "string", example: "https://github.com/mmckeen-nv/openshell_controller.git" },
dashboardPort: { type: "integer", minimum: 1, maximum: 65535, example: 3000 },
terminalPort: { type: "integer", minimum: 1, maximum: 65535, example: 3011 },
openClawDashboardUrl: { type: "string", format: "uri", example: "http://127.0.0.1:18789/" },
openshellGateway: { type: "string", example: "nemoclaw" },
parentControllerUrl: { type: "string", format: "uri", example: "http://localhost:3100" },
existingToken: { type: "string", description: "Optional existing controller shared secret." },
},
required: ["controllerHost"],
},
ControllerDeployRequest: {
allOf: [
{ $ref: "#/components/schemas/ControllerPlanRequest" },
{
type: "object",
properties: {
remoteHost: { type: "string", example: "203.0.113.10" },
remotePort: { type: "integer", minimum: 1, maximum: 65535, example: 22 },
remoteUser: { type: "string", example: "ubuntu" },
remotePassword: { type: "string", format: "password" },
allowSudo: { type: "boolean", default: false },
acceptUnknownHostKey: { type: "boolean", default: false },
expectedHostKeySha256: { type: "string", example: "SHA256:replace-with-remote-host-fingerprint" },
},
required: ["remoteUser", "remotePassword"],
},
],
},
ControllerPlanResponse: {
type: "object",
properties: {
ok: { type: "boolean", example: true },
controller: {
type: "object",
additionalProperties: true,
},
env: { type: "string" },
serviceUnit: { type: "string" },
commands: {
type: "object",
properties: {
ssh: { type: "string" },
localBootstrap: { type: "string" },
start: { type: "string" },
terminal: { type: "string" },
},
required: ["ssh", "localBootstrap", "start", "terminal"],
},
checks: {
type: "array",
items: { type: "string" },
},
},
required: ["ok", "controller", "env", "serviceUnit", "commands", "checks"],
},
ControllerDeployResponse: {
allOf: [
{ $ref: "#/components/schemas/ControllerPlanResponse" },
{
type: "object",
properties: {
hostKeySha256: { type: ["string", "null"] },
stdout: { type: "string" },
stderr: { type: "string" },
note: { type: "string" },
},
required: ["hostKeySha256", "stdout", "stderr", "note"],
},
],
},
ControllerRegistryRequest: {
oneOf: [
{
type: "object",
properties: {
action: { type: "string", const: "rename" },
nodeId: { type: "string" },
name: { type: "string" },
},
required: ["action", "nodeId", "name"],
},
{
type: "object",
properties: {
action: { type: "string", const: "upsert" },
id: { type: "string" },
name: { type: "string" },
host: { type: "string" },
url: { type: "string", format: "uri" },
role: { type: "string", enum: ["local", "controller-node"] },
},
required: ["host", "url"],
},
],
},
},
},
}
}
+114
View File
@@ -0,0 +1,114 @@
import Link from "next/link"
import { buildOpenApiSpec } from "@/app/lib/openapiSpec"
type OpenApiSpec = ReturnType<typeof buildOpenApiSpec>
function methodClass(method: string) {
const normalized = method.toLowerCase()
if (normalized === "get") return "border-blue-500/40 bg-blue-500/10 text-blue-300"
if (normalized === "post") return "border-[var(--nvidia-green)]/50 bg-[var(--nvidia-green)]/10 text-[var(--nvidia-green)]"
return "border-[var(--border-subtle)] bg-[var(--background-tertiary)] text-[var(--foreground-dim)]"
}
function operationRows(spec: OpenApiSpec) {
return Object.entries(spec.paths).flatMap(([path, methods]) =>
Object.entries(methods).map(([method, operation]) => ({
path,
method,
operation,
})),
)
}
export default function SwaggerPage() {
const spec = buildOpenApiSpec()
const rows = operationRows(spec)
return (
<main className="min-h-screen bg-[var(--background)] text-[var(--foreground)]">
<div className="mx-auto max-w-7xl space-y-6 p-8 max-sm:p-4">
<section className="panel p-8">
<div className="flex items-start justify-between gap-4 max-md:flex-col">
<div>
<p className="text-[10px] font-mono uppercase tracking-wider text-[var(--nvidia-green)]">OpenAPI 3.1</p>
<h1 className="mt-2 text-xl font-semibold uppercase tracking-wider text-[var(--foreground)]">Swagger</h1>
<p className="mt-2 max-w-3xl text-sm text-[var(--foreground-dim)]">
Controller-node API reference for generating launch kits, autodeploying remote nodes, and managing friendly names.
</p>
</div>
<div className="flex gap-2">
<Link href="/" className="action-button px-3 py-2">
Dashboard
</Link>
<a href="/api/openapi" className="rounded-sm bg-[var(--nvidia-green)] px-3 py-2 text-xs font-mono uppercase tracking-wider text-black">
OpenAPI JSON
</a>
</div>
</div>
</section>
<section className="grid grid-cols-1 gap-4 md:grid-cols-3">
<div className="metric p-4">
<p className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Title</p>
<p className="mt-1 font-mono text-sm text-[var(--foreground)]">{spec.info.title}</p>
</div>
<div className="metric p-4">
<p className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Version</p>
<p className="mt-1 font-mono text-sm text-[var(--foreground)]">{spec.info.version}</p>
</div>
<div className="metric p-4">
<p className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">Auth</p>
<p className="mt-1 font-mono text-sm text-[var(--foreground)]">session cookie</p>
</div>
</section>
<section className="panel overflow-hidden">
<div className="border-b border-[var(--border-subtle)] p-5">
<h2 className="text-sm font-semibold uppercase tracking-wider text-[var(--foreground)]">Endpoints</h2>
<p className="mt-1 text-xs text-[var(--foreground-dim)]">Expandable operations generated from the local OpenAPI spec.</p>
</div>
<div className="divide-y divide-[var(--border-subtle)]">
{rows.map(({ path, method, operation }) => (
<details key={`${method}-${path}`} className="group">
<summary className="flex cursor-pointer items-center justify-between gap-4 p-5 marker:content-[''] hover:bg-[var(--background-tertiary)]">
<span className="flex min-w-0 items-center gap-3">
<span className={`rounded-sm border px-2.5 py-1 text-[10px] font-mono uppercase tracking-wider ${methodClass(method)}`}>
{method}
</span>
<span className="truncate font-mono text-sm text-[var(--foreground)]">{path}</span>
</span>
<span className="truncate text-xs text-[var(--foreground-dim)]">{operation.summary}</span>
</summary>
<div className="space-y-4 border-t border-[var(--border-subtle)] bg-[var(--background-secondary)] p-5">
<p className="text-sm leading-6 text-[var(--foreground-dim)]">{operation.description || operation.summary}</p>
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4">
<h3 className="text-xs font-semibold uppercase tracking-wider text-[var(--foreground)]">Operation</h3>
<pre className="mt-3 max-h-80 overflow-auto whitespace-pre-wrap text-[11px] leading-5 text-[var(--foreground-dim)]">{JSON.stringify(operation, null, 2)}</pre>
</div>
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4">
<h3 className="text-xs font-semibold uppercase tracking-wider text-[var(--foreground)]">Curl</h3>
<pre className="mt-3 whitespace-pre-wrap text-[11px] leading-5 text-[var(--foreground-dim)]">{`${method.toUpperCase()} ${path}\nContent-Type: application/json\nCookie: ${spec.components.securitySchemes.sessionCookie.name}=...`}</pre>
</div>
</div>
</div>
</details>
))}
</div>
</section>
<section className="panel p-5">
<h2 className="text-sm font-semibold uppercase tracking-wider text-[var(--foreground)]">Schemas</h2>
<div className="mt-4 grid grid-cols-1 gap-3 lg:grid-cols-2">
{Object.entries(spec.components.schemas).map(([name, schema]) => (
<details key={name} className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)]">
<summary className="cursor-pointer p-4 font-mono text-xs text-[var(--foreground)]">{name}</summary>
<pre className="max-h-96 overflow-auto border-t border-[var(--border-subtle)] p-4 text-[11px] leading-5 text-[var(--foreground-dim)]">{JSON.stringify(schema, null, 2)}</pre>
</details>
))}
</div>
</section>
</div>
</main>
)
}
+26
View File
@@ -0,0 +1,26 @@
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
const root = process.cwd()
const [specSource, routeSource, pageSource, sidebarSource] = await Promise.all([
readFile(path.join(root, 'app/lib/openapiSpec.ts'), 'utf8'),
readFile(path.join(root, 'app/api/openapi/route.ts'), 'utf8'),
readFile(path.join(root, 'app/swagger/page.tsx'), 'utf8'),
readFile(path.join(root, 'app/components/Sidebar.tsx'), 'utf8'),
])
assert.match(specSource, /openapi:\s*"3\.1\.0"/, 'OpenAPI spec must declare version 3.1.0')
assert.match(specSource, /\/api\/controller-node\/plan/, 'spec must document controller plan endpoint')
assert.match(specSource, /\/api\/controller-node\/deploy/, 'spec must document controller deploy endpoint')
assert.match(specSource, /\/api\/controller-node\/registry/, 'spec must document controller registry endpoint')
assert.match(specSource, /ControllerDeployRequest/, 'spec must include autodeploy request schema')
assert.match(specSource, /sessionCookie/, 'spec must document session cookie auth')
assert.match(routeSource, /buildOpenApiSpec/, 'OpenAPI API route must return generated spec')
assert.match(pageSource, /OpenAPI JSON/, 'Swagger page must link to the JSON spec')
assert.match(pageSource, /Endpoints/, 'Swagger page must render endpoint docs')
assert.match(pageSource, /Schemas/, 'Swagger page must render schema docs')
assert.match(sidebarSource, /href="\/swagger"/, 'sidebar must link to Swagger page')
console.log('swagger-openapi-check: PASS swagger/openapi assertions')