From 9908f770e6064f14959602d4eea95055cc5c4906 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Mon, 26 May 2025 05:22:48 +0000 Subject: [PATCH] feat: add HandleInstanceResolve API handler --- .../src/mothership-app/pb_hooks/mothership.js | 50 +++++++++++++++ .../mothership-app/pb_hooks/mothership.pb.js | 8 +++ .../instance/api/HandleInstanceResolve.ts | 61 +++++++++++++++++++ .../src/lib/handlers/instance/hooks.ts | 8 +++ .../src/lib/handlers/instance/index.ts | 1 + 5 files changed, 128 insertions(+) create mode 100644 packages/pockethost/src/mothership-app/src/lib/handlers/instance/api/HandleInstanceResolve.ts diff --git a/packages/pockethost/src/mothership-app/pb_hooks/mothership.js b/packages/pockethost/src/mothership-app/pb_hooks/mothership.js index c4bbf879..5a21095a 100644 --- a/packages/pockethost/src/mothership-app/pb_hooks/mothership.js +++ b/packages/pockethost/src/mothership-app/pb_hooks/mothership.js @@ -22,6 +22,7 @@ __export(lib_exports, { HandleInstanceBeforeUpdate: () => HandleInstanceBeforeUpdate, HandleInstanceCreate: () => HandleInstanceCreate, HandleInstanceDelete: () => HandleInstanceDelete, + HandleInstanceResolve: () => HandleInstanceResolve, HandleInstanceUpdate: () => HandleInstanceUpdate, HandleInstanceVersionValidation: () => HandleInstanceVersionValidation, HandleInstancesResetIdle: () => HandleInstancesResetIdle, @@ -146,6 +147,54 @@ var HandleInstanceDelete = (c) => { return c.json(200, { status: "ok" }); }; +// src/lib/handlers/instance/api/HandleInstanceResolve.ts +var HandleInstanceResolve = (c) => { + const dao = $app.dao(); + const log = mkLog(`GET:instance/resolve`); + log(`***TOP OF GET`); + const host = c.queryParam("host"); + if (!host) { + throw new BadRequestError(`Host is required when resolving an instance.`); + } + { + try { + const record = $app.dao().findFirstRecordByData("instances", "cname", host); + if (record) { + return c.json(200, { instance: record }); + } + } catch (e) { + log(`${host} is not a cname`); + } + } + const [subdomain, ...junk] = host.split("."); + if (!subdomain) { + throw new BadRequestError( + `Subdomain is required when resolving an instance.` + ); + } + { + try { + const record = $app.dao().findRecordById("instances", subdomain); + if (record) { + return c.json(200, { instance: record }); + } + } catch (e) { + log(`${subdomain} is not an instance ID`); + } + } + { + try { + const record = $app.dao().findFirstRecordByData("instances", `subdomain`, subdomain); + if (record) { + return c.json(200, { instance: record }); + } + } catch (e) { + log(`${subdomain} is not a subdomain`); + } + } + throw new BadRequestError(`Instance not found.`); +}; + // ../../../../node_modules/.pnpm/@s-libs+micro-dash@18.0.0/node_modules/@s-libs/micro-dash/fesm2022/micro-dash.mjs function keysOfNonArray(object) { return object ? Object.getOwnPropertyNames(object) : []; @@ -3133,6 +3182,7 @@ var HandleVersionsRequest = (c) => { HandleInstanceBeforeUpdate, HandleInstanceCreate, HandleInstanceDelete, + HandleInstanceResolve, HandleInstanceUpdate, HandleInstanceVersionValidation, HandleInstancesResetIdle, diff --git a/packages/pockethost/src/mothership-app/pb_hooks/mothership.pb.js b/packages/pockethost/src/mothership-app/pb_hooks/mothership.pb.js index fbacac72..bf60b150 100644 --- a/packages/pockethost/src/mothership-app/pb_hooks/mothership.pb.js +++ b/packages/pockethost/src/mothership-app/pb_hooks/mothership.pb.js @@ -23,6 +23,14 @@ routerAdd( }, $apis.requireRecordAuth() ); +routerAdd( + "GET", + "/api/instance/resolve", + (c) => { + return require(`${__hooks}/mothership`).HandleInstanceResolve(c); + }, + $apis.requireAdminAuth() +); onModelBeforeCreate((e) => { return require(`${__hooks}/mothership`).HandleInstanceVersionValidation(e); }, "instances"); diff --git a/packages/pockethost/src/mothership-app/src/lib/handlers/instance/api/HandleInstanceResolve.ts b/packages/pockethost/src/mothership-app/src/lib/handlers/instance/api/HandleInstanceResolve.ts new file mode 100644 index 00000000..97984478 --- /dev/null +++ b/packages/pockethost/src/mothership-app/src/lib/handlers/instance/api/HandleInstanceResolve.ts @@ -0,0 +1,61 @@ +import { mkLog } from '$util/Logger' + +export const HandleInstanceResolve = (c: echo.Context) => { + const dao = $app.dao() + + const log = mkLog(`GET:instance/resolve`) + + log(`***TOP OF GET`) + const host = c.queryParam('host') + + if (!host) { + throw new BadRequestError(`Host is required when resolving an instance.`) + } + + { + try { + const record = $app + .dao() + .findFirstRecordByData('instances', 'cname', host) + if (record) { + return c.json(200, { instance: record }) + } + } catch (e) { + log(`${host} is not a cname`) + } + } + + const [subdomain, ...junk] = host.split('.') + + if (!subdomain) { + throw new BadRequestError( + `Subdomain is required when resolving an instance.`, + ) + } + + { + try { + const record = $app.dao().findRecordById('instances', subdomain) + if (record) { + return c.json(200, { instance: record }) + } + } catch (e) { + log(`${subdomain} is not an instance ID`) + } + } + + { + try { + const record = $app + .dao() + .findFirstRecordByData('instances', `subdomain`, subdomain) + if (record) { + return c.json(200, { instance: record }) + } + } catch (e) { + log(`${subdomain} is not a subdomain`) + } + } + + throw new BadRequestError(`Instance not found.`) +} diff --git a/packages/pockethost/src/mothership-app/src/lib/handlers/instance/hooks.ts b/packages/pockethost/src/mothership-app/src/lib/handlers/instance/hooks.ts index 5122ab71..a981e502 100644 --- a/packages/pockethost/src/mothership-app/src/lib/handlers/instance/hooks.ts +++ b/packages/pockethost/src/mothership-app/src/lib/handlers/instance/hooks.ts @@ -22,6 +22,14 @@ routerAdd( }, $apis.requireRecordAuth(), ) +routerAdd( + 'GET', + '/api/instance/resolve', + (c) => { + return require(`${__hooks}/mothership`).HandleInstanceResolve(c) + }, + $apis.requireAdminAuth(), +) /** Validate instance version */ onModelBeforeCreate((e) => { return require(`${__hooks}/mothership`).HandleInstanceVersionValidation(e) diff --git a/packages/pockethost/src/mothership-app/src/lib/handlers/instance/index.ts b/packages/pockethost/src/mothership-app/src/lib/handlers/instance/index.ts index 1d2b3119..9c30ce1f 100644 --- a/packages/pockethost/src/mothership-app/src/lib/handlers/instance/index.ts +++ b/packages/pockethost/src/mothership-app/src/lib/handlers/instance/index.ts @@ -1,5 +1,6 @@ export * from './api/HandleInstanceCreate' export * from './api/HandleInstanceDelete' +export * from './api/HandleInstanceResolve' export * from './api/HandleInstanceUpdate' export * from './bootstrap/HandleInstancesResetIdle' export * from './bootstrap/HandleMigrateInstanceVersions'