-
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.
- Loading branch information
Showing
18 changed files
with
250 additions
and
117 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,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,15 +1,15 @@ | ||
import { createLogger, logLevelSchema, LogConfig } from 'signed-api/common'; | ||
import { createLogger, logConfigSchema } from 'signed-api/common'; | ||
import { loadEnv } from './env'; | ||
|
||
const logLevel = () => { | ||
const res = logLevelSchema.safeParse(process.env.LOG_LEVEL || 'info'); | ||
return res.success ? res.data : 'info'; | ||
}; | ||
// 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: LogConfig = { | ||
colorize: process.env.LOG_COLORIZE !== 'false', | ||
enabled: process.env.LOGGER_ENABLED !== 'false', | ||
minLevel: logLevel(), | ||
format: process.env.LOG_FORMAT === 'json' ? 'json' : 'pretty', | ||
}; | ||
const options = logConfigSchema.parse({ | ||
colorize: env.LOG_COLORIZE, | ||
enabled: env.LOGGER_ENABLED, | ||
minLevel: env.LOG_LEVEL, | ||
format: env.LOG_FORMAT === 'json' ? 'json' : 'pretty', | ||
}); | ||
|
||
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
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.