Skip to content

Commit

Permalink
Add REDIS_SOCKET env var and prefer it over REDIS_URL
Browse files Browse the repository at this point in the history
  • Loading branch information
TSRBerry committed Jun 19, 2023
1 parent e676e87 commit 3b7c0b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 25 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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, {
Expand Down

0 comments on commit 3b7c0b8

Please sign in to comment.