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
|
||||
|
||||
- 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";
|
||||
|
||||
const pool = new Pool({
|
||||
user: "klestiselimaj",
|
||||
host: "localhost",
|
||||
user: envEnsure("DB_USER"),
|
||||
host: envEnsure("DB_HOST"),
|
||||
database: "postgres",
|
||||
password: "",
|
||||
port: 5432,
|
||||
password: envEnsure("DB_PASSWORD"),
|
||||
port: envNumber("DB_PORT") || 5432,
|
||||
});
|
||||
|
||||
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