@defra/lis-hubs-infra-core 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/get-service-base-url.js +45 -0
- package/src/get-service-base-url.test.js +95 -0
- package/src/index.js +1 -0
- package/src/proxy-plugin.js +6 -41
- package/src/proxy-plugin.test.js +3 -2
package/package.json
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the base URL for a service, depending on where this process is
|
|
3
|
+
* running. On `docker_compose`, this reaches the service via the host
|
|
4
|
+
* machine's network stack (host.docker.internal), so it only works if the
|
|
5
|
+
* service's port is published to the host - which every service in this
|
|
6
|
+
* project's compose files is.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} environment 'local' | 'docker_compose' | 'dev' | 'test' | 'ext-test' | 'perf-test' | 'prod'
|
|
9
|
+
* @param {string} serviceName the service's repo name, e.g. 'lis-apps-cattle-home'
|
|
10
|
+
* @param {number} [localPort] the port the service listens on locally - required for 'local' and 'docker_compose'
|
|
11
|
+
* @returns {URL} the service's base URL
|
|
12
|
+
*/
|
|
13
|
+
export const getServiceBaseUrl = (environment, serviceName, localPort) => {
|
|
14
|
+
let base
|
|
15
|
+
|
|
16
|
+
switch (environment) {
|
|
17
|
+
case 'local':
|
|
18
|
+
base = `http://localhost:${requireLocalPort(localPort, environment)}`
|
|
19
|
+
break
|
|
20
|
+
case 'docker_compose':
|
|
21
|
+
base = `http://host.docker.internal:${requireLocalPort(localPort, environment)}`
|
|
22
|
+
break
|
|
23
|
+
case 'dev':
|
|
24
|
+
case 'test':
|
|
25
|
+
case 'ext-test':
|
|
26
|
+
case 'perf-test':
|
|
27
|
+
case 'prod':
|
|
28
|
+
base = `https://${serviceName}.${environment}.cdp-int.defra.cloud`
|
|
29
|
+
break
|
|
30
|
+
default:
|
|
31
|
+
throw new Error(`Unsupported environment: ${environment}`)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return new URL(base)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function requireLocalPort(localPort, environment) {
|
|
38
|
+
if (!localPort) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`getServiceBaseUrl requires a localPort for the ${environment} environment`
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return localPort
|
|
45
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { getServiceBaseUrl } from './get-service-base-url.js'
|
|
4
|
+
|
|
5
|
+
describe('getServiceBaseUrl()', () => {
|
|
6
|
+
test('resolves to localhost with the given port for local', () => {
|
|
7
|
+
// Arrange
|
|
8
|
+
const environment = 'local'
|
|
9
|
+
const serviceName = 'lis-apps-cattle-home'
|
|
10
|
+
const localPort = 3221
|
|
11
|
+
|
|
12
|
+
// Act
|
|
13
|
+
const baseUrl = getServiceBaseUrl(environment, serviceName, localPort)
|
|
14
|
+
|
|
15
|
+
// Assert
|
|
16
|
+
expect(baseUrl.href).toBe('http://localhost:3221/')
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
test('resolves to host.docker.internal with the given port for docker_compose', () => {
|
|
20
|
+
// Arrange
|
|
21
|
+
const environment = 'docker_compose'
|
|
22
|
+
const serviceName = 'lis-apps-cattle-home'
|
|
23
|
+
const localPort = 3221
|
|
24
|
+
|
|
25
|
+
// Act
|
|
26
|
+
const baseUrl = getServiceBaseUrl(environment, serviceName, localPort)
|
|
27
|
+
|
|
28
|
+
// Assert
|
|
29
|
+
expect(baseUrl.href).toBe('http://host.docker.internal:3221/')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test.each([
|
|
33
|
+
['dev', 'https://lis-apps-cattle-home.dev.cdp-int.defra.cloud/'],
|
|
34
|
+
['test', 'https://lis-apps-cattle-home.test.cdp-int.defra.cloud/'],
|
|
35
|
+
['ext-test', 'https://lis-apps-cattle-home.ext-test.cdp-int.defra.cloud/'],
|
|
36
|
+
[
|
|
37
|
+
'perf-test',
|
|
38
|
+
'https://lis-apps-cattle-home.perf-test.cdp-int.defra.cloud/'
|
|
39
|
+
],
|
|
40
|
+
['prod', 'https://lis-apps-cattle-home.prod.cdp-int.defra.cloud/']
|
|
41
|
+
])('resolves to the CDP hostname for %s', (environment, expectedHref) => {
|
|
42
|
+
// Arrange
|
|
43
|
+
const serviceName = 'lis-apps-cattle-home'
|
|
44
|
+
const localPort = 3221
|
|
45
|
+
|
|
46
|
+
// Act
|
|
47
|
+
const baseUrl = getServiceBaseUrl(environment, serviceName, localPort)
|
|
48
|
+
|
|
49
|
+
// Assert
|
|
50
|
+
expect(baseUrl.href).toBe(expectedHref)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test.each(['local', 'docker_compose'])(
|
|
54
|
+
'throws when localPort is missing for %s',
|
|
55
|
+
(environment) => {
|
|
56
|
+
// Arrange
|
|
57
|
+
const serviceName = 'lis-apps-cattle-home'
|
|
58
|
+
|
|
59
|
+
// Act
|
|
60
|
+
let result, error
|
|
61
|
+
try {
|
|
62
|
+
result = getServiceBaseUrl(environment, serviceName)
|
|
63
|
+
} catch (e) {
|
|
64
|
+
error = e
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Assert
|
|
68
|
+
expect(result).not.toBeDefined()
|
|
69
|
+
expect(error).toBeInstanceOf(Error)
|
|
70
|
+
expect(error.message).toBe(
|
|
71
|
+
`getServiceBaseUrl requires a localPort for the ${environment} environment`
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
test('throws for an unsupported environment', () => {
|
|
77
|
+
// Arrange
|
|
78
|
+
const environment = 'unknown'
|
|
79
|
+
const serviceName = 'lis-apps-cattle-home'
|
|
80
|
+
const localPort = 3221
|
|
81
|
+
|
|
82
|
+
// Act
|
|
83
|
+
let result, error
|
|
84
|
+
try {
|
|
85
|
+
result = getServiceBaseUrl(environment, serviceName, localPort)
|
|
86
|
+
} catch (e) {
|
|
87
|
+
error = e
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Assert
|
|
91
|
+
expect(result).not.toBeDefined()
|
|
92
|
+
expect(error).toBeInstanceOf(Error)
|
|
93
|
+
expect(error.message).toBe('Unsupported environment: unknown')
|
|
94
|
+
})
|
|
95
|
+
})
|
package/src/index.js
CHANGED
package/src/proxy-plugin.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import h2o2 from '@hapi/h2o2'
|
|
2
2
|
|
|
3
3
|
import { getModulesForHub } from '@defra/lis-hubs-infra-registry'
|
|
4
|
+
import { getServiceBaseUrl } from './get-service-base-url.js'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* @param {{ hubId: string, environment: string }} options
|
|
@@ -18,21 +19,11 @@ export function createProxyPlugin({ hubId, environment }) {
|
|
|
18
19
|
await server.register(h2o2)
|
|
19
20
|
|
|
20
21
|
for (const { id: moduleName, path, port } of modules) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (isCompose(environment)) {
|
|
27
|
-
baseUri = `http://${moduleName}:${port}`
|
|
28
|
-
}
|
|
29
|
-
if (isCdp(environment)) {
|
|
30
|
-
baseUri = `https://lis-apps-${moduleName}.${environment}.cdp-int.defra.cloud`
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (!isSupportedEnv(environment) || baseUri === '') {
|
|
34
|
-
throw new Error(`Unsupported environment: ${environment}`)
|
|
35
|
-
}
|
|
22
|
+
const baseUri = getServiceBaseUrl(
|
|
23
|
+
environment,
|
|
24
|
+
`lis-apps-${moduleName}`,
|
|
25
|
+
port
|
|
26
|
+
).origin
|
|
36
27
|
|
|
37
28
|
server.route({
|
|
38
29
|
method: '*',
|
|
@@ -66,29 +57,3 @@ export function createProxyPlugin({ hubId, environment }) {
|
|
|
66
57
|
}
|
|
67
58
|
}
|
|
68
59
|
}
|
|
69
|
-
|
|
70
|
-
function isSupportedEnv(env) {
|
|
71
|
-
const envs = [
|
|
72
|
-
'local',
|
|
73
|
-
'docker_compose',
|
|
74
|
-
'dev',
|
|
75
|
-
'test',
|
|
76
|
-
'ext-test',
|
|
77
|
-
'perf-test',
|
|
78
|
-
'prod'
|
|
79
|
-
]
|
|
80
|
-
return envs.includes(env)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function isLocal(env) {
|
|
84
|
-
return env === 'local'
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function isCompose(env) {
|
|
88
|
-
return env === 'docker_compose'
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function isCdp(env) {
|
|
92
|
-
const envs = ['dev', 'test', 'ext-test', 'perf-test', 'prod']
|
|
93
|
-
return envs.includes(env)
|
|
94
|
-
}
|
package/src/proxy-plugin.test.js
CHANGED
|
@@ -20,8 +20,9 @@ describe('createProxyPlugin()', () => {
|
|
|
20
20
|
test.each([
|
|
21
21
|
// Local proxying intentionally uses HTTP because the services run locally.
|
|
22
22
|
['local', 'http://localhost:3222'],
|
|
23
|
-
//
|
|
24
|
-
|
|
23
|
+
// docker_compose reaches every service via the host's published ports -
|
|
24
|
+
// there's no reliable compose service-name discovery to lean on instead.
|
|
25
|
+
['docker_compose', 'http://host.docker.internal:3222'],
|
|
25
26
|
['test', 'https://lis-apps-cattle-home.test.cdp-int.defra.cloud'],
|
|
26
27
|
['prod', 'https://lis-apps-cattle-home.prod.cdp-int.defra.cloud']
|
|
27
28
|
])('registers the %s proxy target', async (environment, expectedBaseUri) => {
|