Skip to content

Commit c6d100f

Browse files
committed
refactor(well-known): fastify & prom metrics
1 parent 0db653d commit c6d100f

File tree

12 files changed

+305
-187
lines changed

12 files changed

+305
-187
lines changed

.vscode/launch.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@
5252
"localRoot": "${workspaceFolder}/oada/",
5353
"remoteRoot": "/oada/"
5454
},
55+
{
56+
"name": "k8s: Attach to well-known",
57+
"presentation": {
58+
"group": "k8s"
59+
},
60+
"type": "cloudcode.kubernetes",
61+
"request": "attach",
62+
"language": "Node",
63+
"debugPort": "inspect",
64+
"podSelector": {
65+
"app.kubernetes.io/name": "well-known"
66+
},
67+
"smartStep": true,
68+
"localRoot": "${workspaceFolder}/oada/",
69+
"remoteRoot": "/oada/"
70+
},
5571
{
5672
"type": "node",
5773
"request": "attach",
@@ -69,7 +85,8 @@
6985
"configurations": [
7086
"k8s: Attach to http-handler",
7187
"k8s: Attach to write-handler",
72-
"k8s: Attach to auth"
88+
"k8s: Attach to auth",
89+
"k8s: Attach to well-known"
7390
],
7491
"presentation": {
7592
"group": "k8s",

charts/oada/templates/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ data:
3838
{{ if eq (include "oada.kafka.deploy" .) "true" -}}
3939
KAFKA_BROKERS: {{ include "oada.kafka.brokers" . }}
4040
{{- end }}
41+
WELLKNOWN_SUBSERVICES: http://auth-{{ .Release.Name }}
4142
{{ if .Values.global.development -}}
4243
NODE_ENV: development
4344
NODE_TLS_REJECT_UNAUTHORIZED: '0'

oada/.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ packageExtensions:
2121
"@oada/well-known-json@*":
2222
peerDependencies:
2323
"@types/express": "*"
24+
fastify: "*"
2425
express@*:
2526
peerDependencies:
2627
ejs: "*"

oada/libs/lib-prom/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017-2021 Open Ag Data Alliance
3+
* Copyright 2022 Open Ag Data Alliance
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

oada/services/http-handler/src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
import libConfig from '@oada/lib-config';
1919

2020
export const { config, schema } = await libConfig({
21+
'trustProxy': {
22+
format: Array,
23+
default: ['uniquelocal'],
24+
env: 'TRUST_PROXY',
25+
arg: 'trust-proxy',
26+
},
2127
'server': {
2228
port: {
2329
format: 'port',

oada/services/http-handler/src/server.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
import { mixins, pino } from '@oada/pino-debug';
1919

20+
import { config } from './config.js';
21+
2022
import { KafkaError } from '@oada/lib-kafka';
2123
import { nstats } from '@oada/lib-prom';
2224

2325
import { plugin as formats } from '@oada/formats-server';
2426

2527
import tokenLookup, { TokenResponse } from './tokenLookup.js';
2628
import authorizations from './authorizations.js';
27-
import { config } from './config.js';
2829
import resources from './resources.js';
2930
import users from './users.js';
3031
import websockets from './websockets.js';
@@ -86,8 +87,10 @@ mixins.push(() => ({
8687
reqId: requestContext.get('id'),
8788
}));
8889

90+
const trustProxy = config.get('trustProxy');
8991
// eslint-disable-next-line new-cap
9092
export const fastify = Fastify({
93+
trustProxy,
9194
logger: {
9295
...logger,
9396
// HACK: fastify overrides existing serializers. This circumvents that...
@@ -137,12 +140,13 @@ if (process.env.NODE_ENV !== 'production') {
137140
});
138141
}
139142

143+
const port = config.get('server.port');
140144
export async function start(): Promise<void> {
141145
await fastify.listen({
142-
port: config.get('server.port'),
146+
port,
143147
host: '::',
144148
});
145-
fastify.log.info('OADA Server started on port %d', config.get('server.port'));
149+
fastify.log.info('OADA Server started on port %d', port);
146150
}
147151

148152
declare module '@fastify/request-context' {

oada/services/http-handler/src/types.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,19 @@ declare module 'es-main' {
2424
declare module 'assert' {
2525
function internal(value: unknown, message?: string | Error): asserts value;
2626
}
27+
28+
declare module 'nstats' {
29+
import type { Server as HTTPServer } from 'node:http';
30+
31+
import type { Plugin } from 'fastify-plugin';
32+
import type { Server as WSServer } from 'ws';
33+
34+
export interface NStats {
35+
fastify(): Plugin;
36+
toPrometheus(): string;
37+
}
38+
39+
function nstats(ws?: WSServer, http?: HTTPServer, semver?: string): NStats;
40+
41+
export = nstats;
42+
}

oada/services/well-known/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@
2828
"test": "echo \"Error: no test specified\" && exit 1"
2929
},
3030
"dependencies": {
31-
"@oada/error": "^2.0.1",
31+
"@fastify/accepts": "^4.0.0",
32+
"@fastify/cors": "^8.0.0",
33+
"@fastify/helmet": "^9.1.0",
3234
"@oada/formats-server": "^3.1.1",
3335
"@oada/lib-config": "^3.5.1",
3436
"@oada/lib-prom": "workspace:^",
3537
"@oada/pino-debug": "^3.5.1",
36-
"@oada/well-known-json": "^2.0.1",
37-
"axios": "^0.27.2",
38-
"cors": "^2.8.5",
38+
"@oada/well-known-json": "^4.0.0",
3939
"debug": "^4.3.4",
40-
"express": "^4.18.1",
41-
"helmet": "^5.1.1",
40+
"fastify": "^4.3.0",
41+
"got": "^12.3.0",
4242
"tslib": "^2.4.0"
4343
},
4444
"devDependencies": {
4545
"@types/cors": "^2.8.12",
4646
"@types/debug": "^4.1.7",
47-
"@types/express": "^4.17.13"
47+
"fastify-plugin": "^4.0.0"
4848
},
4949
"volta": {
5050
"node": "18.5.0"

oada/services/well-known/src/config.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
import libConfig from '@oada/lib-config';
1919

2020
export const { config, schema } = await libConfig({
21+
trustProxy: {
22+
format: Array,
23+
default: ['uniquelocal'],
24+
env: 'TRUST_PROXY',
25+
arg: 'trust-proxy',
26+
},
2127
wellKnown: {
22-
'forceProtocol': {
23-
doc: 'use this to force https prefixes on URLs. Useful when behind a proxy.',
24-
format: ['https', 'http'],
25-
nullable: true,
26-
default: null,
27-
},
2828
'server': {
2929
port: {
3030
format: 'port',
@@ -66,11 +66,15 @@ export const { config, schema } = await libConfig({
6666
},
6767
'mergeSubServices': {
6868
format: Array,
69-
default: [] as Array<{
70-
resource: string;
71-
base: string;
72-
addPrefix?: string;
73-
}>,
69+
default: [] as Array<
70+
| {
71+
base: string;
72+
addPrefix?: string;
73+
}
74+
| string
75+
>,
76+
env: 'WELLKNOWN_SUBSERVICES',
77+
arg: 'wellknown-subservices',
7478
},
7579
'oada-configuration': {
7680
format: Object,
@@ -100,7 +104,7 @@ if (!config.get('wellKnown.oada-configuration.oada_base_uri')) {
100104
config.set(
101105
'wellKnown.wellKnown.server.oada-configuration.oada_base_uri',
102106
`${server.mode}//${server.domain}${server.port ? `:${server.port}` : ''}${
103-
server.path_prefix ? server.path_prefix : ''
107+
server.path_prefix ?? ''
104108
}`
105109
);
106110
}

0 commit comments

Comments
 (0)