-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamically load api config and refactor the logger (#51)
* WIP - add cloudformation.yaml (health checks failing) * WIP - load dynamic config * Extract config get/set functions * Extract logger options to env variables * Make logger usage consistent in data-pusher * Moving more logger options and fixing tests * Load env in data-pusher * Remove cloudformation.yaml for now * Fix TS configuration for building project * Improve validation for ENVs --------- Co-authored-by: Emanuel Tesař <e.tesarr@gmail.com>
- Loading branch information
Showing
40 changed files
with
931 additions
and
163 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
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,16 @@ | ||
LOGGER_ENABLED=true | ||
LOG_COLORIZE=true | ||
LOG_FORMAT=pretty | ||
LOG_LEVEL=info | ||
|
||
# Available options | ||
# local (default) - loads config/signed-api.json from the filesystem | ||
# aws-s3 - loads the config file from AWS S3 | ||
CONFIG_SOURCE=local | ||
|
||
# Set these variables if you would like to source your config from AWS S3 | ||
AWS_ACCESS_KEY_ID= | ||
AWS_SECRET_ACCESS_KEY= | ||
AWS_REGION= | ||
AWS_S3_BUCKET_NAME= | ||
AWS_S3_BUCKET_PATH= |
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
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,54 @@ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { go } from '@api3/promise-utils'; | ||
import { S3 } from '@aws-sdk/client-s3'; | ||
import { logger } from './logger'; | ||
import { Config, configSchema } from './schema'; | ||
import { loadEnv } from './env'; | ||
|
||
let config: Config | undefined; | ||
|
||
export const getConfig = (): Config => { | ||
if (!config) throw new Error(`Config has not been set yet`); | ||
|
||
return config; | ||
}; | ||
|
||
export const fetchAndCacheConfig = async (): Promise<Config> => { | ||
const jsonConfig = await fetchConfig(); | ||
config = configSchema.parse(jsonConfig); | ||
return config; | ||
}; | ||
|
||
const fetchConfig = async (): Promise<any> => { | ||
const env = loadEnv(); | ||
const source = env.CONFIG_SOURCE; | ||
if (!source || source === 'local') { | ||
return JSON.parse(readFileSync(join(__dirname, '../config/signed-api.json'), 'utf8')); | ||
} | ||
if (source === 'aws-s3') { | ||
return await fetchConfigFromS3(); | ||
} | ||
throw new Error(`Unable to load config CONFIG_SOURCE:${source}`); | ||
}; | ||
|
||
const fetchConfigFromS3 = async (): Promise<any> => { | ||
const env = loadEnv(); | ||
const region = env.AWS_REGION!; // Validated by environment variables schema. | ||
const s3 = new S3({ region }); | ||
|
||
const params = { | ||
Bucket: env.AWS_S3_BUCKET_NAME, | ||
Key: env.AWS_S3_BUCKET_PATH, | ||
}; | ||
|
||
logger.info(`Fetching config from AWS S3 region:${region}...`); | ||
const res = await go(() => s3.getObject(params), { retries: 1 }); | ||
if (!res.success) { | ||
logger.error('Error fetching config from AWS S3:', res.error); | ||
throw res.error; | ||
} | ||
logger.info('Config fetched successfully from AWS S3'); | ||
const stringifiedConfig = await res.data.Body!.transformToString(); | ||
return JSON.parse(stringifiedConfig); | ||
}; |
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,19 @@ | ||
import { join } from 'path'; | ||
import dotenv from 'dotenv'; | ||
import { EnvConfig, envConfigSchema } from './schema'; | ||
|
||
let env: EnvConfig | undefined; | ||
|
||
export const loadEnv = () => { | ||
if (env) return env; | ||
|
||
dotenv.config({ path: join(__dirname, '../.env') }); | ||
|
||
const parseResult = envConfigSchema.safeParse(process.env); | ||
if (!parseResult.success) { | ||
throw new Error(`Invalid environment variables:\n, ${JSON.stringify(parseResult.error.format())}`); | ||
} | ||
|
||
env = parseResult.data; | ||
return env; | ||
}; |
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
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 |
---|---|---|
@@ -1,5 +1,15 @@ | ||
import { createLogger } from 'signed-api/common'; | ||
import { getConfig } from './utils'; | ||
import { createLogger, logConfigSchema } from 'signed-api/common'; | ||
import { loadEnv } from './env'; | ||
|
||
const config = getConfig(); | ||
export const logger = createLogger(config.logger); | ||
// We need to load the environment variables before we can use the logger. Because we want the logger to always be | ||
// available, we load the environment variables as a side effect during the module import. | ||
const env = loadEnv(); | ||
|
||
const options = logConfigSchema.parse({ | ||
colorize: env.LOG_COLORIZE, | ||
enabled: env.LOGGER_ENABLED, | ||
minLevel: env.LOG_LEVEL, | ||
format: env.LOG_FORMAT, | ||
}); | ||
|
||
export const logger = createLogger(options); |
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
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
Oops, something went wrong.