Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom PostgreSQL configuration for API server endpoint #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ Deployment
3. Run: `kgi-processing --connection-string=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?sslmode=disable`
6. Run `api`
1. Navigate to wherever you copied `api` to
2. Run: `npm run start`
2. Set the following environment variables:
1. POSTGRES_USER=username - which is the username for database connection.
2. POSTGRES_PASSWORD=password - which is the password for database connection.
3. POSTGRES_DB=database-name - which is the database to be used.
4. POSTGRES_HOST=database.example.com (optional) - which is the host of the database server (default: localhost).
5. POSTGRES_PORT=5432 (optional) - which is the port for database connection (default: 5432).
3. Run: `npm run start`
7. Run `web`
1. Navigate to wherever you copied `web` to
2. Run: `npm install -g serve`
Expand Down
29 changes: 28 additions & 1 deletion api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@ import pg from "pg";
import {AppConfig, Block, BlockHashById, BlocksAndEdgesAndHeightGroups, Edge, HeightGroup} from "./model";
import { packageVersion } from "./version.js";

/* database environment. */
const postgres_user = process.env.POSTGRES_USER;
const postgres_password = process.env.POSTGRES_PASSWORD;
const postgres_host = process.env.POSTGRES_HOST ?? "localhost";
const postgres_port = process.env.POSTGRES_PORT ?? "5432";
const postgres_database = process.env.POSTGRES_DB;

/* missing check. */
if (!postgres_user) {
console.log("The POSTGRES_USER environment variable is required");
process.exit(1);
}
if (!postgres_password) {
console.log("The POSTGRES_PASSWORD environment variable is required");
process.exit(1);
}
if (!postgres_database) {
console.log("The POSTGRES_DB environment variable is required");
process.exit(1);
}

export default class Database {
private pool: pg.Pool;

constructor() {
this.pool = new pg.Pool();
this.pool = new pg.Pool({
user: postgres_user,
password: postgres_password,
host: postgres_host,
port: parseInt(postgres_port),
database: postgres_database,
});
}

withClient = async (func: (client: pg.PoolClient) => Promise<void>) => {
Expand Down
8 changes: 3 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ services:
depends_on:
- postgres
environment:
- PGUSER=${POSTGRES_USER}
- PGPASSWORD=${POSTGRES_PASSWORD}
- PGDATABASE=${POSTGRES_DB}
- PGHOST=localhost
- PGPORT=5432
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- API_PORT=${API_PORT}
command:
- "npm"
Expand Down