Skip to content

PSQL #23

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

Merged
merged 17 commits into from
Oct 28, 2024
Merged

PSQL #23

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
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
**/build-*
**/dist
LICENSE
README.md
README*.md
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ name: Node.js CI

on:
pull_request:
branches: [main, dev, multiroot]
push:
branches: [main, dev]
branches: [main, dev, multiroot]

jobs:
pre-commit:
Expand Down
47 changes: 42 additions & 5 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
"dependsOn": [
"npm: dev - packages/frontend",
"npm: dev - packages/backend",
"npm: dev - packages/interface",
],
"problemMatcher": [],
},
{
"type": "npm",
"script": "dev",
"isBackground": true,
"path": "packages/frontend",
// See here https://stackoverflow.com/a/72655542/7346915
"problemMatcher": [
{
"owner": "typescript",
Expand All @@ -25,8 +26,8 @@
"applyTo": "allDocuments",
"background": {
"activeOnStart": true,
// "beginsPattern": "sd",
// "endsPattern": " > "
"beginsPattern": "ready",
"endsPattern": "ready",
},
"pattern": [
{
Expand All @@ -50,10 +51,46 @@
"type": "npm",
"script": "dev",
"isBackground": true,
"dependsOn": ["compose: database"],
"path": "packages/backend",
"problemMatcher": ["$tsc-watch"],
"problemMatcher": [
{
"owner": "typescript",
"source": "ts",
"applyTo": "allDocuments",
"background": {
"activeOnStart": true,
"beginsPattern": ".*starting.*",
"endsPattern": ".*running.*",
},
},
],
"label": "npm: dev - packages/backend",
"detail": "tsc-watch --onSuccess 'node .'",
},
{
"type": "npm",
"script": "dev",
"isBackground": true,
"path": "packages/interface",
"problemMatcher": ["$tsc-watch"],
"label": "npm: dev - packages/interface",
},
{
"type": "shell",
"command": "docker",
"args": [
"compose",
"-f",
"compose.yaml",
"-f",
"compose.debug.yaml",
"up",
"db",
"-d",
],
"isBackground": false,
"problemMatcher": [],
"label": "compose: database",
},
],
}
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ RUN --mount=type=bind,source=package.json,target=package.json \


# Copy the rest of the source files into the image.
COPY --link . .
COPY --link package*.json tsconfig*.json .
COPY --link packages packages

RUN npm run build --ws
RUN ls && npm run build --ws

FROM node:${NODE_VERSION}-alpine AS production-base

Expand Down
17 changes: 9 additions & 8 deletions README.Docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ Your application will be available at http://localhost:3000.

### Deploying your application to the cloud

First, build your image, e.g.: `docker build -t myapp .`.
If your cloud uses a different CPU architecture than your development
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
you'll want to build the image for that platform, e.g.:
`docker build --platform=linux/amd64 -t myapp .`.
First, build your image, e.g.: `docker build -t myapp .`. If your cloud uses a
different CPU architecture than your development machine (e.g., you are on a Mac
M1 and your cloud provider is amd64), you'll want to build the image for that
platform, e.g.: `docker build --platform=linux/amd64 -t myapp .`.

Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.

Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
docs for more detail on building and pushing.
Consult Docker's
[getting started](https://docs.docker.com/go/get-started-sharing/) docs for more
detail on building and pushing.

### References
* [Docker's Node.js guide](https://docs.docker.com/language/nodejs/)

- [Docker's Node.js guide](https://docs.docker.com/language/nodejs/)
98 changes: 96 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,104 @@
# Example

In order do as follows

## Setup

### Docker

1. Copy `dbpass.txt.example` to `dbpass.txt` and set a password
2. Copy `password-file.txt.example` to `password-file.txt` and set the same
password

Alternatively, set `POSTGRES_HOST_AUTH_METHOD=trust` and pass it to docker
compose (No authentication, this is done on debug mode)

### Local

1. Install **node 22**
2. Run `npm install`

## Local Development

### Build

Only required for local development

```bash
npm run build -ws
```

### Test

```bash
npm run test -ws
```

### Prepare database for local development

```bash
# at a separate terminal run this and keep running
docker compose -f compose.yaml -f compose.debug.yaml up db
# In a separate terminal, (I believe postgresql does this automatically)
# npm run db:prepare -w backend
```

### Launch

### Hot-reload mode

In `vscode` use the task labeled `dev`.

In the terminal you want to open two terminals, opening a database and the
development environment

```bash
# Then keep this process running
npm run dev
```

Clean resources

```bash
docker compose -f compose.yaml -f compose.debug.yaml down -v db
npm run clean -ws --if-present
```

Access `http://localhost:3000/tasks/debug/populate` to populate a few tasks (or
click the populate button)

## Docker _deployment_

```bash
docker compose up
```

App will be accessible on port 8080

### Launch in debug mode

TODO, WIP

## Using workspaces

Most commands can be launched for each workspace by using `--workspaces|-ws`

Examples:

```bash
npm i -ws
npm install --workspaces
npm i --workspaces
npm ci -ws
npm run build -ws --if-present
npm run test -ws --if-present
npm run clean -ws --include-workspace-root --if-present
```

Or in a specific workspace

```bash
npm i --workspace=backend
npm ci -w=backend --omit=dev
npm run start --workspace=backend
```

Use auto-reload with
16 changes: 16 additions & 0 deletions compose.debug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TODO: Make this auto-reloadable and expose debug ports to the outside world
services:
frontend: {}
backend:
ports:
# Avoid conflicts with local development
- 3003:3000
db:
environment:
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
volumes:
- db-debug:/var/lib/postgresql/data
volumes:
db-debug:
105 changes: 64 additions & 41 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,77 @@
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
services:
backend:
build:
context: .
target: backend
environment:
NODE_ENV: production
PORT: 3000
ports:
- 3000:3000
frontend:
build:
context: .
target: frontend
environment:
BACKEND_URL: http://backend:3000
PORT: 8080
depends_on:
backend:
condition: service_healthy
ports:
- 8080:8080
backend:
build:
context: .
target: backend
environment:
NODE_ENV: production
PORT: 3000
PGPASSWORDFILE: /run/secrets/db-password
PGHOST: db
PGUSER: postgres
PGPORT: 5432
PGDATABASE: taskdb
depends_on:
db:
condition: service_healthy
secrets:
- db-password
healthcheck:
test:
- CMD
- /usr/bin/wget
- --no-verbose
- --server-response
- --tries=1
- --spider
- http://localhost:3000/health
interval: 5s
start_interval: 1s
start_period: 1s
timeout: 1s
retries: 3
db:
image: postgres:17-alpine
restart: always
user: postgres
secrets:
- db-password
volumes:
- db-data:/var/lib/postgresql/data
- ./packages/backend/db:/docker-entrypoint-initdb.d:ro
environment:
POSTGRES_DB: taskdb
POSTGRES_PASSWORD_FILE: /run/secrets/db-password
POSTGRES_HOST_AUTH_METHOD: $POSTGRES_HOST_AUTH_METHOD
expose:
- 5432

healthcheck:
test: ['CMD', 'pg_isready']
interval: 5s
start_interval: 1s
start_period: 1s
timeout: 1s
retries: 3

# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the
# database data between container restarts. The `db-password` secret is used
# to set the database password. You must create `db/password.txt` and add
# a password of your choosing to it before running `docker-compose up`.
# depends_on:
# db:
# condition: service_healthy
# db:
# image: postgres
# restart: always
# user: postgres
# secrets:
# - db-password
# volumes:
# - db-data:/var/lib/postgresql/data
# environment:
# - POSTGRES_DB=example
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
# expose:
# - 5432
# healthcheck:
# test: [ "CMD", "pg_isready" ]
# interval: 10s
# timeout: 5s
# retries: 5
# volumes:
# db-data:
# secrets:
# db-password:
# file: db/password.txt
volumes:
db-data:
secrets:
db-password:
file: db/dbpass.txt
db-passwordfile:
file: db/password-file.txt
1 change: 1 addition & 0 deletions db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.txt
1 change: 1 addition & 0 deletions db/dbpass.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YOUR_PGPASSWORD_HERE
4 changes: 4 additions & 0 deletions db/password-file.txt.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# https://www.postgresql.org/docs/current/libpq-pgpass.html
# hostname:port:database:username:password
# copy this file to password.txt in this same folder
*:*:*:postgres:YOUR_PGPASSWORD_HERE
Loading
Loading