mirror of
https://github.com/pockethost/pockethost.git
synced 2025-06-08 15:16:40 +00:00
chore(pockethost): refactor edge mirror
This commit is contained in:
parent
a049665893
commit
e4b0581c17
@ -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,
|
|
||||||
}
|
|
||||||
})
|
|
@ -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)
|
|
@ -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()}`)
|
|
||||||
})
|
|
||||||
}
|
|
@ -1,7 +1,6 @@
|
|||||||
import { Command } from 'commander'
|
import { Command } from 'commander'
|
||||||
import { DaemonCommand } from './DaemonCommand'
|
import { DaemonCommand } from './DaemonCommand'
|
||||||
import { FtpCommand } from './FtpCommand'
|
import { FtpCommand } from './FtpCommand'
|
||||||
import { MirrorCommand } from './MirrorCommand'
|
|
||||||
import { SyslogCommand } from './SyslogCommand'
|
import { SyslogCommand } from './SyslogCommand'
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
@ -15,6 +14,5 @@ export const EdgeCommand = () => {
|
|||||||
.addCommand(DaemonCommand())
|
.addCommand(DaemonCommand())
|
||||||
.addCommand(FtpCommand())
|
.addCommand(FtpCommand())
|
||||||
.addCommand(SyslogCommand())
|
.addCommand(SyslogCommand())
|
||||||
.addCommand(MirrorCommand())
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -14,16 +14,16 @@ import {
|
|||||||
} from '../../../../../../core'
|
} from '../../../../../../core'
|
||||||
import { MothershipAdminClientService } from '../../../../../services'
|
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`)
|
const { dbg, info, error } = LoggerService().create(`EdgeMirror`)
|
||||||
|
|
||||||
info(`Initializing edge mirror`)
|
info(`Initializing edge mirror`)
|
||||||
const adminSvc = await MothershipAdminClientService()
|
const adminSvc = await MothershipAdminClientService()
|
||||||
const { client } = adminSvc.client
|
const { client } = adminSvc.client
|
||||||
|
|
||||||
type MirrorUserFields = UserFields<WithCredentials>
|
|
||||||
type MirrorInstanceFields = InstanceFields<WithUser<MirrorUserFields>>
|
|
||||||
|
|
||||||
const instanceCleanupsById: { [_: InstanceId]: () => void } = {}
|
const instanceCleanupsById: { [_: InstanceId]: () => void } = {}
|
||||||
const instancesById: { [_: InstanceId]: InstanceFields | undefined } = {}
|
const instancesById: { [_: InstanceId]: InstanceFields | undefined } = {}
|
||||||
const instancesByHostName: { [_: string]: InstanceFields | undefined } = {}
|
const instancesByHostName: { [_: string]: InstanceFields | undefined } = {}
|
||||||
@ -107,7 +107,7 @@ export const EdgeMirror = mkSingleton(async () => {
|
|||||||
dbg(`Updating instance ${record.subdomain} (${record.id})`)
|
dbg(`Updating instance ${record.subdomain} (${record.id})`)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getItem(host: string): MirrorInstanceFields | null {
|
function getInstanceByHost(host: string): MirrorInstanceFields | null {
|
||||||
const instance = instancesByHostName[host]
|
const instance = instancesByHostName[host]
|
||||||
if (!instance) return null
|
if (!instance) return null
|
||||||
const user = usersById[instance.uid]
|
const user = usersById[instance.uid]
|
||||||
@ -119,5 +119,5 @@ export const EdgeMirror = mkSingleton(async () => {
|
|||||||
return { ...instance, expand: { uid: user } }
|
return { ...instance, expand: { uid: user } }
|
||||||
}
|
}
|
||||||
|
|
||||||
return { getItem }
|
return { getInstanceByHost }
|
||||||
})
|
})
|
@ -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,
|
||||||
|
}
|
||||||
|
})
|
@ -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)
|
@ -1,5 +1,5 @@
|
|||||||
import { Command } from 'commander'
|
import { Command } from 'commander'
|
||||||
import { startInstanceMirrorServer } from './server'
|
import { startMothershipMirrorServer } from './server'
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
debug: boolean
|
debug: boolean
|
||||||
@ -7,9 +7,9 @@ type Options = {
|
|||||||
|
|
||||||
export const ServeCommand = () => {
|
export const ServeCommand = () => {
|
||||||
const cmd = new Command(`serve`)
|
const cmd = new Command(`serve`)
|
||||||
.description(`Run an edge cache server`)
|
.description(`Run a mothership mirror`)
|
||||||
.action(async (options: Options) => {
|
.action(async (options: Options) => {
|
||||||
await startInstanceMirrorServer()
|
await startMothershipMirrorServer()
|
||||||
})
|
})
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
@ -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()}`)
|
||||||
|
})
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
import { Command } from 'commander'
|
import { Command } from 'commander'
|
||||||
|
import { MirrorCommand } from './MirrorCommand'
|
||||||
import { SchemaCommand } from './SchemaCommand'
|
import { SchemaCommand } from './SchemaCommand'
|
||||||
import { ServeCommand } from './ServeCommand'
|
import { ServeCommand } from './ServeCommand'
|
||||||
|
|
||||||
@ -11,5 +12,7 @@ export const MothershipCommand = () => {
|
|||||||
.description(`Mothership commands`)
|
.description(`Mothership commands`)
|
||||||
.addCommand(ServeCommand())
|
.addCommand(ServeCommand())
|
||||||
.addCommand(SchemaCommand())
|
.addCommand(SchemaCommand())
|
||||||
|
.addCommand(MirrorCommand())
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -280,7 +280,7 @@ export const DOCKER_CONTAINER_HOST = () => settings().DOCKER_CONTAINER_HOST
|
|||||||
export const PH_GOBOT_ROOT = (...paths: string[]) =>
|
export const PH_GOBOT_ROOT = (...paths: string[]) =>
|
||||||
join(settings().PH_GOBOT_ROOT, ...paths)
|
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()
|
env.get('PH_EDGE_MIRROR_PORT').default(3001).asPortNumber()
|
||||||
|
|
||||||
export const PH_GOBOT_VERBOSITY = () =>
|
export const PH_GOBOT_VERBOSITY = () =>
|
||||||
@ -357,7 +357,7 @@ export const logConstants = () => {
|
|||||||
PH_GOBOT_ROOT,
|
PH_GOBOT_ROOT,
|
||||||
MOTHERSHIP_DATA_ROOT,
|
MOTHERSHIP_DATA_ROOT,
|
||||||
MOTHERSHIP_DATA_DB,
|
MOTHERSHIP_DATA_DB,
|
||||||
PH_EDGE_MIRROR_PORT,
|
PH_EDGE_MIRROR_PORT: PH_MOTHERSHIP_MIRROR_PORT,
|
||||||
PH_GOBOT_VERBOSITY,
|
PH_GOBOT_VERBOSITY,
|
||||||
}
|
}
|
||||||
forEach(vars, (v, k) => {
|
forEach(vars, (v, k) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user