chore(pockethost): refactor edge mirror

This commit is contained in:
Ben Allfree 2024-08-02 05:48:16 -07:00
parent a049665893
commit e4b0581c17
12 changed files with 63 additions and 72 deletions

View File

@ -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<InstanceFields_WithUser | null>,
)
const setItem = (record: InstanceFields_WithUser) =>
fetch(mkEdgeMirrorUrl(`setItem`), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(record),
})
return {
getItem,
setItem,
}
})

View File

@ -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)

View File

@ -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()}`)
})
}

View File

@ -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
}

View File

@ -14,16 +14,16 @@ import {
} from '../../../../../../core'
import { MothershipAdminClientService } from '../../../../../services'
export const EdgeMirror = mkSingleton(async () => {
export type MirrorUserFields = UserFields<WithCredentials>
export type MirrorInstanceFields = InstanceFields<WithUser<MirrorUserFields>>
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<WithCredentials>
type MirrorInstanceFields = InstanceFields<WithUser<MirrorUserFields>>
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 }
})

View File

@ -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<InstanceFields_WithUser | null>,
)
return {
getInstanceByHost,
}
})

View File

@ -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)

View File

@ -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
}

View File

@ -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()}`)
})
}

View File

@ -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
}

View File

@ -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) => {