Skip to content

Commit

Permalink
Manage SSL locally/in Heroku
Browse files Browse the repository at this point in the history
  • Loading branch information
textbook committed Feb 1, 2024
1 parent d70050d commit 12bc96a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion api/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ import { default as pg } from "pg";
import config from "./utils/config.js";
import logger from "./utils/logger.js";

const databaseUrl = new URL(config.databaseUrl);

const localDb = ["0.0.0.0", "127.0.0.1", "localhost"].includes(
databaseUrl.hostname,
);
const sslMode = ["prefer", "require", "verify-ca", "verify-full"].includes(
databaseUrl.searchParams.get("sslmode") ?? "none",
);

const pool = new pg.Pool({
connectionString: config.databaseUrl,
connectionString: databaseUrl.toString(),
connectionTimeoutMillis: 5_000,
ssl: localDb ? false : { rejectUnauthorized: sslMode },
});

pool.on("error", (err) => logger.error("%O", err));
Expand Down
2 changes: 1 addition & 1 deletion api/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ configDotenv({ path: dotenvPath });
requireArgs(["DATABASE_URL"]);

/**
* @property {string} databaseUrl
* @property {URL} databaseUrl
* @property {string} dotenvPath
* @property {string} logLevel
* @property {number} port
Expand Down

0 comments on commit 12bc96a

Please sign in to comment.