-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor content-watcher service configs (#554)
This PR refactors `ConfigModule` and `ConfigService` for `account-service`. - Eliminate custom config service class and rely simply on built-in NestJS `ConfigModule`, using namespaced configs for individual modules - Move config property configuration into module-specific config, ie 'blockchain', 'cache', 'queue', 'account-api', 'account-worker'. (NOTE, as each module is refactored in the future, these module configs will be merged with the corresponding module configs from the other services, but for now they will still be duplicated across the services) - Update & add unit tests to cover new config setup Closes #547
- Loading branch information
1 parent
348f57b
commit 8682999
Showing
31 changed files
with
581 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { describe, it, expect, beforeAll } from '@jest/globals'; | ||
import apiConfig, { IContentWatcherApiConfig } from './api.config'; | ||
import configSetup from '#testlib/utils.config-tests'; | ||
|
||
const { setupConfigService, shouldFailBadValues } = configSetup<IContentWatcherApiConfig>(apiConfig); | ||
|
||
describe('Content Watcher API config', () => { | ||
const ALL_ENV: { [key: string]: string | undefined } = { | ||
API_PORT: undefined, | ||
}; | ||
|
||
beforeAll(() => { | ||
Object.keys(ALL_ENV).forEach((key) => { | ||
ALL_ENV[key] = process.env[key]; | ||
}); | ||
}); | ||
|
||
describe('invalid environment', () => { | ||
it('invalid API port should fail', async () => shouldFailBadValues(ALL_ENV, 'API_PORT', [-1, 'bad port'])); | ||
}); | ||
|
||
describe('valid environment', () => { | ||
let apiConf: IContentWatcherApiConfig; | ||
beforeAll(async () => { | ||
apiConf = await setupConfigService(ALL_ENV); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(apiConf).toBeDefined(); | ||
}); | ||
|
||
it('should get API port', async () => { | ||
expect(apiConf.apiPort).toStrictEqual(parseInt(ALL_ENV.API_PORT, 10)); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { JoiUtils } from '#config'; | ||
import { registerAs } from '@nestjs/config'; | ||
import Joi from 'joi'; | ||
|
||
export interface IContentWatcherApiConfig { | ||
apiPort: number; | ||
} | ||
|
||
export default registerAs('api', (): IContentWatcherApiConfig => { | ||
const configs: JoiUtils.JoiConfig<IContentWatcherApiConfig> = { | ||
apiPort: { | ||
value: process.env.API_PORT, | ||
joi: Joi.number().min(0).default(3000), | ||
}, | ||
}; | ||
|
||
return JoiUtils.validate<IContentWatcherApiConfig>(configs); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
libs/content-watcher-lib/src/blockchain/blockchain.config.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { describe, it, expect, beforeAll } from '@jest/globals'; | ||
import blockchainConfig, { IBlockchainConfig } from './blockchain.config'; | ||
import configSetup from '#testlib/utils.config-tests'; | ||
|
||
const { setupConfigService, validateMissing, shouldFailBadValues } = configSetup<IBlockchainConfig>(blockchainConfig); | ||
|
||
describe('Blockchain module config', () => { | ||
const ALL_ENV: { [key: string]: string | undefined } = { | ||
FREQUENCY_URL: undefined, | ||
}; | ||
|
||
beforeAll(() => { | ||
Object.keys(ALL_ENV).forEach((key) => { | ||
ALL_ENV[key] = process.env[key]; | ||
}); | ||
}); | ||
|
||
describe('invalid environment', () => { | ||
it('missing frequency url should fail', async () => validateMissing(ALL_ENV, 'FREQUENCY_URL')); | ||
it('invalid frequency url should fail', async () => shouldFailBadValues(ALL_ENV, 'FREQUENCY_URL', ['invalid url'])); | ||
}); | ||
|
||
describe('valid environment', () => { | ||
let blockchainConf: IBlockchainConfig; | ||
beforeAll(async () => { | ||
blockchainConf = await setupConfigService(ALL_ENV); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(blockchainConf).toBeDefined(); | ||
}); | ||
|
||
it('should get frequency url', () => { | ||
const expectedUrl = new URL(ALL_ENV.FREQUENCY_URL).toString(); | ||
expect(blockchainConf.frequencyUrl?.toString()).toStrictEqual(expectedUrl); | ||
}); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
libs/content-watcher-lib/src/blockchain/blockchain.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { registerAs } from '@nestjs/config'; | ||
import Joi from 'joi'; | ||
import * as JoiUtil from '#config/joi-utils'; | ||
|
||
export interface IBlockchainConfig { | ||
frequencyUrl: URL; | ||
} | ||
|
||
export default registerAs('blockchain', (): IBlockchainConfig => { | ||
const configs: JoiUtil.JoiConfig<IBlockchainConfig> = { | ||
frequencyUrl: { | ||
value: process.env.FREQUENCY_URL, | ||
joi: Joi.string() | ||
.uri({ scheme: ['http', 'https', 'ws', 'wss'] }) | ||
.required() | ||
.custom((v) => new URL(v)), | ||
}, | ||
}; | ||
|
||
return JoiUtil.validate<IBlockchainConfig>(configs); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { describe, it, expect, beforeAll } from '@jest/globals'; | ||
import cacheConfig, { ICacheConfig } from './cache.config'; | ||
import configSetup from '#testlib/utils.config-tests'; | ||
|
||
const { setupConfigService, validateMissing, shouldFailBadValues } = configSetup<ICacheConfig>(cacheConfig); | ||
|
||
describe('Cache module config', () => { | ||
const ALL_ENV: { [key: string]: string | undefined } = { | ||
REDIS_URL: undefined, | ||
CACHE_KEY_PREFIX: undefined, | ||
}; | ||
|
||
beforeAll(() => { | ||
Object.keys(ALL_ENV).forEach((key) => { | ||
ALL_ENV[key] = process.env[key]; | ||
}); | ||
}); | ||
|
||
describe('invalid environment', () => { | ||
it('missing redis url should fail', async () => validateMissing(ALL_ENV, 'REDIS_URL')); | ||
|
||
it('invalid redis url should fail', async () => shouldFailBadValues(ALL_ENV, 'REDIS_URL', ['invalid url'])); | ||
|
||
it('missing cache key prefix should fail', async () => validateMissing(ALL_ENV, 'CACHE_KEY_PREFIX')); | ||
}); | ||
|
||
describe('valid environment', () => { | ||
let cacheConf: ICacheConfig; | ||
beforeAll(async () => { | ||
cacheConf = await setupConfigService(ALL_ENV); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(cacheConf).toBeDefined(); | ||
}); | ||
|
||
it('should get redis url', () => { | ||
expect(cacheConf.redisUrl).toStrictEqual(new URL(ALL_ENV.REDIS_URL).toString()); | ||
}); | ||
|
||
it('should get cache key prefix', () => { | ||
expect(cacheConf.cacheKeyPrefix).toStrictEqual(ALL_ENV.CACHE_KEY_PREFIX?.toString()); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { JoiUtils } from '#config'; | ||
import { registerAs } from '@nestjs/config'; | ||
import Joi from 'joi'; | ||
|
||
export interface ICacheConfig { | ||
redisUrl: string; | ||
cacheKeyPrefix: string; | ||
} | ||
|
||
export default registerAs('cache', (): ICacheConfig => { | ||
const configs: JoiUtils.JoiConfig<ICacheConfig> = { | ||
redisUrl: { | ||
value: process.env.REDIS_URL, | ||
joi: Joi.string().uri().required(), | ||
}, | ||
cacheKeyPrefix: { | ||
value: process.env.CACHE_KEY_PREFIX, | ||
joi: Joi.string().required(), | ||
}, | ||
}; | ||
|
||
return JoiUtils.validate<ICacheConfig>(configs); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.