From ca8d50241e51d03f3d0a331dd66cf7a66696584a Mon Sep 17 00:00:00 2001 From: "Peter.Morton" Date: Thu, 12 Oct 2023 21:23:14 -0500 Subject: [PATCH] better config for store. better error logging for redis connection --- .env | 6 +++--- src/api/routes/config.js | 8 +++++--- src/config/index.js | 7 ++++--- src/utils/store.js | 14 +++++++++++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/.env b/.env index faec5c4..fa784af 100644 --- a/.env +++ b/.env @@ -1,7 +1,7 @@ PORT = 3000 ## Store type 'local-storage' or 'redis' supported -STORE = local-storage +STORE_PROVIDER = redis -# use if STORE = redis -REDIS_URL = redis://localhost:6379 \ No newline at end of file +# for example if STORE = redis +STORE_URL = redis://localhost:6379 \ No newline at end of file diff --git a/src/api/routes/config.js b/src/api/routes/config.js index 1216852..efb4197 100644 --- a/src/api/routes/config.js +++ b/src/api/routes/config.js @@ -1,10 +1,12 @@ import { Router } from "express"; +import config from "../../config/index.js"; + const router = Router(); router.get("/", (req, res) => { - const config = { + res.send({ + config: config, env: process.env, - }; - res.send(config); + }); }); export default router; diff --git a/src/config/index.js b/src/config/index.js index 863b980..53743c7 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -25,14 +25,15 @@ export default { level: process.env.LOG_LEVEL || "silly", }, - redis: { - url: process.env.REDIS_URL || "redis://localhost:6379", + store: { + provider: process.env.STORE_PROVIDER || "local-storage", + url: process.env.STORE_URL || "redis://localhost:6379", }, /** * API configs */ api: { - prefix: "/api", + prefix: process.env.API_PREFIX || "/api", }, }; diff --git a/src/utils/store.js b/src/utils/store.js index 3fed32e..5f5a945 100644 --- a/src/utils/store.js +++ b/src/utils/store.js @@ -7,7 +7,7 @@ import Promise from "promise"; const store = await createStore(); async function createStore() { - if (process.env.STORE === "redis") { + if (config.store.provider === "redis") { return { client: await createRedisClient(), @@ -37,10 +37,18 @@ async function createStore() { async function createRedisClient() { const client = createClient({ - url: config.redis.url, + url: config.store.url, }); - client.on("error", (err) => logger.error("Redis Client Error", err)); + client.on("error", (err) => { + if (err.errors) { + err.errors.forEach((error) => { + logger.error(`Redis Client Error ${error}`); + }); + } else { + logger.error(`Redis Client Error ${err.message}`); + } + }); await client.connect(); logger.info(`Connect store to ${config.redis.url}`);