Skip to content

Commit fc0548e

Browse files
authored
Merge pull request #23 from alexrecuenco/psql
PSQL
2 parents 93527a9 + 089c7bc commit fc0548e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+8986
-5111
lines changed

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
**/build-*
3535
**/dist
3636
LICENSE
37-
README.md
37+
README*.md

.github/workflows/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ name: Node.js CI
22

33
on:
44
pull_request:
5+
branches: [main, dev, multiroot]
56
push:
6-
branches: [main, dev]
7+
branches: [main, dev, multiroot]
78

89
jobs:
910
pre-commit:

.vscode/tasks.json

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
"dependsOn": [
1010
"npm: dev - packages/frontend",
1111
"npm: dev - packages/backend",
12+
"npm: dev - packages/interface",
1213
],
14+
"problemMatcher": [],
1315
},
1416
{
1517
"type": "npm",
1618
"script": "dev",
1719
"isBackground": true,
1820
"path": "packages/frontend",
19-
// See here https://stackoverflow.com/a/72655542/7346915
2021
"problemMatcher": [
2122
{
2223
"owner": "typescript",
@@ -25,8 +26,8 @@
2526
"applyTo": "allDocuments",
2627
"background": {
2728
"activeOnStart": true,
28-
// "beginsPattern": "sd",
29-
// "endsPattern": " > "
29+
"beginsPattern": "ready",
30+
"endsPattern": "ready",
3031
},
3132
"pattern": [
3233
{
@@ -50,10 +51,46 @@
5051
"type": "npm",
5152
"script": "dev",
5253
"isBackground": true,
54+
"dependsOn": ["compose: database"],
5355
"path": "packages/backend",
54-
"problemMatcher": ["$tsc-watch"],
56+
"problemMatcher": [
57+
{
58+
"owner": "typescript",
59+
"source": "ts",
60+
"applyTo": "allDocuments",
61+
"background": {
62+
"activeOnStart": true,
63+
"beginsPattern": ".*starting.*",
64+
"endsPattern": ".*running.*",
65+
},
66+
},
67+
],
5568
"label": "npm: dev - packages/backend",
56-
"detail": "tsc-watch --onSuccess 'node .'",
69+
},
70+
{
71+
"type": "npm",
72+
"script": "dev",
73+
"isBackground": true,
74+
"path": "packages/interface",
75+
"problemMatcher": ["$tsc-watch"],
76+
"label": "npm: dev - packages/interface",
77+
},
78+
{
79+
"type": "shell",
80+
"command": "docker",
81+
"args": [
82+
"compose",
83+
"-f",
84+
"compose.yaml",
85+
"-f",
86+
"compose.debug.yaml",
87+
"up",
88+
"db",
89+
"-d",
90+
],
91+
"isBackground": false,
92+
"problemMatcher": [],
93+
"label": "compose: database",
5794
},
5895
],
5996
}

README.Docker.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ Your application will be available at http://localhost:3000.
77

88
### Deploying your application to the cloud
99

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

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

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

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

README.md

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,104 @@
11
# Example
22

3+
In order do as follows
4+
5+
## Setup
6+
7+
### Docker
8+
9+
1. Copy `dbpass.txt.example` to `dbpass.txt` and set a password
10+
2. Copy `password-file.txt.example` to `password-file.txt` and set the same
11+
password
12+
13+
Alternatively, set `POSTGRES_HOST_AUTH_METHOD=trust` and pass it to docker
14+
compose (No authentication, this is done on debug mode)
15+
16+
### Local
17+
18+
1. Install **node 22**
19+
2. Run `npm install`
20+
21+
## Local Development
22+
23+
### Build
24+
25+
Only required for local development
26+
27+
```bash
28+
npm run build -ws
29+
```
30+
31+
### Test
32+
33+
```bash
34+
npm run test -ws
35+
```
36+
37+
### Prepare database for local development
38+
39+
```bash
40+
# at a separate terminal run this and keep running
41+
docker compose -f compose.yaml -f compose.debug.yaml up db
42+
# In a separate terminal, (I believe postgresql does this automatically)
43+
# npm run db:prepare -w backend
44+
```
45+
46+
### Launch
47+
48+
### Hot-reload mode
49+
50+
In `vscode` use the task labeled `dev`.
51+
52+
In the terminal you want to open two terminals, opening a database and the
53+
development environment
54+
55+
```bash
56+
# Then keep this process running
57+
npm run dev
58+
```
59+
60+
Clean resources
61+
62+
```bash
63+
docker compose -f compose.yaml -f compose.debug.yaml down -v db
64+
npm run clean -ws --if-present
65+
```
66+
67+
Access `http://localhost:3000/tasks/debug/populate` to populate a few tasks (or
68+
click the populate button)
69+
70+
## Docker _deployment_
71+
72+
```bash
73+
docker compose up
74+
```
75+
76+
App will be accessible on port 8080
77+
78+
### Launch in debug mode
79+
80+
TODO, WIP
81+
382
## Using workspaces
483

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

86+
Examples:
87+
788
```bash
8-
npm i -ws
9-
npm install --workspaces
89+
npm i --workspaces
90+
npm ci -ws
91+
npm run build -ws --if-present
92+
npm run test -ws --if-present
93+
npm run clean -ws --include-workspace-root --if-present
1094
```
95+
96+
Or in a specific workspace
97+
98+
```bash
99+
npm i --workspace=backend
100+
npm ci -w=backend --omit=dev
101+
npm run start --workspace=backend
102+
```
103+
104+
Use auto-reload with

compose.debug.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# TODO: Make this auto-reloadable and expose debug ports to the outside world
2+
services:
3+
frontend: {}
4+
backend:
5+
ports:
6+
# Avoid conflicts with local development
7+
- 3003:3000
8+
db:
9+
environment:
10+
POSTGRES_HOST_AUTH_METHOD: trust
11+
ports:
12+
- 5432:5432
13+
volumes:
14+
- db-debug:/var/lib/postgresql/data
15+
volumes:
16+
db-debug:

compose.yaml

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,77 @@
88
# database or a cache. For examples, see the Awesome Compose repository:
99
# https://github.com/docker/awesome-compose
1010
services:
11-
backend:
12-
build:
13-
context: .
14-
target: backend
15-
environment:
16-
NODE_ENV: production
17-
PORT: 3000
18-
ports:
19-
- 3000:3000
2011
frontend:
2112
build:
2213
context: .
2314
target: frontend
2415
environment:
2516
BACKEND_URL: http://backend:3000
2617
PORT: 8080
18+
depends_on:
19+
backend:
20+
condition: service_healthy
2721
ports:
2822
- 8080:8080
23+
backend:
24+
build:
25+
context: .
26+
target: backend
27+
environment:
28+
NODE_ENV: production
29+
PORT: 3000
30+
PGPASSWORDFILE: /run/secrets/db-password
31+
PGHOST: db
32+
PGUSER: postgres
33+
PGPORT: 5432
34+
PGDATABASE: taskdb
35+
depends_on:
36+
db:
37+
condition: service_healthy
38+
secrets:
39+
- db-password
40+
healthcheck:
41+
test:
42+
- CMD
43+
- /usr/bin/wget
44+
- --no-verbose
45+
- --server-response
46+
- --tries=1
47+
- --spider
48+
- http://localhost:3000/health
49+
interval: 5s
50+
start_interval: 1s
51+
start_period: 1s
52+
timeout: 1s
53+
retries: 3
54+
db:
55+
image: postgres:17-alpine
56+
restart: always
57+
user: postgres
58+
secrets:
59+
- db-password
60+
volumes:
61+
- db-data:/var/lib/postgresql/data
62+
- ./packages/backend/db:/docker-entrypoint-initdb.d:ro
63+
environment:
64+
POSTGRES_DB: taskdb
65+
POSTGRES_PASSWORD_FILE: /run/secrets/db-password
66+
POSTGRES_HOST_AUTH_METHOD: $POSTGRES_HOST_AUTH_METHOD
67+
expose:
68+
- 5432
69+
70+
healthcheck:
71+
test: ['CMD', 'pg_isready']
72+
interval: 5s
73+
start_interval: 1s
74+
start_period: 1s
75+
timeout: 1s
76+
retries: 3
2977

30-
# The commented out section below is an example of how to define a PostgreSQL
31-
# database that your application can use. `depends_on` tells Docker Compose to
32-
# start the database before your application. The `db-data` volume persists the
33-
# database data between container restarts. The `db-password` secret is used
34-
# to set the database password. You must create `db/password.txt` and add
35-
# a password of your choosing to it before running `docker-compose up`.
36-
# depends_on:
37-
# db:
38-
# condition: service_healthy
39-
# db:
40-
# image: postgres
41-
# restart: always
42-
# user: postgres
43-
# secrets:
44-
# - db-password
45-
# volumes:
46-
# - db-data:/var/lib/postgresql/data
47-
# environment:
48-
# - POSTGRES_DB=example
49-
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
50-
# expose:
51-
# - 5432
52-
# healthcheck:
53-
# test: [ "CMD", "pg_isready" ]
54-
# interval: 10s
55-
# timeout: 5s
56-
# retries: 5
57-
# volumes:
58-
# db-data:
59-
# secrets:
60-
# db-password:
61-
# file: db/password.txt
78+
volumes:
79+
db-data:
80+
secrets:
81+
db-password:
82+
file: db/dbpass.txt
83+
db-passwordfile:
84+
file: db/password-file.txt

db/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.txt

db/dbpass.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
YOUR_PGPASSWORD_HERE

db/password-file.txt.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# https://www.postgresql.org/docs/current/libpq-pgpass.html
2+
# hostname:port:database:username:password
3+
# copy this file to password.txt in this same folder
4+
*:*:*:postgres:YOUR_PGPASSWORD_HERE

0 commit comments

Comments
 (0)