From 8752f52a8f114e6bce4bc52d864dcdf3f8d178c2 Mon Sep 17 00:00:00 2001 From: martintrajanovski Date: Mon, 18 Nov 2024 15:01:52 +0100 Subject: [PATCH] load the proposalTypes.json using the existing functionality and add PROPOSAL_TYPES_FILE env variable --- .env.example | 1 + README.md | 1 + src/config/configuration.ts | 28 ++++++++-------------------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/.env.example b/.env.example index 32b3cdc9a..869d056e4 100644 --- a/.env.example +++ b/.env.example @@ -79,3 +79,4 @@ ES_PASSWORD="duo-password" ES_REFRESH=<"wait_for"|"false"> LOGGERS_CONFIG_FILE="loggers.json" +PROPOSAL_TYPES_FILE="proposalTypes.json" diff --git a/README.md b/README.md index 6bd2163c9..dd85794f1 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,7 @@ Valid environment variables for the .env file. See [.env.example](/.env.example) | `ES_FIELDS_LIMIT` | number | | The total number of fields in an index. | 1000 | | `ES_REFRESH` | string | | If set to `wait_for`, Elasticsearch will wait till data is inserted into the specified index before returning a response. | false | | `LOGGERS_CONFIG_FILE` | string | | The file name for loggers configuration, located in the project root directory. | "loggers.json" | +| `PROPOSAL_TYPES_FILE` | string | | The file name for proposal types configuration, located in the project root directory. | "proposalTypes.json" | | `SWAGGER_PATH` | string | Yes | swaggerPath is the path where the swagger UI will be available| "explorer"| | `MAX_FILE_UPLOAD_SIZE` | string | Yes | Maximum allowed file upload size | "16mb"| diff --git a/src/config/configuration.ts b/src/config/configuration.ts index e9eec5399..971a2205a 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -39,9 +39,12 @@ const configuration = () => { modulePath: "./loggingProviders/defaultLogger", config: {}, }; - const jsonConfigMap: { [key: string]: object[] | boolean } = {}; + const jsonConfigMap: { [key: string]: object | object[] | boolean } = { + proposalTypes: {}, + }; const jsonConfigFileList: { [key: string]: string } = { loggers: process.env.LOGGERS_CONFIG_FILE || "loggers.json", + proposalTypes: process.env.PROPOSAL_TYPES_FILE || "proposalTypes.json", }; Object.keys(jsonConfigFileList).forEach((key) => { const filePath = jsonConfigFileList[key]; @@ -58,25 +61,10 @@ const configuration = () => { } }); - let proposalTypesData; - - try { - proposalTypesData = fs.readFileSync("proposalTypes.json", "utf8"); - } catch (error) { - // NOTE: If there is no proposalTypes.json file provided we just use the DEFAULT_PROPOSAL_TYPE type - console.warn(error); - } - - if (!proposalTypesData) { - console.warn( - `Proposal types configuration file not provided. The "${DEFAULT_PROPOSAL_TYPE}" type will be used`, - ); - } - - const proposalTypes: Record = { - ...JSON.parse(proposalTypesData || "{}"), + // NOTE: Add the default proposal type here + Object.assign(jsonConfigMap.proposalTypes, { DefaultProposal: DEFAULT_PROPOSAL_TYPE, - }; + }); const config = { maxFileUploadSizeInMb: process.env.MAX_FILE_UPLOAD_SIZE || "16mb", // 16MB by default @@ -224,7 +212,7 @@ const configuration = () => { policyPublicationShiftInYears: process.env.POLICY_PUBLICATION_SHIFT ?? 3, policyRetentionShiftInYears: process.env.POLICY_RETENTION_SHIFT ?? -1, }, - proposalTypes: proposalTypes, + proposalTypes: jsonConfigMap.proposalTypes, }; return merge(config, localconfiguration); };