From 3b7c0b80a1f703b66ef2ab7a80321aec82d9684d Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Mon, 19 Jun 2023 13:04:52 +0200 Subject: [PATCH] Add REDIS_SOCKET env var and prefer it over REDIS_URL --- README.md | 13 +++++++------ src/app.ts | 30 +++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8e28fa1..aa0dc49 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,13 @@ This app can be configured using the following environment variables: -| Name | Description | Default value | Notes | -| :---------- | ----------------------------------------------------------------------------------------- | :-----------: | ---------------------------------------------------------------------- | -| `NODE_ENV` | This should be set to `production` or `development` depending on the current environment. | `""` | | -| `HOST` | The address this server should be listening on. | `"127.0.0.1"` | | -| `PORT` | The port this server should be using. | `3000` | | -| `REDIS_URL` | The URL of the redis server. | `""` | Format: `redis[s]://[[username][:password]@][host][:port][/db-number]` | +| Name | Description | Default value | Notes | +| :------------- | ----------------------------------------------------------------------------------------- | :-----------: | ---------------------------------------------------------------------- | +| `NODE_ENV` | This should be set to `production` or `development` depending on the current environment. | `""` | | +| `HOST` | The address this server should be listening on. | `"127.0.0.1"` | | +| `PORT` | The port this server should be using. | `3000` | | +| `REDIS_URL` | The URL of the redis server. | `""` | Format: `redis[s]://[[username][:password]@][host][:port][/db-number]` | +| `REDIS_SOCKET` | The path to the unix socket of the redis server. | `""` | If this is not empty `REDIS_URL` will be ignored. | ## Contribute diff --git a/src/app.ts b/src/app.ts index d0c0355..affa687 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,12 @@ import express from "express"; import actuator from "express-actuator"; -import { createClient } from "redis"; +import { + RedisClientOptions, + RedisFunctions, + RedisModules, + RedisScripts, + createClient, +} from "redis"; import winston from "winston"; import apiRouter from "./api"; import { errorLogger, requestLogger } from "./middleware"; @@ -20,12 +26,26 @@ export const logger = loggerInstance.child({ source: "Node", }); -// Init Redis client -export const redisClient = createClient({ - url: process.env.REDIS_URL, +const redisClientOptions: RedisClientOptions< + RedisModules, + RedisFunctions, + RedisScripts +> = { // NOTE: Enable this if we ever start using cluster mode // readonly: true, -}); +}; + +// Prefer unix socket over REDIS_URL +if (process.env.REDIS_SOCKET != null && process.env.REDIS_SOCKET.length > 0) { + redisClientOptions.socket = { + path: process.env.REDIS_SOCKET, + }; +} else { + redisClientOptions.url = process.env.REDIS_URL; +} + +// Init Redis client +export const redisClient = createClient(redisClientOptions); redisClient.on("error", (err: Error) => loggerInstance.error(err.message, {