better config for store. better error logging for

redis connection
This commit is contained in:
Peter Morton 2023-10-12 21:23:14 -05:00
parent 5dd83863cb
commit ca8d50241e
4 changed files with 23 additions and 12 deletions

6
.env
View File

@ -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
# for example if STORE = redis
STORE_URL = redis://localhost:6379

View File

@ -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;

View File

@ -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",
},
};

View File

@ -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}`);