Skip to content

Commit

Permalink
refactor(well-known): fastify & prom metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed Aug 2, 2022
1 parent 0db653d commit c6d100f
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 187 deletions.
19 changes: 18 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@
"localRoot": "${workspaceFolder}/oada/",
"remoteRoot": "/oada/"
},
{
"name": "k8s: Attach to well-known",
"presentation": {
"group": "k8s"
},
"type": "cloudcode.kubernetes",
"request": "attach",
"language": "Node",
"debugPort": "inspect",
"podSelector": {
"app.kubernetes.io/name": "well-known"
},
"smartStep": true,
"localRoot": "${workspaceFolder}/oada/",
"remoteRoot": "/oada/"
},
{
"type": "node",
"request": "attach",
Expand All @@ -69,7 +85,8 @@
"configurations": [
"k8s: Attach to http-handler",
"k8s: Attach to write-handler",
"k8s: Attach to auth"
"k8s: Attach to auth",
"k8s: Attach to well-known"
],
"presentation": {
"group": "k8s",
Expand Down
1 change: 1 addition & 0 deletions charts/oada/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ data:
{{ if eq (include "oada.kafka.deploy" .) "true" -}}
KAFKA_BROKERS: {{ include "oada.kafka.brokers" . }}
{{- end }}
WELLKNOWN_SUBSERVICES: http://auth-{{ .Release.Name }}
{{ if .Values.global.development -}}
NODE_ENV: development
NODE_TLS_REJECT_UNAUTHORIZED: '0'
Expand Down
1 change: 1 addition & 0 deletions oada/.yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ packageExtensions:
"@oada/well-known-json@*":
peerDependencies:
"@types/express": "*"
fastify: "*"
express@*:
peerDependencies:
ejs: "*"
Expand Down
2 changes: 1 addition & 1 deletion oada/libs/lib-prom/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017-2021 Open Ag Data Alliance
* Copyright 2022 Open Ag Data Alliance
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
6 changes: 6 additions & 0 deletions oada/services/http-handler/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
import libConfig from '@oada/lib-config';

export const { config, schema } = await libConfig({
'trustProxy': {
format: Array,
default: ['uniquelocal'],
env: 'TRUST_PROXY',
arg: 'trust-proxy',
},
'server': {
port: {
format: 'port',
Expand Down
10 changes: 7 additions & 3 deletions oada/services/http-handler/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

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

import { config } from './config.js';

import { KafkaError } from '@oada/lib-kafka';
import { nstats } from '@oada/lib-prom';

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

import tokenLookup, { TokenResponse } from './tokenLookup.js';
import authorizations from './authorizations.js';
import { config } from './config.js';
import resources from './resources.js';
import users from './users.js';
import websockets from './websockets.js';
Expand Down Expand Up @@ -86,8 +87,10 @@ mixins.push(() => ({
reqId: requestContext.get('id'),
}));

const trustProxy = config.get('trustProxy');
// eslint-disable-next-line new-cap
export const fastify = Fastify({
trustProxy,
logger: {
...logger,
// HACK: fastify overrides existing serializers. This circumvents that...
Expand Down Expand Up @@ -137,12 +140,13 @@ if (process.env.NODE_ENV !== 'production') {
});
}

const port = config.get('server.port');
export async function start(): Promise<void> {
await fastify.listen({
port: config.get('server.port'),
port,
host: '::',
});
fastify.log.info('OADA Server started on port %d', config.get('server.port'));
fastify.log.info('OADA Server started on port %d', port);
}

declare module '@fastify/request-context' {
Expand Down
16 changes: 16 additions & 0 deletions oada/services/http-handler/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ declare module 'es-main' {
declare module 'assert' {
function internal(value: unknown, message?: string | Error): asserts value;
}

declare module 'nstats' {
import type { Server as HTTPServer } from 'node:http';

import type { Plugin } from 'fastify-plugin';
import type { Server as WSServer } from 'ws';

export interface NStats {
fastify(): Plugin;
toPrometheus(): string;
}

function nstats(ws?: WSServer, http?: HTTPServer, semver?: string): NStats;

export = nstats;
}
14 changes: 7 additions & 7 deletions oada/services/well-known/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@oada/error": "^2.0.1",
"@fastify/accepts": "^4.0.0",
"@fastify/cors": "^8.0.0",
"@fastify/helmet": "^9.1.0",
"@oada/formats-server": "^3.1.1",
"@oada/lib-config": "^3.5.1",
"@oada/lib-prom": "workspace:^",
"@oada/pino-debug": "^3.5.1",
"@oada/well-known-json": "^2.0.1",
"axios": "^0.27.2",
"cors": "^2.8.5",
"@oada/well-known-json": "^4.0.0",
"debug": "^4.3.4",
"express": "^4.18.1",
"helmet": "^5.1.1",
"fastify": "^4.3.0",
"got": "^12.3.0",
"tslib": "^2.4.0"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/debug": "^4.1.7",
"@types/express": "^4.17.13"
"fastify-plugin": "^4.0.0"
},
"volta": {
"node": "18.5.0"
Expand Down
28 changes: 16 additions & 12 deletions oada/services/well-known/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import libConfig from '@oada/lib-config';

export const { config, schema } = await libConfig({
trustProxy: {
format: Array,
default: ['uniquelocal'],
env: 'TRUST_PROXY',
arg: 'trust-proxy',
},
wellKnown: {
'forceProtocol': {
doc: 'use this to force https prefixes on URLs. Useful when behind a proxy.',
format: ['https', 'http'],
nullable: true,
default: null,
},
'server': {
port: {
format: 'port',
Expand Down Expand Up @@ -66,11 +66,15 @@ export const { config, schema } = await libConfig({
},
'mergeSubServices': {
format: Array,
default: [] as Array<{
resource: string;
base: string;
addPrefix?: string;
}>,
default: [] as Array<
| {
base: string;
addPrefix?: string;
}
| string
>,
env: 'WELLKNOWN_SUBSERVICES',
arg: 'wellknown-subservices',
},
'oada-configuration': {
format: Object,
Expand Down Expand Up @@ -100,7 +104,7 @@ if (!config.get('wellKnown.oada-configuration.oada_base_uri')) {
config.set(
'wellKnown.wellKnown.server.oada-configuration.oada_base_uri',
`${server.mode}//${server.domain}${server.port ? `:${server.port}` : ''}${
server.path_prefix ? server.path_prefix : ''
server.path_prefix ?? ''
}`
);
}
Loading

0 comments on commit c6d100f

Please sign in to comment.