From e4b0581c17aac4d1ae72facef41d708e9dfecfe4 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Fri, 2 Aug 2024 05:48:16 -0700 Subject: [PATCH] chore(pockethost): refactor edge mirror --- .../MirrorCommand/ServeCommand/client.ts | 24 --------------- .../MirrorCommand/ServeCommand/helpers.ts | 5 ---- .../MirrorCommand/ServeCommand/server.ts | 30 ------------------- .../src/cli/commands/EdgeCommand/index.ts | 2 -- .../MirrorCommand/ServeCommand/EdgeMirror.ts | 12 ++++---- .../MirrorCommand/ServeCommand/client.ts | 14 +++++++++ .../MirrorCommand/ServeCommand/helpers.ts | 5 ++++ .../MirrorCommand/ServeCommand/index.ts | 6 ++-- .../MirrorCommand/ServeCommand/server.ts | 30 +++++++++++++++++++ .../MirrorCommand/index.ts | 0 .../cli/commands/MothershipCommand/index.ts | 3 ++ packages/pockethost/src/constants.ts | 4 +-- 12 files changed, 63 insertions(+), 72 deletions(-) delete mode 100644 packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/client.ts delete mode 100644 packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/helpers.ts delete mode 100644 packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/server.ts rename packages/pockethost/src/cli/commands/{EdgeCommand => MothershipCommand}/MirrorCommand/ServeCommand/EdgeMirror.ts (92%) create mode 100644 packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/client.ts create mode 100644 packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/helpers.ts rename packages/pockethost/src/cli/commands/{EdgeCommand => MothershipCommand}/MirrorCommand/ServeCommand/index.ts (59%) create mode 100644 packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/server.ts rename packages/pockethost/src/cli/commands/{EdgeCommand => MothershipCommand}/MirrorCommand/index.ts (100%) diff --git a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/client.ts b/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/client.ts deleted file mode 100644 index 9d873e52..00000000 --- a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/client.ts +++ /dev/null @@ -1,24 +0,0 @@ -import fetch from 'node-fetch' -import { InstanceFields_WithUser, mkSingleton } from '../../../../../common' -import { mkEdgeMirrorUrl } from './helpers' - -export const EdgeMirrorClient = mkSingleton(() => { - const getItem = (host: string) => - fetch(mkEdgeMirrorUrl(`getItem`, host)).then( - (res) => res.json() as Promise, - ) - - const setItem = (record: InstanceFields_WithUser) => - fetch(mkEdgeMirrorUrl(`setItem`), { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(record), - }) - - return { - getItem, - setItem, - } -}) diff --git a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/helpers.ts b/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/helpers.ts deleted file mode 100644 index dc4cdcb2..00000000 --- a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/helpers.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { join } from 'path' -import { PH_EDGE_MIRROR_PORT } from '../../../../../constants' - -export const mkEdgeMirrorUrl = (...paths: string[]) => - join(`http://localhost:${PH_EDGE_MIRROR_PORT()}`, ...paths) diff --git a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/server.ts b/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/server.ts deleted file mode 100644 index bf82db84..00000000 --- a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/server.ts +++ /dev/null @@ -1,30 +0,0 @@ -import express from 'express' -import { LoggerService } from '../../../../../common' -import { PH_EDGE_MIRROR_PORT } from '../../../../../constants' -import { MothershipAdminClientService } from '../../../../../services' -import { EdgeMirror } from './EdgeMirror' - -export async function startInstanceMirrorServer() { - const logger = LoggerService().create(`EdgeCacheServeCommand`) - const { dbg, error, info, warn } = logger - info(`Starting`) - - await MothershipAdminClientService({}) - const cache = await EdgeMirror({}) - - const app = express() - - app.get(`/getItem/:host`, (req, res) => { - const { host } = req.params - const cached = cache.getItem(host) - if (!cached) { - info(`Cache miss for ${host}`) - return res.status(404).json(null) - } - res.json(cached) - }) - - app.listen(PH_EDGE_MIRROR_PORT(), () => { - info(`Listening on port ${PH_EDGE_MIRROR_PORT()}`) - }) -} diff --git a/packages/pockethost/src/cli/commands/EdgeCommand/index.ts b/packages/pockethost/src/cli/commands/EdgeCommand/index.ts index 88842ebc..73045c39 100644 --- a/packages/pockethost/src/cli/commands/EdgeCommand/index.ts +++ b/packages/pockethost/src/cli/commands/EdgeCommand/index.ts @@ -1,7 +1,6 @@ import { Command } from 'commander' import { DaemonCommand } from './DaemonCommand' import { FtpCommand } from './FtpCommand' -import { MirrorCommand } from './MirrorCommand' import { SyslogCommand } from './SyslogCommand' type Options = { @@ -15,6 +14,5 @@ export const EdgeCommand = () => { .addCommand(DaemonCommand()) .addCommand(FtpCommand()) .addCommand(SyslogCommand()) - .addCommand(MirrorCommand()) return cmd } diff --git a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/EdgeMirror.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/EdgeMirror.ts similarity index 92% rename from packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/EdgeMirror.ts rename to packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/EdgeMirror.ts index 2df6f444..00ab4ba6 100644 --- a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/EdgeMirror.ts +++ b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/EdgeMirror.ts @@ -14,16 +14,16 @@ import { } from '../../../../../../core' import { MothershipAdminClientService } from '../../../../../services' -export const EdgeMirror = mkSingleton(async () => { +export type MirrorUserFields = UserFields +export type MirrorInstanceFields = InstanceFields> + +export const MirrorService = mkSingleton(async () => { const { dbg, info, error } = LoggerService().create(`EdgeMirror`) info(`Initializing edge mirror`) const adminSvc = await MothershipAdminClientService() const { client } = adminSvc.client - type MirrorUserFields = UserFields - type MirrorInstanceFields = InstanceFields> - const instanceCleanupsById: { [_: InstanceId]: () => void } = {} const instancesById: { [_: InstanceId]: InstanceFields | undefined } = {} const instancesByHostName: { [_: string]: InstanceFields | undefined } = {} @@ -107,7 +107,7 @@ export const EdgeMirror = mkSingleton(async () => { dbg(`Updating instance ${record.subdomain} (${record.id})`) } - function getItem(host: string): MirrorInstanceFields | null { + function getInstanceByHost(host: string): MirrorInstanceFields | null { const instance = instancesByHostName[host] if (!instance) return null const user = usersById[instance.uid] @@ -119,5 +119,5 @@ export const EdgeMirror = mkSingleton(async () => { return { ...instance, expand: { uid: user } } } - return { getItem } + return { getInstanceByHost } }) diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/client.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/client.ts new file mode 100644 index 00000000..e0066d85 --- /dev/null +++ b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/client.ts @@ -0,0 +1,14 @@ +import fetch from 'node-fetch' +import { InstanceFields_WithUser, mkSingleton } from '../../../../../common' +import { mkMothershipMirrorUrl } from './helpers' + +export const EdgeMirrorClient = mkSingleton(() => { + const getInstanceByHost = (host: string) => + fetch(mkMothershipMirrorUrl(`instance`, `byHost`, host)).then( + (res) => res.json() as Promise, + ) + + return { + getInstanceByHost, + } +}) diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/helpers.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/helpers.ts new file mode 100644 index 00000000..3a23790c --- /dev/null +++ b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/helpers.ts @@ -0,0 +1,5 @@ +import { join } from 'path' +import { PH_MOTHERSHIP_MIRROR_PORT } from '../../../../../constants' + +export const mkMothershipMirrorUrl = (...paths: string[]) => + join(`http://localhost:${PH_MOTHERSHIP_MIRROR_PORT()}`, ...paths) diff --git a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/index.ts similarity index 59% rename from packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/index.ts rename to packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/index.ts index 62ff7dee..0aa85253 100644 --- a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/ServeCommand/index.ts +++ b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/index.ts @@ -1,5 +1,5 @@ import { Command } from 'commander' -import { startInstanceMirrorServer } from './server' +import { startMothershipMirrorServer } from './server' type Options = { debug: boolean @@ -7,9 +7,9 @@ type Options = { export const ServeCommand = () => { const cmd = new Command(`serve`) - .description(`Run an edge cache server`) + .description(`Run a mothership mirror`) .action(async (options: Options) => { - await startInstanceMirrorServer() + await startMothershipMirrorServer() }) return cmd } diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/server.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/server.ts new file mode 100644 index 00000000..7cafef6f --- /dev/null +++ b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/ServeCommand/server.ts @@ -0,0 +1,30 @@ +import express from 'express' +import { LoggerService } from '../../../../../common' +import { PH_MOTHERSHIP_MIRROR_PORT } from '../../../../../constants' +import { MothershipAdminClientService } from '../../../../../services' +import { MirrorService } from './EdgeMirror' + +export async function startMothershipMirrorServer() { + const logger = LoggerService().create(`MothershipMirrorServer`) + const { dbg, error, info, warn } = logger + info(`Starting`) + + await MothershipAdminClientService({}) + const cache = await MirrorService({}) + + const app = express() + + app.get(`/instance/byHost/:host`, (req, res) => { + const { host } = req.params + const cached = cache.getInstanceByHost(host) + if (!cached) { + info(`Cache miss for ${host}`) + return res.status(404).json(null) + } + res.json(cached) + }) + + app.listen(PH_MOTHERSHIP_MIRROR_PORT(), () => { + info(`Listening on port ${PH_MOTHERSHIP_MIRROR_PORT()}`) + }) +} diff --git a/packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/index.ts similarity index 100% rename from packages/pockethost/src/cli/commands/EdgeCommand/MirrorCommand/index.ts rename to packages/pockethost/src/cli/commands/MothershipCommand/MirrorCommand/index.ts diff --git a/packages/pockethost/src/cli/commands/MothershipCommand/index.ts b/packages/pockethost/src/cli/commands/MothershipCommand/index.ts index 4019eff6..bedde468 100644 --- a/packages/pockethost/src/cli/commands/MothershipCommand/index.ts +++ b/packages/pockethost/src/cli/commands/MothershipCommand/index.ts @@ -1,4 +1,5 @@ import { Command } from 'commander' +import { MirrorCommand } from './MirrorCommand' import { SchemaCommand } from './SchemaCommand' import { ServeCommand } from './ServeCommand' @@ -11,5 +12,7 @@ export const MothershipCommand = () => { .description(`Mothership commands`) .addCommand(ServeCommand()) .addCommand(SchemaCommand()) + .addCommand(MirrorCommand()) + return cmd } diff --git a/packages/pockethost/src/constants.ts b/packages/pockethost/src/constants.ts index 23288981..3302fa95 100644 --- a/packages/pockethost/src/constants.ts +++ b/packages/pockethost/src/constants.ts @@ -280,7 +280,7 @@ export const DOCKER_CONTAINER_HOST = () => settings().DOCKER_CONTAINER_HOST export const PH_GOBOT_ROOT = (...paths: string[]) => join(settings().PH_GOBOT_ROOT, ...paths) -export const PH_EDGE_MIRROR_PORT = () => +export const PH_MOTHERSHIP_MIRROR_PORT = () => env.get('PH_EDGE_MIRROR_PORT').default(3001).asPortNumber() export const PH_GOBOT_VERBOSITY = () => @@ -357,7 +357,7 @@ export const logConstants = () => { PH_GOBOT_ROOT, MOTHERSHIP_DATA_ROOT, MOTHERSHIP_DATA_DB, - PH_EDGE_MIRROR_PORT, + PH_EDGE_MIRROR_PORT: PH_MOTHERSHIP_MIRROR_PORT, PH_GOBOT_VERBOSITY, } forEach(vars, (v, k) => {