Add REDIS_SOCKET env var and prefer it over REDIS_URL

This commit is contained in:
TSR Berry 2023-06-19 13:04:52 +02:00
parent e676e87276
commit 3b7c0b80a1
No known key found for this signature in database
GPG key ID: 52353C0A4CCA15E2
2 changed files with 32 additions and 11 deletions

View file

@ -20,12 +20,13 @@
This app can be configured using the following environment variables: This app can be configured using the following environment variables:
| Name | Description | Default value | Notes | | Name | Description | Default value | Notes |
| :---------- | ----------------------------------------------------------------------------------------- | :-----------: | ---------------------------------------------------------------------- | | :------------- | ----------------------------------------------------------------------------------------- | :-----------: | ---------------------------------------------------------------------- |
| `NODE_ENV` | This should be set to `production` or `development` depending on the current environment. | `""` | | | `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"` | | | `HOST` | The address this server should be listening on. | `"127.0.0.1"` | |
| `PORT` | The port this server should be using. | `3000` | | | `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_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 ## Contribute

View file

@ -1,6 +1,12 @@
import express from "express"; import express from "express";
import actuator from "express-actuator"; import actuator from "express-actuator";
import { createClient } from "redis"; import {
RedisClientOptions,
RedisFunctions,
RedisModules,
RedisScripts,
createClient,
} from "redis";
import winston from "winston"; import winston from "winston";
import apiRouter from "./api"; import apiRouter from "./api";
import { errorLogger, requestLogger } from "./middleware"; import { errorLogger, requestLogger } from "./middleware";
@ -20,12 +26,26 @@ export const logger = loggerInstance.child({
source: "Node", source: "Node",
}); });
// Init Redis client const redisClientOptions: RedisClientOptions<
export const redisClient = createClient({ RedisModules,
url: process.env.REDIS_URL, RedisFunctions,
RedisScripts
> = {
// NOTE: Enable this if we ever start using cluster mode // NOTE: Enable this if we ever start using cluster mode
// readonly: true, // 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) => redisClient.on("error", (err: Error) =>
loggerInstance.error(err.message, { loggerInstance.error(err.message, {