diff --git a/package.json b/package.json index 678dfca..cceb28a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "description": "", "main": "proxy.js", "scripts": { - "test": "jest", + "test": "jest --detectOpenHandles", "start": "node --no-warnings src/server.js", "prepare-binary": "rm -rf dist && mkdir dist", "create-win-x64-dist": "pkg package.json -t node16-win-x64 --out-path dist", diff --git a/src/api/registry.js b/src/api/registry.js index f77557a..bb66b3d 100644 --- a/src/api/registry.js +++ b/src/api/registry.js @@ -6,9 +6,9 @@ const wallet = require("../chia/wallet"); const utils = require("../utils"); const registryUri = utils.generateUriForHostAndPort( - CONFIG().REGISTRY.PROTOCOL, - CONFIG().REGISTRY.HOST, - CONFIG().REGISTRY.PORT + CONFIG().CADT.PROTOCOL, + CONFIG().CADT.HOST, + CONFIG().CADT.PORT ); /** @@ -18,8 +18,8 @@ const registryUri = utils.generateUriForHostAndPort( * @returns {Object} Headers with API Key added if available */ const maybeAppendRegistryApiKey = (headers = {}) => { - if (CONFIG().REGISTRY.API_KEY) { - headers["x-api-key"] = CONFIG().REGISTRY.API_KEY; + if (CONFIG().CADT.API_KEY) { + headers["x-api-key"] = CONFIG().CADT.API_KEY; } return headers; }; diff --git a/src/api/token-driver.js b/src/api/token-driver.js index 9513e4c..df70417 100644 --- a/src/api/token-driver.js +++ b/src/api/token-driver.js @@ -9,9 +9,9 @@ const { } = require("../utils"); const tokenDriverUri = generateUriForHostAndPort( - CONFIG().TOKEN_DRIVER.PROTOCOL, - CONFIG().TOKEN_DRIVER.HOST, - CONFIG().TOKEN_DRIVER.PORT + CONFIG().CHIA_CLIMATE_TOKENIZATION.PROTOCOL, + CONFIG().CHIA_CLIMATE_TOKENIZATION.HOST, + CONFIG().CHIA_CLIMATE_TOKENIZATION.PORT ); /** @@ -20,8 +20,8 @@ const tokenDriverUri = generateUriForHostAndPort( * @returns {Object} Headers with API Key added if available */ const maybeAppendTokenDriverApiKey = (headers = {}) => { - if (CONFIG().TOKEN_DRIVER.API_KEY) { - headers["x-api-key"] = CONFIG().TOKEN_DRIVER.API_KEY; + if (CONFIG().CHIA_CLIMATE_TOKENIZATION.API_KEY) { + headers["x-api-key"] = CONFIG().CHIA_CLIMATE_TOKENIZATION.API_KEY; } return headers; }; @@ -129,7 +129,7 @@ const createToken = async (tokenizationBody) => { ); } - logger.debug(`Token creation response: ${JSON.stringify(response.body)}`); + logger.trace(`Token creation response: ${JSON.stringify(response.body)}`); return response?.body; } catch (error) { diff --git a/src/proxy.js b/src/proxy.js index dbc8aef..ba2f9ab 100644 --- a/src/proxy.js +++ b/src/proxy.js @@ -4,9 +4,9 @@ const { updateQueryWithParam, generateUriForHostAndPort } = require("./utils"); const { CONFIG } = require("./config"); const registryUri = generateUriForHostAndPort( - CONFIG().REGISTRY.PROTOCOL, - CONFIG().REGISTRY.HOST, - CONFIG().REGISTRY.PORT + CONFIG().CADT.PROTOCOL, + CONFIG().CADT.HOST, + CONFIG().CADT.PORT ); const getTokenizedUnits = () => { @@ -26,8 +26,8 @@ const getTokenizedUnits = () => { return "/v1/units" + newQuery; }, onProxyReq: (proxyReq) => { - if (CONFIG().REGISTRY.API_KEY) { - proxyReq.setHeader("x-api-key", CONFIG().REGISTRY.API_KEY); + if (CONFIG().CADT.API_KEY) { + proxyReq.setHeader("x-api-key", CONFIG().CADT.API_KEY); } }, onProxyRes: async (proxyRes) => { @@ -55,8 +55,8 @@ const getProjectsFromRegistry = () => { return "/v1/projects" + newQuery; }, onProxyReq: (proxyReq) => { - if (CONFIG().REGISTRY.API_KEY) { - proxyReq.setHeader("x-api-key", CONFIG().REGISTRY.API_KEY); + if (CONFIG().CADT.API_KEY) { + proxyReq.setHeader("x-api-key", CONFIG().CADT.API_KEY); } }, onProxyRes: async (proxyRes) => { @@ -87,8 +87,8 @@ const getUntokenizedUnits = () => { return "/v1/units" + newQuery; }, onProxyReq: (proxyReq) => { - if (CONFIG().REGISTRY.API_KEY) { - proxyReq.setHeader("x-api-key", CONFIG().REGISTRY.API_KEY); + if (CONFIG().CADT.API_KEY) { + proxyReq.setHeader("x-api-key", CONFIG().CADT.API_KEY); } }, onProxyRes: async (proxyRes) => { diff --git a/src/server.js b/src/server.js index 64be353..61600e8 100644 --- a/src/server.js +++ b/src/server.js @@ -66,22 +66,12 @@ app.get("/health", (req, res) => { }); // Initialize server -let shouldListen = false; - -// Check if CONFIG.TOKENIZATION_ENGINE.HOST is either "localhost" or "127.0.0.1" -// OR if CONFIG.TOKENIZATION_ENGINE.API_KEY exists. -// In either case, set shouldListen to true. -if ( - ["localhost", "127.0.0.1"].includes(CONFIG().TOKENIZATION_ENGINE.HOST) || - CONFIG().TOKENIZATION_ENGINE.API_KEY -) { - shouldListen = true; -} +let server; -if (shouldListen) { - app.listen( +const startServer = () => { + server = app.listen( CONFIG().TOKENIZATION_ENGINE.PORT, - CONFIG().TOKENIZATION_ENGINE.HOST, + CONFIG().TOKENIZATION_ENGINE.BIND_ADDRESS, () => { logger.info( `Application is running on port ${CONFIG().TOKENIZATION_ENGINE.PORT}.` @@ -93,10 +83,45 @@ if (shouldListen) { if (CONFIG().GENERAL.CORE_REGISTRY_MODE) { setTimeout(() => scheduler.start(), 5000); } +}; + +const stopServer = () => { + if (server) { + server.close((err) => { + if (err) { + logger.error("Error closing server:", err); + } else { + logger.info("Server closed gracefully."); + } + }); + } +}; + +let shouldListen = false; + +// Check if CONFIG.TOKENIZATION_ENGINE.BIND_ADDRESS is either "localhost" or "127.0.0.1" +// OR if CONFIG.TOKENIZATION_ENGINE.API_KEY exists. +// In either case, set shouldListen to true. +if ( + ["localhost", "127.0.0.1"].includes( + CONFIG().TOKENIZATION_ENGINE.BIND_ADDRESS + ) || + CONFIG().TOKENIZATION_ENGINE.API_KEY +) { + shouldListen = true; +} + +if (shouldListen) { + startServer(); } else { logger.warn( "Server not started due to missing CONFIG.TOKENIZATION_ENGINE.API_KEY on a non-local host." ); } -module.exports = app; +// Export the start and stop server functions +module.exports = { + app, + startServer, + stopServer, +}; diff --git a/src/tasks/sync-retirements.js b/src/tasks/sync-retirements.js index 5733832..c8a5fb8 100644 --- a/src/tasks/sync-retirements.js +++ b/src/tasks/sync-retirements.js @@ -19,7 +19,7 @@ const task = new Task("sync-retirements", async () => { await startSyncRetirementsTask(); } } catch (error) { - logger.task_error(`Error in sync-retirements task: ${error.message}`); + logger.error(`Error in sync-retirements task: ${error.message}`); } finally { isTaskInProgress = false; } diff --git a/src/utils/defaultConfig.json b/src/utils/defaultConfig.json index 443a35b..5becf35 100644 --- a/src/utils/defaultConfig.json +++ b/src/utils/defaultConfig.json @@ -10,7 +10,7 @@ "CERTIFICATE_FOLDER_PATH": null, "ALLOW_SELF_SIGNED_CERTIFICATES": true }, - "REGISTRY": { + "CADT": { "PROTOCOL": "http", "HOST": "127.0.0.1", "PORT": 31310, @@ -22,7 +22,7 @@ "PORT": 31313, "API_KEY": null }, - "TOKEN_DRIVER": { + "CHIA_CLIMATE_TOKENIZATION": { "PROTOCOL": "http", "HOST": "127.0.0.1", "PORT": 31312, @@ -33,6 +33,7 @@ "HOST": "127.0.0.1", "API_KEY": null, "PORT": 31311, + "BIND_ADDRESS": "localhost", "UNITS_FILTER": "unitStatus:[\"Retired\", \"Cancelled\", \"Expired\"]:not", "TASKS": { "SYNC_RETIREMENTS_TO_REGISTRY_INTERVAL_SECONDS": 300 diff --git a/tests/create-token.test.js b/tests/create-token.test.js index d1a968d..de6f83d 100644 --- a/tests/create-token.test.js +++ b/tests/create-token.test.js @@ -3,7 +3,7 @@ const sinon = require("sinon"); const request = require("supertest"); const nock = require("nock"); -const app = require("../src/server"); +const { app, stopServer } = require("../src/server"); const wallet = require("../src/chia/wallet"); const registry = require("../src/api/registry"); const tokenDriver = require("../src/api/token-driver"); @@ -14,14 +14,12 @@ const { CONFIG, setConfig } = require("../src/config"); // Create a nock interceptor for the registry API const registryMock = nock( - `${CONFIG().REGISTRY.PROTOCOL}://${CONFIG().REGISTRY.HOST}:${ - CONFIG().REGISTRY.PORT - }` + `${CONFIG().CADT.PROTOCOL}://${CONFIG().CADT.HOST}:${CONFIG().CADT.PORT}` ); const tokenDriverMock = nock( - `${CONFIG().TOKEN_DRIVER.PROTOCOL}://${CONFIG().TOKEN_DRIVER.HOST}:${ - CONFIG().TOKEN_DRIVER.PORT - }` + `${CONFIG().CHIA_CLIMATE_TOKENIZATION.PROTOCOL}://${ + CONFIG().CHIA_CLIMATE_TOKENIZATION.HOST + }:${CONFIG().CHIA_CLIMATE_TOKENIZATION.PORT}` ); describe("Create Token Process", () => { @@ -56,6 +54,11 @@ describe("Create Token Process", () => { nock.cleanAll(); }); + afterAll(async () => { + // Close the server gracefully + await stopServer(); + }); + it("health check", async () => { const response = await request(app).get("/health"); expect(response.status).to.equal(200);