Skip to content

Commit

Permalink
feat(ur): allow mapping specific process to specific CU
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Jun 4, 2024
1 parent 2322176 commit 3387390
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
1 change: 0 additions & 1 deletion servers/ur/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const middlewareWith = middlewareWithByStrategy[config.strategy]

pipe(
(app) => app.use(cors()),
(app) => app.use(express.static(config.DUMP_PATH)),
(app) => app.get('/healthcheck', (req, res) => res.status(200).send('OK')),
middlewareWith({ ...config }),
(app) => {
Expand Down
16 changes: 12 additions & 4 deletions servers/ur/src/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { tmpdir } from 'node:os'
import { z } from 'zod'

/**
Expand All @@ -15,6 +14,11 @@ const MODE = process.env.NODE_CONFIG_ENV

if (!MODE) throw new Error('NODE_CONFIG_ENV must be defined')

const stringifiedJsonSchema = z.preprocess(
(val) => JSON.parse(val),
z.record(z.string())
)

/**
* The server config is an extension of the config required by the domain (business logic).
* This prevents our domain from being aware of the environment it is running in ie.
Expand All @@ -27,11 +31,11 @@ const serverConfigSchema = z.object({
if (typeof val === 'number') return val
return typeof val === 'string' ? parseInt(val) : -1
}, z.number().positive()),
processToHost: stringifiedJsonSchema.nullish(),
hosts: z.preprocess(
(arg) => (typeof arg === 'string' ? arg.split(',').map(str => str.trim()) : arg),
z.array(z.string().url())
),
DUMP_PATH: z.string().min(1),
aoUnit: z.enum(['cu', 'mu']),
strategy: z.enum(['proxy', 'redirect']),
subrouterUrl: z.string().nullable().optional(),
Expand All @@ -53,14 +57,16 @@ const CONFIG_ENVS = {
MODE,
port: process.env.PORT || 3005,
hosts: process.env.HOSTS || ['http://127.0.0.1:3005'],
DUMP_PATH: process.env.DUMP_PATH || tmpdir(),
processToHost: process.env.PROCESS_TO_HOST || JSON.stringify({}),

/**
* default to the CU for no hassle startup in development mode,
*
* but should consider setting explicitly in your .env
*/
aoUnit: process.env.AO_UNIT || 'cu',
strategy: process.env.STRATEGY || 'proxy',

subrouterUrl: process.env.SUBROUTER_URL,
surUrl: process.env.SUR_URL,
owners: process.env.OWNERS
Expand All @@ -69,9 +75,11 @@ const CONFIG_ENVS = {
MODE,
port: process.env.PORT || 3005,
hosts: process.env.HOSTS,
DUMP_PATH: process.env.DUMP_PATH || tmpdir(),
processToHost: process.env.PROCESS_TO_HOST || JSON.stringify({}),

aoUnit: process.env.AO_UNIT,
strategy: process.env.STRATEGY || 'proxy',

subrouterUrl: process.env.SUBROUTER_URL,
surUrl: process.env.SUR_URL,
owners: process.env.OWNERS
Expand Down
8 changes: 7 additions & 1 deletion servers/ur/src/domain.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LRUCache } from 'lru-cache'

export function bailoutWith ({ fetch, subrouterUrl, surUrl, owners }) {
export function bailoutWith ({ fetch, subrouterUrl, surUrl, owners, processToHost }) {
const cache = new LRUCache({
/**
* 10MB
Expand All @@ -13,6 +13,12 @@ export function bailoutWith ({ fetch, subrouterUrl, surUrl, owners }) {
})

return async (processId) => {
/**
* If a process has a specific mapping configured,
* then immediately return it's mapping
*/
if (processToHost && processToHost[processId]) return processToHost[processId]

/**
* All three of these must be set for the
* subrouter logic to work so if any are
Expand Down
7 changes: 7 additions & 0 deletions servers/ur/src/domain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,12 @@ describe('domain', () => {
assert(await determineHost({ processId: 'process-123', failoverAttempt: 0 }))
assert.equal(await determineHost({ processId: 'process-123', failoverAttempt: 0 }), 'http://foo.bar')
})

test('should redirect to the specific host for the process', async () => {
const bailout = bailoutWith({ processToHost: { 'process-123': 'https://specific.host' } })

const determineHost = determineHostWith({ hosts: HOSTS, cache, bailout })
assert.equal(await determineHost({ processId: 'process-123', failoverAttempt: 0 }), 'https://specific.host')
})
})
})
4 changes: 2 additions & 2 deletions servers/ur/src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import { logger } from './logger.js'

import { mountRoutesWithByAoUnit } from './routes/byAoUnit.js'

export function proxyWith ({ aoUnit, hosts, subrouterUrl, surUrl, owners }) {
export function proxyWith ({ aoUnit, hosts, subrouterUrl, surUrl, owners, processToHost }) {
const _logger = logger.child('proxy')
_logger('Configuring to reverse proxy ao %s units...', aoUnit)

const proxy = httpProxy.createProxyServer({})

const bailout = aoUnit === 'cu' ? bailoutWith({ fetch, subrouterUrl, surUrl, owners }) : undefined
const bailout = aoUnit === 'cu' ? bailoutWith({ fetch, subrouterUrl, surUrl, owners, processToHost }) : undefined
const determineHost = determineHostWith({ hosts, bailout })

async function trampoline (init) {
Expand Down
4 changes: 2 additions & 2 deletions servers/ur/src/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { logger } from './logger.js'

import { mountRoutesWithByAoUnit } from './routes/byAoUnit.js'

export function redirectWith ({ aoUnit, hosts, subrouterUrl, surUrl, owners }) {
export function redirectWith ({ aoUnit, hosts, subrouterUrl, surUrl, owners, processToHost }) {
const _logger = logger.child('redirect')
_logger('Configuring to redirect ao %s units...', aoUnit)

const bailout = aoUnit === 'cu' ? bailoutWith({ fetch, subrouterUrl, surUrl, owners }) : undefined
const bailout = aoUnit === 'cu' ? bailoutWith({ fetch, subrouterUrl, surUrl, owners, processToHost }) : undefined
const determineHost = determineHostWith({ hosts, bailout })

/**
Expand Down

0 comments on commit 3387390

Please sign in to comment.