Using .env for secrets #4
This commit is contained in:
@@ -1,3 +1,16 @@
|
|||||||
|
## Setup
|
||||||
|
### Requirements
|
||||||
|
- Node.Js with npm
|
||||||
|
- Postgres DB
|
||||||
|
|
||||||
|
```python
|
||||||
|
DB_USER= # Usually related to the OS username but do your own research
|
||||||
|
DB_HOST=localhost
|
||||||
|
DB_PASSWORD= # Can be empty
|
||||||
|
DB_PORT=5432 # Optional
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Keywords
|
## Keywords
|
||||||
|
|
||||||
- Voxa Client: The user-side app which communicates with all below.
|
- Voxa Client: The user-side app which communicates with all below.
|
||||||
|
|||||||
+5
-4
@@ -1,11 +1,12 @@
|
|||||||
|
import { envEnsure, envNumber } from "@/lib/env";
|
||||||
import { Pool } from "pg";
|
import { Pool } from "pg";
|
||||||
|
|
||||||
const pool = new Pool({
|
const pool = new Pool({
|
||||||
user: "klestiselimaj",
|
user: envEnsure("DB_USER"),
|
||||||
host: "localhost",
|
host: envEnsure("DB_HOST"),
|
||||||
database: "postgres",
|
database: "postgres",
|
||||||
password: "",
|
password: envEnsure("DB_PASSWORD"),
|
||||||
port: 5432,
|
port: envNumber("DB_PORT") || 5432,
|
||||||
});
|
});
|
||||||
|
|
||||||
const createUsersTableQuery = `
|
const createUsersTableQuery = `
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
export function env(name: string): string | undefined {
|
||||||
|
return process.env[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function envEnsure(name: string): string {
|
||||||
|
const v = process.env[name];
|
||||||
|
|
||||||
|
if (v === undefined) {
|
||||||
|
throw new Error(
|
||||||
|
`Environment variable '${name}' is required but doesn't exist`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function envNumber(name: string): number | undefined {
|
||||||
|
const v = process.env[name];
|
||||||
|
return v ? parseInt(v) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function envNumberEnsure(name: string): number {
|
||||||
|
const v = process.env[name];
|
||||||
|
|
||||||
|
if (v === undefined) {
|
||||||
|
throw new Error(
|
||||||
|
`Environment variable '${name}' is required but doesn't exist`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseInt(v);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user