Skip to content

Commit 43803b4

Browse files
authored
feat(lambda-tiler): expose configuration id and hash if present (#3446)
### Motivation I would like to know what configuration is in use on each instance of basemaps, we have a version endpoint `/v1/version` that gives the build information (commit hash, github actions run id) but does not say what configuration is being used. ### Modifications If the current configuration has a `id` and `hash` return those as `{ config: { id, hash} }` ```json { "id": "cb_01JTQ7ZK49F8EY4N5DRJ3XFT73", "hash": "HcByZ8WS2zpaTxFJp6wSKg2eUpwahLqAGEQdcDxKxqp6" } ``` ### Verification Unit tests + running locally
1 parent f349080 commit 43803b4

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

packages/config/src/memory/memory.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ function removeUndefined(obj: unknown): void {
6969
export class ConfigProviderMemory extends BasemapsConfigProvider {
7070
override type = 'memory' as const;
7171

72+
static is(cfg: BasemapsConfigProvider): cfg is ConfigProviderMemory {
73+
return cfg.type === 'memory';
74+
}
75+
7276
/** Optional id of the configuration */
7377
id?: string;
78+
/** Optional hash of the config if the config was loaded from JSON */
79+
hash?: string;
7480

7581
Imagery = new MemoryConfigObject<ConfigImagery>(this, ConfigPrefix.Imagery);
7682
Style = new MemoryConfigObject<ConfigVectorStyle>(this, ConfigPrefix.Style);
@@ -245,6 +251,7 @@ export class ConfigProviderMemory extends BasemapsConfigProvider {
245251

246252
mem.assets = cfg.assets;
247253
mem.id = cfg.id;
254+
mem.hash = cfg.hash;
248255

249256
return mem;
250257
}

packages/lambda-tiler/src/__tests__/index.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import assert from 'node:assert';
22
import { afterEach, describe, it } from 'node:test';
33

4+
import { ConfigProviderMemory } from '@basemaps/config';
5+
46
import { handler } from '../index.js';
7+
import { ConfigLoader } from '../util/config.loader.js';
58
import { mockRequest } from './xyz.util.js';
69

710
describe('LambdaXyz index', () => {
@@ -17,7 +20,10 @@ describe('LambdaXyz index', () => {
1720
delete process.env['BUILD_ID'];
1821
});
1922

20-
it('should return version', async () => {
23+
it('should return version', async (t) => {
24+
const config = new ConfigProviderMemory();
25+
t.mock.method(ConfigLoader, 'getDefaultConfig', () => config);
26+
2127
process.env['GIT_VERSION'] = '1.2.3';
2228
process.env['GIT_HASH'] = 'abc456';
2329

@@ -32,7 +38,10 @@ describe('LambdaXyz index', () => {
3238
});
3339
});
3440

35-
it('should include buildId if exists', async () => {
41+
it('should include buildId if exists', async (t) => {
42+
const config = new ConfigProviderMemory();
43+
t.mock.method(ConfigLoader, 'getDefaultConfig', () => config);
44+
3645
process.env['GIT_VERSION'] = '1.2.3';
3746
process.env['BUILD_ID'] = '1658821493-3';
3847

@@ -46,6 +55,25 @@ describe('LambdaXyz index', () => {
4655
buildId: '1658821493-3',
4756
});
4857
});
58+
59+
it('should return config information if present', async (t) => {
60+
const config = new ConfigProviderMemory();
61+
t.mock.method(ConfigLoader, 'getDefaultConfig', () => config);
62+
config.id = 'config-id';
63+
64+
const response = await handler.router.handle(mockRequest('/v1/version'));
65+
assert.deepEqual(JSON.parse(response.body), {
66+
version: 'dev',
67+
config: { id: 'config-id' },
68+
});
69+
70+
config.hash = 'config-hash';
71+
const responseHash = await handler.router.handle(mockRequest('/v1/version'));
72+
assert.deepEqual(JSON.parse(responseHash.body), {
73+
version: 'dev',
74+
config: { id: 'config-id', hash: 'config-hash' },
75+
});
76+
});
4977
});
5078

5179
it('should respond to /ping', async () => {
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
import { HttpHeader, LambdaHttpResponse } from '@linzjs/lambda';
1+
import { BasemapsConfigProvider, ConfigProviderMemory } from '@basemaps/config';
2+
import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
23

3-
export function versionGet(): Promise<LambdaHttpResponse> {
4+
import { ConfigLoader } from '../util/config.loader.js';
5+
6+
function getConfigHash(cfg: BasemapsConfigProvider): { id: string; hash?: string } | undefined {
7+
if (!ConfigProviderMemory.is(cfg)) return undefined;
8+
if (cfg.id == null) return undefined;
9+
return { id: cfg.id, hash: cfg.hash };
10+
}
11+
12+
export async function versionGet(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
13+
const config = await ConfigLoader.load(req);
414
const response = new LambdaHttpResponse(200, 'ok');
515
response.header(HttpHeader.CacheControl, 'no-store');
616
response.json({
@@ -9,16 +19,26 @@ export function versionGet(): Promise<LambdaHttpResponse> {
919
* @example "v6.42.1"
1020
*/
1121
version: process.env['GIT_VERSION'] ?? 'dev',
22+
1223
/**
1324
* Full git commit hash
1425
* @example "e4231b1ee62c276c8657c56677ced02681dfe5d6"
1526
*/
1627
hash: process.env['GIT_HASH'],
28+
1729
/**
30+
*
1831
* The exact build that this release was run from
1932
* @example "1658821493-3"
2033
*/
2134
buildId: process.env['BUILD_ID'],
35+
36+
/**
37+
* Configuration id that was used to power this config
38+
* @example { "id": "cb_01JTQ7ZK49F8EY4N5DRJ3XFT73", hash: "HcByZ8WS2zpaTxFJp6wSKg2eUpwahLqAGEQdcDxKxqp6" }
39+
*/
40+
config: getConfigHash(config),
2241
});
42+
2343
return Promise.resolve(response);
2444
}

0 commit comments

Comments
 (0)