From 14a734863201332c5de46756a326c892d2e87bb8 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 1 Aug 2026 19:53:32 +0200 Subject: [PATCH] feat: update Docker setup and documentation for PostgreSQL integration --- CollabTableServer/.dockerignore | 1 - CollabTableServer/.env.example | 11 +----- CollabTableServer/README.md | 43 +++++++++------------- CollabTableServer/docker-compose.yml | 50 ------------------------- CollabTableServer/src/db.ts | 13 ++----- CollabTableServer/src/index.ts | 3 +- README.md | 55 ++++++++++++---------------- 7 files changed, 46 insertions(+), 130 deletions(-) delete mode 100644 CollabTableServer/docker-compose.yml diff --git a/CollabTableServer/.dockerignore b/CollabTableServer/.dockerignore index 9eb36cb..c1fab6a 100644 --- a/CollabTableServer/.dockerignore +++ b/CollabTableServer/.dockerignore @@ -7,7 +7,6 @@ dist .vscode .idea README.md -docker-compose.yml Dockerfile data/ collabtable.db diff --git a/CollabTableServer/.env.example b/CollabTableServer/.env.example index 48930a2..f351423 100644 --- a/CollabTableServer/.env.example +++ b/CollabTableServer/.env.example @@ -9,18 +9,11 @@ COOKIE_SECURE=false ################################################################# # PostgreSQL configuration -# You can either provide a single DATABASE_URL or the PG* variables below ################################################################# -# Example URL: postgres://user:password@localhost:5432/collabtable +# Postgres connection string (bring your own - this app does not run Postgres for you). +# Example: postgres://user:password@localhost:5432/collabtable DATABASE_URL= -# If DATABASE_URL is not set, these individual settings are used: -PGHOST=localhost -PGPORT=5432 -PGUSER=postgres -PGPASSWORD= -PGDATABASE=collabtable - ################################################################# # Startup resilience and sync tuning ################################################################# diff --git a/CollabTableServer/README.md b/CollabTableServer/README.md index 28b9011..b702121 100644 --- a/CollabTableServer/README.md +++ b/CollabTableServer/README.md @@ -9,12 +9,12 @@ A collaborative table management server built with Node.js, Express, TypeScript, - PostgreSQL (pg) database backend - Single-admin-account web dashboard login (session cookie, bcrypt-hashed password) plus a long-lived API token for sync clients/devices, generated from the dashboard -- Dockerized deployment with persistent storage +- Dockerized deployment (bring your own PostgreSQL instance) - TypeScript for type safety ## Prerequisites -- Docker and Docker Compose +- Docker and a PostgreSQL instance (bring your own - this app does not run Postgres for you) - Or Node.js 20+ and a PostgreSQL instance (for local development) ## Quick Start with Docker @@ -25,14 +25,18 @@ A collaborative table management server built with Node.js, Express, TypeScript, cd CollabTableServer ``` -3. Copy the example environment file: +3. Build the image: ```bash - cp .env.example .env + docker build -t collabtable-server . ``` -4. Start the services (this also starts a bundled PostgreSQL container): +4. Run it, pointing at your own Postgres instance: ```bash - docker-compose up -d + docker run -d --name collabtable-server --restart unless-stopped \ + -p 3000:3000 \ + -e DATABASE_URL=postgres://user:password@host:5432/collabtable \ + -e COOKIE_SECURE=false \ + collabtable-server ``` The server will be available at `http://localhost:3000`. Open it in a browser to create the @@ -53,12 +57,6 @@ admin account, then generate an API token from the dashboard for your sync clien 3. Point it at a PostgreSQL instance in `.env`: ``` DATABASE_URL=postgres://user:password@localhost:5432/collabtable - # OR, as discrete variables: - # PGHOST=localhost - # PGPORT=5432 - # PGUSER=postgres - # PGPASSWORD=yourpassword - # PGDATABASE=collabtable # Set to "true" once served over HTTPS COOKIE_SECURE=false @@ -199,24 +197,19 @@ npm start ## Docker Commands -Start services: +Stop the container: ```bash -docker-compose up -d -``` - -Stop services: -```bash -docker-compose down +docker stop collabtable-server ``` View logs: ```bash -docker-compose logs -f +docker logs -f collabtable-server ``` Rebuild and restart: ```bash -docker-compose up -d --build +docker build -t collabtable-server . && docker restart collabtable-server ``` ## Environment Variables @@ -224,8 +217,7 @@ docker-compose up -d --build - `PORT` - Server port (default: 3000) - `NODE_ENV` - Environment (development/production) - `COOKIE_SECURE` - Set to `true` to mark the admin session cookie `Secure` (requires HTTPS); default `false` -- `DATABASE_URL` - PostgreSQL connection URL (optional alternative to PG* variables) -- `PGHOST`, `PGPORT`, `PGUSER`, `PGPASSWORD`, `PGDATABASE` - PostgreSQL connection parameters +- `DATABASE_URL` - PostgreSQL connection URL (required; bring your own Postgres instance) - `DB_INIT_RETRY_INTERVAL_MS` - Delay between DB init retries in ms (default: `5000`) - `DB_INIT_RETRY_MAX_ATTEMPTS` - Max DB init retries; `0` retries forever (default: `0`) - `SYNC_MAX_FUTURE_SKEW_MS` - Allowed future timestamp skew from clients during sync (default: `0`) @@ -245,9 +237,8 @@ when left unset. ## Data Persistence -Use your normal PostgreSQL backup/restore procedures (e.g., `pg_dump`, `pg_restore`). If -using the bundled Dockerized Postgres service, the data is stored in a named volume (see -`docker-compose.yml`). +Use your normal PostgreSQL backup/restore procedures (e.g., `pg_dump`, `pg_restore`) against +your own Postgres instance - this app does not manage Postgres storage itself. ## License diff --git a/CollabTableServer/docker-compose.yml b/CollabTableServer/docker-compose.yml deleted file mode 100644 index ce81917..0000000 --- a/CollabTableServer/docker-compose.yml +++ /dev/null @@ -1,50 +0,0 @@ -version: '3.8' - -services: - postgres: - image: postgres:17-alpine - container_name: collabtable-postgres - restart: unless-stopped - environment: - - POSTGRES_DB=collabtable - - POSTGRES_USER=collabtable - - POSTGRES_PASSWORD=collabtable - volumes: - - pg_data:/var/lib/postgresql/data - networks: - - collabtable-network - healthcheck: - test: ["CMD-SHELL", "pg_isready -U collabtable"] - interval: 10s - timeout: 5s - retries: 5 - - server: - build: . - container_name: collabtable-server - restart: unless-stopped - environment: - - PORT=3000 - - NODE_ENV=production - # Session cookie for the admin web dashboard (see src/routes/authRoutes.ts) - - COOKIE_SECURE=false - # Retry DB initialization instead of crashing immediately on startup races - - DB_INIT_RETRY_INTERVAL_MS=5000 - - DB_INIT_RETRY_MAX_ATTEMPTS=0 - # Clamp future client timestamps during sync to avoid resend loops - - SYNC_MAX_FUTURE_SKEW_MS=0 - - DATABASE_URL=postgres://collabtable:collabtable@postgres:5432/collabtable - ports: - - "3000:3000" - depends_on: - postgres: - condition: service_healthy - networks: - - collabtable-network - -volumes: - pg_data: - -networks: - collabtable-network: - driver: bridge diff --git a/CollabTableServer/src/db.ts b/CollabTableServer/src/db.ts index c4c180c..fe82115 100644 --- a/CollabTableServer/src/db.ts +++ b/CollabTableServer/src/db.ts @@ -36,17 +36,10 @@ class PostgresAdapter implements DBAdapter { // OID 20 = INT8, OID 1700 = NUMERIC (keep default for now) pgTypes.setTypeParser(20, (val: string) => parseInt(val, 10)); const connectionString = process.env.DATABASE_URL; - if (connectionString) { - this.pool = new Pool({ connectionString }); - } else { - this.pool = new Pool({ - host: process.env.PGHOST || 'localhost', - port: parseInt(process.env.PGPORT || '5432', 10), - user: process.env.PGUSER || 'postgres', - password: process.env.PGPASSWORD || '', - database: process.env.PGDATABASE || 'collabtable' - }); + if (!connectionString) { + throw new Error('DATABASE_URL environment variable is required'); } + this.pool = new Pool({ connectionString }); } async initialize(): Promise { diff --git a/CollabTableServer/src/index.ts b/CollabTableServer/src/index.ts index 8b89cdc..3921c0e 100644 --- a/CollabTableServer/src/index.ts +++ b/CollabTableServer/src/index.ts @@ -102,8 +102,7 @@ app.use('/', webRoutes); app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); console.log(`Environment: ${process.env.NODE_ENV || 'development'}`); - const source = process.env.DATABASE_URL ? 'DATABASE_URL' : 'PGHOST/PGDATABASE'; - console.log(`Database: PostgreSQL (${source})`); + console.log('Database: PostgreSQL (DATABASE_URL)'); }); } catch (error) { console.error('Database initialization error:', error); diff --git a/README.md b/README.md index 9412f93..8d33a26 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ CollabTable allows multiple users to collaboratively create and manage lists wit ## Prerequisites ### Server -- **Docker** and **Docker Compose** (recommended) +- **Docker** and a PostgreSQL instance (bring your own - this app does not run Postgres for you) - Or **Node.js 20+** for local development ### Android App @@ -65,17 +65,20 @@ CollabTable allows multiple users to collaboratively create and manage lists wit cp .env.example .env ``` -3. **Start the server (this also starts a bundled PostgreSQL container):** +3. **Build and run the server, pointing at your own PostgreSQL instance:** ```bash - docker-compose up -d + docker build -t collabtable-server . + docker run -d --name collabtable-server --restart unless-stopped \ + -p 3000:3000 \ + -e DATABASE_URL=postgres://user:password@host:5432/collabtable \ + collabtable-server ``` Server runs on `http://localhost:3000`. Open it in a browser to create the admin account, then generate an API token from the dashboard's Settings page for your sync clients/devices — see `CollabTableServer/README.md`'s "Authentication" section for details. -**Database:** PostgreSQL, provisioned automatically via the bundled `postgres` service in -`docker-compose.yml`. +**Database:** PostgreSQL, bring your own instance via `DATABASE_URL`. ### Android App @@ -124,7 +127,6 @@ CollabTable/ ├── webui/ # SvelteKit web dashboard (read-only database viewer) ├── data/ # SQLite database storage ├── Dockerfile - ├── docker-compose.yml └── README.md ``` @@ -340,34 +342,29 @@ The actual value for a specific field of an item: ```bash # Find and kill process using port 3000 lsof -ti:3000 | xargs kill -9 -# Or change port in docker-compose.yml or .env +# Or change the port mapping in your `docker run` command or .env ``` **Docker container won't start** ```bash # View logs -docker-compose logs -f +docker logs -f collabtable-server # Rebuild container -docker-compose down -docker-compose up -d --build +docker stop collabtable-server && docker rm collabtable-server +docker build -t collabtable-server . && docker run -d --name collabtable-server ... ``` **Database connection error (PostgreSQL)** -- Verify PostgreSQL is running -- Check connection details in `.env` -- Ensure database exists - -**Data not persisting after container restart** -- Verify Docker volume exists: `docker volume ls` -- Check volume mount in `docker-compose.yml` -- For SQLite, data is in `sqlite_data` volume +- Verify your PostgreSQL instance is running and reachable from the container +- Check `DATABASE_URL` in `.env` +- Ensure the database exists ### Getting Help - Check the [Android README](CollabTableAndroid/README.md) for app-specific details - Check the [Server README](CollabTableServer/README.md) for server-specific details -- Review Docker logs: `docker-compose logs -f` +- Review Docker logs: `docker logs -f collabtable-server` - Open an issue on GitHub with detailed error messages ## Synchronization @@ -440,30 +437,24 @@ Response: ### Data Persistence **PostgreSQL** -- Use standard PostgreSQL backup tools (`pg_dump`, `pg_restore`) -- The bundled `postgres` service's data is stored in a named Docker volume (`pg_data`), which - persists across container restarts +- Use standard PostgreSQL backup tools (`pg_dump`, `pg_restore`) against your own instance - + this app does not manage Postgres storage itself ### Docker Commands -**Start services:** +**Stop the container:** ```bash -docker-compose up -d -``` - -**Stop services:** -```bash -docker-compose down +docker stop collabtable-server ``` **View logs:** ```bash -docker-compose logs -f +docker logs -f collabtable-server ``` **Rebuild and restart:** ```bash -docker-compose up -d --build +docker build -t collabtable-server . && docker restart collabtable-server ``` **Check running containers:** @@ -474,7 +465,7 @@ docker ps ### Production Deployment **Environment Variables** -Configure these in `.env` or docker-compose.yml: +Configure these in `.env` or your `docker run` command: ```env PORT=3000 # Server port NODE_ENV=production # Environment