mirror of
https://github.com/harivansh-afk/betterNAS.git
synced 2026-04-17 11:04:54 +00:00
add control plane service
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
parent
679fd504a2
commit
57f221fb72
8 changed files with 766 additions and 0 deletions
59
exapps/control-plane/src/app.ts
Normal file
59
exapps/control-plane/src/app.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
||||
import {
|
||||
CONTROL_PLANE_ROUTES,
|
||||
type ControlPlaneHealthResponse,
|
||||
type ControlPlaneVersionResponse
|
||||
} from "@ainas/contracts";
|
||||
|
||||
import type { ControlPlaneConfig } from "./config.js";
|
||||
import { NextcloudBackendAdapter } from "./adapters/nextcloud-backend.js";
|
||||
|
||||
export function createApp(config: ControlPlaneConfig) {
|
||||
const startedAt = Date.now();
|
||||
const nextcloudBackend = new NextcloudBackendAdapter(config.nextcloudBaseUrl);
|
||||
|
||||
return createServer((request, response) => {
|
||||
if (request.method !== "GET" || !request.url) {
|
||||
writeJson(response, 405, { error: "Method not allowed" });
|
||||
return;
|
||||
}
|
||||
|
||||
const url = new URL(request.url, "http://localhost");
|
||||
|
||||
if (url.pathname === CONTROL_PLANE_ROUTES.health) {
|
||||
const payload: ControlPlaneHealthResponse = {
|
||||
service: "control-plane",
|
||||
status: "ok",
|
||||
timestamp: new Date().toISOString(),
|
||||
uptimeSeconds: Math.floor((Date.now() - startedAt) / 1000),
|
||||
nextcloud: nextcloudBackend.describe()
|
||||
};
|
||||
|
||||
writeJson(response, 200, payload);
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === CONTROL_PLANE_ROUTES.version) {
|
||||
const payload: ControlPlaneVersionResponse = {
|
||||
service: "control-plane",
|
||||
version: config.version,
|
||||
apiVersion: "v1"
|
||||
};
|
||||
|
||||
writeJson(response, 200, payload);
|
||||
return;
|
||||
}
|
||||
|
||||
writeJson(response, 404, {
|
||||
error: "Not found"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function writeJson(response: ServerResponse<IncomingMessage>, statusCode: number, payload: unknown) {
|
||||
response.writeHead(statusCode, {
|
||||
"content-type": "application/json; charset=utf-8"
|
||||
});
|
||||
response.end(JSON.stringify(payload));
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue