Skip to content

Commit

Permalink
Move Trackbear over to PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
dispatchrabbi committed Feb 13, 2024
1 parent 25bf3a7 commit 5ae45ca
Show file tree
Hide file tree
Showing 39 changed files with 678 additions and 76 deletions.
19 changes: 13 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# See docs/env.md for more details.

# Contaner volume paths
## Contaner volume paths
LOGS_VOLUME_DIR=./logs
CERTS_VOLUME_DIR=./certs
DB_VOLUME_DIR=./db
Expand All @@ -9,22 +9,29 @@ DB_VOLUME_DIR=./db
PORT=3000
HAS_PROXY=0

# HTTPS/TLS
## HTTPS/TLS
ENABLE_TLS=0
TLS_KEY_PATH=
TLS_CERT_PATH=
TLS_ALLOW_SELF_SIGNED=0

# Logs
## Logs
LOG_LEVEL=info

# Database
## Database
DATABASE_USER=postgres
DATABASE_PASSWORD=postgres
DATABASE_NAME=trackbear
DATABASE_HOST=db
# This is used in prisma.schema; don't change it
DATABASE_URL=postgresql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}

DB_APP_DB_URL=file:/db/trackbear.db

# Session/Cookies
## Session/Cookies
COOKIE_SECRET=replace-this-with-random-characters

# Email
## Email
ENABLE_EMAIL=1
MAILERSEND_API_KEY=
EMAIL_URL_PREFIX=
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# base stage
FROM node:lts-slim as base

# Install the latest openssl (maybe we need alpine?)
RUN apt-get update -y && apt-get install -y openssl curl
# Install the latest openssl (for HTTPS), curl, and psql (for debugging)
RUN apt update -y && apt install -y openssl curl postgresql-client

# Default NODE_ENV to development (the safest); later stages will override this if needed
ENV NODE_ENV=development
Expand Down Expand Up @@ -58,7 +58,7 @@ ENV DB_APP_DB_URL $DB_APP_DB_URL
RUN npx prisma generate

# Check every 30s to ensure /api/ping returns HTTP 200
HEALTHCHECK --interval=30s CMD node ./scripts/healthcheck.js
HEALTHCHECK --interval=30s CMD node ./scripts/healthchecks/trackbear.js

# Start the server (this also runs migrations)
CMD [ "./entrypoint.sh" ]
Expand All @@ -81,7 +81,7 @@ RUN npx prisma generate
RUN npm run build:client

# Check every 30s to ensure /api/ping returns HTTP 200
HEALTHCHECK --interval=30s CMD node ./scripts/healthcheck.js
HEALTHCHECK --interval=30s CMD node ./scripts/healthchecks/trackbear.js

# Start the server (this also runs migrations)
CMD [ "./entrypoint.sh" ]
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@

> TrackBear is... not even in beta. It's in super-mega-alpha mode right now. Please don't use it unless you are willing to be testing an alpha build.
## Installation
## Setup

```sh
# install dependencies
npm install

# copy the .env file and then fill it out
cp .env.example .env
```

## Setup
See [the environment variable documentation](./docs/env.md) for more details on environment variables.

Then set up the docker stack:

```sh
# copy the .env file
cp .env.example .env
# build the container
docker compose build

# run the docker compose stack
docker compose up
```

See [the environment variable documentation](./docs/env.md) for more details on environment variables.
**The first time you set up your docker stack, you will need to manually create the `queue` database.** This is a limitation of the Postgres docker container. Exec into the container (`docker exec -it /bin/bash trackbear-db-1`) and run:

```sh
createdb -U $POSTGRES_USER queue
```

## Developing

Expand All @@ -34,6 +46,10 @@ npm run watch

Starting the container in watch mode means that it will either copy in changed files or restart the container (depending on what's needed) as you save files. This enables HMR and other creature comforts.

See [the docker documentation](./docs/docker.md) for more details on specific commands.

### Production mode

You can also start up the app in a docker container in production mode:

```sh
Expand All @@ -46,7 +62,7 @@ docker compose -f docker-compose.production.yaml up -d
npm run start:prod
```

See [the docker documentation](./docs/docker.md) for more details on specific commands.
### Outside of a container

You *can* start up the app locally (outside of a container) using `npm run local:start:dev` and `npm run local:start:prod` but **these are deprecated and you shouldn't use them**.

Expand Down
33 changes: 19 additions & 14 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ services:
ports:
- "${PORT}:${PORT}" # http(s)
- 24678:24678 # HMR
# TODO: migrate from sqlite to postgres
# depends_on:
# db:
# condition: service_healthy
depends_on:
db:
condition: service_healthy
env_file:
- ./.env
volumes:
Expand Down Expand Up @@ -67,13 +66,19 @@ services:
action: rebuild
target: .

# db:
# image: postgres:alpine
# environment:
# POSTGRES_USER: postgres
# POSTGRES_PASSWORD: postgres
# volumes:
# - ./healthchecks:/healthchecks
# healthcheck:
# test: /healthchecks/postgres-healthcheck
# interval: "5s"
db:
image: postgres:16-alpine
env_file:
- ./.env
environment:
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_INITDB_ARGS: -E UTF8
volumes:
- ./scripts/healthchecks:/healthchecks
- ${DB_VOLUME_DIR}/postgres:/var/lib/postgresql/data:rw
- ${DB_VOLUME_DIR}:/data:rw
healthcheck:
test: /healthchecks/postgres.sh
interval: "5s"
4 changes: 4 additions & 0 deletions docs/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ These are only needed for working with the Docker Compose files.
## Database
| Variable | Default | Notes |
| --- | --- | --- |
| `DATABASE_USER` | | The username to use with the Postgres database |
| `DATABASE_PASSWORD` | | The password to use with the Postgres database |
| `DATABASE_NAME` | | The database name to use |
| `DATABASE_HOST` | | The hostname for the Postgres database |
| `DB_APP_DB_URL` | `file:/db/trackbear.db` | This URL is used by the Prisma schema (see *prisma/schema.prisma*) to connect to the database. |
| `DB_PATH` | `/db` | The directory to create databases in. Don't set this unless you're running outside a container for some reason. |

Expand Down
Loading

0 comments on commit 5ae45ca

Please sign in to comment.