Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
fix(config)!: use HOST and PORT env variables for cloud services
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `SERVICE_HOST` and `SERVICE_PORT` env variables renamed to `HOST` and `PORT` respectively.
  • Loading branch information
Fdawgs committed Sep 8, 2022
1 parent a59a22f commit 45ead4f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ NODE_ENV="development"

# NOTE: Needs to be kept at "0.0.0.0" if using Docker
# Defaults to resolving to "localhost" if not set
SERVICE_HOST=
SERVICE_PORT=8204
HOST=
PORT=8204

### CORS ##################################################

Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ services:
LOG_ROTATION_DATE_FORMAT:
LOG_ROTATION_FILENAME:
NODE_ENV: production
SERVICE_HOST: 0.0.0.0
HOST: 0.0.0.0
healthcheck:
test: curl "localhost:${SERVICE_PORT:-8204}/admin/healthcheck"
test: curl "localhost:${PORT:-8204}/admin/healthcheck"
interval: 60s
timeout: 3s
start_period: 5s
Expand All @@ -45,7 +45,7 @@ services:
max-file: "${LOG_ROTATION_MAX_LOGS:-10}"
max-size: "${LOG_ROTATION_MAX_SIZE:-100m}"
ports:
- "127.0.0.1:${SERVICE_PORT:-8204}:${SERVICE_PORT:-8204}"
- "127.0.0.1:${PORT:-8204}:${PORT:-8204}"
restart: "${DOCKER_RESTART_POLICY:-unless-stopped}"

volumes:
Expand Down
56 changes: 28 additions & 28 deletions src/config/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe("Configuration", () => {

test("Should use defaults if values missing and return values according to environment variables", async () => {
const NODE_ENV = "development";
const SERVICE_HOST = faker.internet.ip();
const SERVICE_PORT = faker.datatype.number();
const HOST = faker.internet.ip();
const PORT = faker.datatype.number();
const CORS_ORIGIN = "";
const CORS_ALLOWED_HEADERS = "";
const CORS_ALLOW_CREDENTIALS = "";
Expand Down Expand Up @@ -51,8 +51,8 @@ describe("Configuration", () => {

Object.assign(process.env, {
NODE_ENV,
SERVICE_HOST,
SERVICE_PORT,
HOST,
PORT,
CORS_ORIGIN,
CORS_ALLOWED_HEADERS,
CORS_ALLOW_CREDENTIALS,
Expand Down Expand Up @@ -81,8 +81,8 @@ describe("Configuration", () => {
const config = await getConfig();

expect(config.fastify).toEqual({
host: SERVICE_HOST,
port: SERVICE_PORT,
host: HOST,
port: PORT,
});

expect(config.fastifyInit.logger).toEqual({
Expand Down Expand Up @@ -141,8 +141,8 @@ describe("Configuration", () => {

test("Should return values according to environment variables - HTTPS (SSL cert) enabled and HTTP2 enabled", async () => {
const NODE_ENV = "development";
const SERVICE_HOST = faker.internet.ip();
const SERVICE_PORT = faker.datatype.number();
const HOST = faker.internet.ip();
const PORT = faker.datatype.number();
const HTTPS_SSL_CERT_PATH =
"./test_resources/test_ssl_cert/server.cert";
const HTTPS_SSL_KEY_PATH = "./test_resources/test_ssl_cert/server.key";
Expand Down Expand Up @@ -172,8 +172,8 @@ describe("Configuration", () => {

Object.assign(process.env, {
NODE_ENV,
SERVICE_HOST,
SERVICE_PORT,
HOST,
PORT,
HTTPS_SSL_CERT_PATH,
HTTPS_SSL_KEY_PATH,
HTTPS_HTTP2_ENABLED,
Expand All @@ -199,8 +199,8 @@ describe("Configuration", () => {
const config = await getConfig();

expect(config.fastify).toEqual({
host: SERVICE_HOST,
port: SERVICE_PORT,
host: HOST,
port: PORT,
});

expect(config.fastifyInit.logger).toEqual({
Expand Down Expand Up @@ -257,8 +257,8 @@ describe("Configuration", () => {
});

test("Should return values according to environment variables - HTTPS (PFX cert) enabled and HTTP2 enabled", async () => {
const SERVICE_HOST = faker.internet.ip();
const SERVICE_PORT = faker.datatype.number();
const HOST = faker.internet.ip();
const PORT = faker.datatype.number();
const HTTPS_PFX_FILE_PATH =
"./test_resources/test_ssl_cert/server.cert"; // Not an actual PFX file
const HTTPS_PFX_PASSPHRASE = faker.lorem.word();
Expand All @@ -272,8 +272,8 @@ describe("Configuration", () => {
const ADMIN_PASSWORD = "password";

Object.assign(process.env, {
SERVICE_HOST,
SERVICE_PORT,
HOST,
PORT,
HTTPS_PFX_FILE_PATH,
HTTPS_PFX_PASSPHRASE,
HTTPS_HTTP2_ENABLED,
Expand All @@ -285,8 +285,8 @@ describe("Configuration", () => {
const config = await getConfig();

expect(config.fastify).toEqual({
host: SERVICE_HOST,
port: SERVICE_PORT,
host: HOST,
port: PORT,
});

expect(config.fastifyInit.https).toEqual({
Expand Down Expand Up @@ -349,8 +349,8 @@ describe("Configuration", () => {
])(
"Should return values according to environment variables - $testName",
async ({ envVariables, expected }) => {
const SERVICE_HOST = faker.internet.ip();
const SERVICE_PORT = faker.datatype.number();
const HOST = faker.internet.ip();
const PORT = faker.datatype.number();
const CORS_ORIGIN = envVariables.CORS_ORIGIN;
const CORS_ALLOWED_HEADERS =
"Accept, Authorization, Content-Type, Origin, X-Requested-With";
Expand All @@ -367,8 +367,8 @@ describe("Configuration", () => {
const ADMIN_PASSWORD = "password";

Object.assign(process.env, {
SERVICE_HOST,
SERVICE_PORT,
HOST,
PORT,
CORS_ORIGIN,
CORS_ALLOWED_HEADERS,
CORS_ALLOW_CREDENTIALS,
Expand All @@ -382,8 +382,8 @@ describe("Configuration", () => {
const config = await getConfig();

expect(config.fastify).toEqual({
host: SERVICE_HOST,
port: SERVICE_PORT,
host: HOST,
port: PORT,
});

expect(config.cors).toEqual({
Expand Down Expand Up @@ -415,8 +415,8 @@ describe("Configuration", () => {
},
},
])("Should throw error if $testName", async ({ envVariables }) => {
const SERVICE_HOST = faker.internet.ip();
const SERVICE_PORT = faker.datatype.number();
const HOST = faker.internet.ip();
const PORT = faker.datatype.number();
const HTTPS_SSL_KEY_PATH = envVariables?.HTTPS_SSL_KEY_PATH || "";
const HTTPS_SSL_CERT_PATH = envVariables?.HTTPS_SSL_CERT_PATH || "";
const HTTPS_PFX_FILE_PATH = envVariables?.HTTPS_PFX_FILE_PATH || "";
Expand All @@ -430,8 +430,8 @@ describe("Configuration", () => {
const ADMIN_PASSWORD = "password";

Object.assign(process.env, {
SERVICE_HOST,
SERVICE_PORT,
HOST,
PORT,
HTTPS_SSL_CERT_PATH,
HTTPS_SSL_KEY_PATH,
HTTPS_PFX_FILE_PATH,
Expand Down
14 changes: 7 additions & 7 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ async function getConfig() {
.prop("NODE_ENV", S.string())

// Service
.prop("SERVICE_HOST", S.string())
.prop("SERVICE_PORT", S.number())
.prop("HOST", S.string())
.prop("PORT", S.number())

// CORS
.prop("CORS_ORIGIN", S.anyOf([S.string(), S.null()]))
Expand Down Expand Up @@ -129,8 +129,8 @@ async function getConfig() {
.prop("DB_CONNECTION_STRING", S.string())
.required([
"NODE_ENV",
"SERVICE_HOST",
"SERVICE_PORT",
"HOST",
"PORT",
"ADMIN_USERNAME",
"ADMIN_PASSWORD",
"DB_CONNECTION_STRING",
Expand All @@ -139,7 +139,7 @@ async function getConfig() {

const config = {
fastify: {
port: env.SERVICE_PORT,
port: env.PORT,
},
fastifyInit: {
/**
Expand Down Expand Up @@ -273,8 +273,8 @@ async function getConfig() {
};

// Ensure API listens on both IPv4 and IPv6 addresses
if (env.SERVICE_HOST) {
config.fastify.host = env.SERVICE_HOST;
if (env.HOST) {
config.fastify.host = env.HOST;
}

if (env.LOG_ROTATION_FILENAME) {
Expand Down
4 changes: 2 additions & 2 deletions src/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ describe("Server Deployment", () => {

beforeAll(async () => {
Object.assign(process.env, {
SERVICE_HOST: "localhost",
SERVICE_PORT: "8204",
HOST: "localhost",
PORT: "8204",
HTTPS_PFX_PASSPHRASE: "",
HTTPS_PFX_FILE_PATH: "",
HTTPS_SSL_CERT_PATH: "",
Expand Down

0 comments on commit 45ead4f

Please sign in to comment.