Skip to content

Commit

Permalink
feat(config): add logging support to lib-config
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed Feb 9, 2024
1 parent cdea9f3 commit a923942
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 38 deletions.
2 changes: 2 additions & 0 deletions oada/libs/lib-arangodb/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const { config, schema } = await libConfig({
password: {
doc: 'Password for arangodb authentication',
format: String,
sensitive: true,
default: '',
env: 'ARANGODB_IMPORT_PASSWORD',
arg: 'arangodb-import-password',
Expand Down Expand Up @@ -118,6 +119,7 @@ export const { config, schema } = await libConfig({
password: {
doc: 'Password for arangodb authentication',
format: String,
sensitive: true,
default: '',
env: 'ARANGODB_PASSWORD',
},
Expand Down
4 changes: 3 additions & 1 deletion oada/libs/lib-config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@oada/lib-config",
"version": "3.10.0",
"description": "Confing wrapper",
"description": "Config wrapper",
"main": "dist/index.js",
"type": "module",
"engines": {
Expand Down Expand Up @@ -33,9 +33,11 @@
},
"homepage": "https://github.com/oada/@oada/lib-config#readme",
"dependencies": {
"@oada/pino-debug": "workspace:^",
"convict": "^6.2.4",
"convict-format-with-moment": "^6.2.0",
"convict-format-with-validator": "^6.2.0",
"debug": "^4.3.4",
"dotenv": "^16.4.1",
"json5": "^2.2.3",
"tslib": "2.6.2",
Expand Down
79 changes: 48 additions & 31 deletions oada/libs/lib-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ import { pathToFileURL } from 'node:url';
import { readFile } from 'node:fs/promises';

import 'dotenv/config';
import type { Config, Schema } from 'convict';
import convict from 'convict';
import convict, { type Config, type Schema } from 'convict';
import json5 from 'json5';
// @ts-expect-error no types for this
import moment from 'convict-format-with-moment';
import validator from 'convict-format-with-validator';
import yaml from 'yaml';

import '@oada/pino-debug';
import log from 'debug';

const debug = log('@oada/lib-config:debug');

// Builtin part of the config schema
const defaults = {
configfiles: {
Expand Down Expand Up @@ -147,38 +151,51 @@ convict.addParser([
* @param inSchema Config schema for your application
* @see Schema
*/
export default async function config<S>(
export default async function loadConfig<S>(
// Defer type inference
inSchema: S,
) {
type C = S extends Schema<infer T> ? Config<T> : never;
// Merge input schema with default schema and create config
const schema = convict({ ...defaults, ...inSchema });

// Optionally load any config file(s)
const files = schema.get('configfiles');
for await (const file of files) {
// Allow requiring a js config?
// FIXME: Probably remove this
if (['.js', '.mjs', '.cjs'].includes(extname(file))) {
const configFile = (await import(file)) as { default?: unknown };
schema.load(configFile.default ?? configFile); // Nosemgrep: javascript.lang.security.detect-non-literal-require.detect-non-literal-require
} else {
schema.loadFile(file);
try {
type C = S extends Schema<infer T> ? Config<T> : never;
// Merge input schema with default schema and create config
const config = convict({ ...defaults, ...inSchema });

// Optionally load any config file(s)
const files = config.get('configfiles');
for await (const file of files) {
// Allow requiring a js config?
// FIXME: Probably remove this
if (['.js', '.mjs', '.cjs'].includes(extname(file))) {
const configFile = (await import(file)) as { default?: unknown };
config.load(configFile.default ?? configFile); // Nosemgrep: javascript.lang.security.detect-non-literal-require.detect-non-literal-require
} else {
config.loadFile(file);
}
}

// Ensure config is valid
config.validate({
// Allow extra items
allowed: 'warn',
// Do not actually output warnings about extra items?
// eslint-disable-next-line @typescript-eslint/no-empty-function
output() {},
});

if (debug.enabled) {
debug(
{
config: JSON.parse(`${config}`) as unknown,
},
'Config loaded successfully',
);
}
}

// Ensure config is valid
schema.validate({
// Allow extra items
allowed: 'warn',
// Do not actually output warnings about extra items?
// eslint-disable-next-line @typescript-eslint/no-empty-function
output() {},
});

return {
config: schema as C & typeof schema,
schema: inSchema,
};
return {
config: config as C & typeof config,
schema: inSchema,
};
} catch (error: unknown) {
throw new Error('Failed to load config', { cause: error });
}
}
3 changes: 2 additions & 1 deletion oada/libs/lib-config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
"include": ["src"],
"references": [{ "path": "../pino-debug" }]
}
2 changes: 0 additions & 2 deletions oada/oada.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ export default {
serviceName: 'Trellis',
serviceLongName: 'Trellis - A Framework for Produce Audit Data',
server: {
// Replace these in production with things that are actually secret...
'sessionSecret': '2jp901p3#2#(!)kd9',
'port-http': 8080,
'mode': 'http',
'proxy': 'uniquelocal',
Expand Down
4 changes: 2 additions & 2 deletions oada/services/auth/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import libConfig from '@oada/lib-config';
import { schema as arangoSchema } from '@oada/lib-arangodb/dist/config.js';

const trace = debug('auth#config:trace');
const error = debug('auth#config:error');
const warn = debug('auth#config:warn');

export const { config, schema } = await libConfig({
trustProxy: {
Expand Down Expand Up @@ -402,7 +402,7 @@ for await (const dirname of await fs.readdir(domainsDirectory)) {
domainConfigs.set(dConfig.domain, dConfig);
break;
} catch (cError: unknown) {
error(cError, `Could not read config for domain ${dirname}, skipping`);
warn(cError, `Could not read config for domain ${dirname}, skipping`);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion oada/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1034,12 +1034,14 @@ __metadata:
version: 0.0.0-use.local
resolution: "@oada/lib-config@workspace:libs/lib-config"
dependencies:
"@oada/pino-debug": "workspace:^"
"@types/convict": "npm:^6.1.6"
"@types/convict-format-with-validator": "npm:^6.0.5"
"@types/debug": "npm:^4.1.12"
convict: "npm:^6.2.4"
convict-format-with-moment: "npm:^6.2.0"
convict-format-with-validator: "npm:^6.2.0"
debug: "npm:^4.3.4"
dotenv: "npm:^16.4.1"
json5: "npm:^2.2.3"
tslib: "npm:2.6.2"
Expand Down Expand Up @@ -1161,7 +1163,7 @@ __metadata:
languageName: unknown
linkType: soft

"@oada/pino-debug@npm:^3.7.0, @oada/pino-debug@workspace:libs/pino-debug":
"@oada/pino-debug@npm:^3.7.0, @oada/pino-debug@workspace:^, @oada/pino-debug@workspace:libs/pino-debug":
version: 0.0.0-use.local
resolution: "@oada/pino-debug@workspace:libs/pino-debug"
dependencies:
Expand Down

0 comments on commit a923942

Please sign in to comment.