feat: initialize CollabTableServer with models, sync, and notification handling
- Add go.mod and go.sum for dependency management - Implement models for List, Field, Item, ItemValue, SyncRequest, SyncResponse, and NotificationEvent - Create notification handling functions including enqueueNotification and ignoreNotificationError - Implement sync functionality to handle data synchronization between client and server - Add web data handling and notification polling endpoints - Include tests for timestamp normalization and sync logic - Embed web assets for serving the frontend application
This commit is contained in:
1 parent
e12940fef0
commit
cac2faae2e
41 files changed
+2535
-5502
No files matched your search
+116
-245
@@ -1,245 +1,116 @@
|
||||
# CollabTable Server
|
||||
|
||||
A collaborative table management server built with Node.js, Express, TypeScript, and PostgreSQL.
|
||||
|
||||
## Features
|
||||
|
||||
- RESTful API for managing lists, fields, items, and values
|
||||
- Real-time synchronization via WebSocket (primary) with HTTP fallback
|
||||
- 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 (bring your own PostgreSQL instance)
|
||||
- TypeScript for type safety
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- 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
|
||||
|
||||
1. Clone the repository
|
||||
2. Navigate to the server directory:
|
||||
```bash
|
||||
cd CollabTableServer
|
||||
```
|
||||
|
||||
3. Build the image:
|
||||
```bash
|
||||
docker build -t collabtable-server .
|
||||
```
|
||||
|
||||
4. Run it, pointing at your own Postgres instance:
|
||||
```bash
|
||||
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
|
||||
admin account, then generate an API token from the dashboard for your sync clients/devices.
|
||||
|
||||
## Local Development
|
||||
|
||||
1. Install dependencies:
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
2. Create a `.env` file:
|
||||
```bash
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
3. Point it at a PostgreSQL instance in `.env`:
|
||||
```
|
||||
DATABASE_URL=postgres://user:password@localhost:5432/collabtable
|
||||
|
||||
# Set to "true" once served over HTTPS
|
||||
COOKIE_SECURE=false
|
||||
```
|
||||
|
||||
4. Start the development server:
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
## API Endpoints
|
||||
|
||||
### Lists
|
||||
- `GET /api/lists` - Get all lists
|
||||
- `GET /api/lists/:id` - Get a specific list
|
||||
- `POST /api/lists` - Create a new list
|
||||
- `PUT /api/lists/:id` - Update a list
|
||||
- `DELETE /api/lists/:id` - Delete a list (soft delete)
|
||||
|
||||
### Fields
|
||||
- `GET /api/fields/list/:listId` - Get all fields for a list
|
||||
- `POST /api/fields` - Create a new field
|
||||
- `PUT /api/fields/:id` - Update a field
|
||||
- `DELETE /api/fields/:id` - Delete a field (soft delete)
|
||||
|
||||
### Items
|
||||
- `GET /api/items/list/:listId` - Get all items for a list
|
||||
- `GET /api/items/:itemId/values` - Get all values for an item
|
||||
- `POST /api/items` - Create a new item
|
||||
- `POST /api/items/values` - Create/update an item value
|
||||
- `PUT /api/items/:id` - Update an item
|
||||
- `DELETE /api/items/:id` - Delete an item (soft delete)
|
||||
|
||||
### Sync
|
||||
- `POST /api/sync` - Synchronize data between client and server
|
||||
|
||||
### WebSocket
|
||||
- `GET /api/ws` - WebSocket endpoint. Send JSON messages:
|
||||
- `{"type":"ping"}` → `{"type":"pong"}`
|
||||
- `{"type":"sync", "id":"<uuid>", "payload": { ...SyncRequest }}` → `{"type":"syncResponse", "id":"<uuid>", "payload": { ...SyncResponse }}`
|
||||
|
||||
Authentication: include header `Authorization: Bearer <api-token>` on sync/collaboration API requests, using the API token generated from the web dashboard's Settings page (see "Authentication" below).
|
||||
|
||||
### Health Check
|
||||
- `GET /health` - Check server health
|
||||
|
||||
## Data Models
|
||||
|
||||
### List
|
||||
```typescript
|
||||
{
|
||||
id: string;
|
||||
name: string;
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
isDeleted: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
### Field
|
||||
```typescript
|
||||
{
|
||||
id: string;
|
||||
listId: string;
|
||||
name: string;
|
||||
order: number;
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
isDeleted: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
### Item
|
||||
```typescript
|
||||
{
|
||||
id: string;
|
||||
listId: string;
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
isDeleted: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
### ItemValue
|
||||
```typescript
|
||||
{
|
||||
id: string;
|
||||
itemId: string;
|
||||
fieldId: string;
|
||||
value: string;
|
||||
updatedAt: number;
|
||||
}
|
||||
```
|
||||
|
||||
## Sync Protocol
|
||||
|
||||
The sync endpoint accepts a POST request with the following structure:
|
||||
|
||||
```json
|
||||
{
|
||||
"lastSyncTimestamp": 1234567890,
|
||||
"lists": [...],
|
||||
"fields": [...],
|
||||
"items": [...],
|
||||
"itemValues": [...]
|
||||
}
|
||||
```
|
||||
|
||||
The server responds with:
|
||||
|
||||
```json
|
||||
{
|
||||
"lists": [...],
|
||||
"fields": [...],
|
||||
"items": [...],
|
||||
"itemValues": [...],
|
||||
"serverTimestamp": 1234567890
|
||||
}
|
||||
```
|
||||
|
||||
The sync process:
|
||||
1. Client sends all local changes since the last sync
|
||||
2. Server saves the client's changes
|
||||
3. Server returns all changes made on the server since the client's last sync
|
||||
4. Client applies the server's changes locally
|
||||
|
||||
## Building for Production
|
||||
|
||||
Build the TypeScript code:
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
Start the production server:
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
## Docker Commands
|
||||
|
||||
Stop the container:
|
||||
```bash
|
||||
docker stop collabtable-server
|
||||
```
|
||||
|
||||
View logs:
|
||||
```bash
|
||||
docker logs -f collabtable-server
|
||||
```
|
||||
|
||||
Rebuild and restart:
|
||||
```bash
|
||||
docker build -t collabtable-server . && docker restart collabtable-server
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
|
||||
- `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 (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`)
|
||||
|
||||
## Authentication
|
||||
|
||||
There is a single admin account for the web dashboard (username + password, bcrypt-hashed,
|
||||
session cookie - no accounts beyond the one admin). The first time you open the dashboard
|
||||
you're walked through creating it; there is no default/seeded account.
|
||||
|
||||
Sync clients and devices (the mobile app, or anything hitting the `/api/lists` /
|
||||
`/api/fields` / `/api/items` / `/api/sync` / `/api/notifications` endpoints) authenticate
|
||||
with a separate long-lived API token instead - generate or regenerate it from the
|
||||
dashboard's Settings page, then set `Authorization: Bearer <token>` on those requests. This
|
||||
replaces the old optional `SERVER_PASSWORD` scheme, which silently disabled auth entirely
|
||||
when left unset.
|
||||
|
||||
## Data Persistence
|
||||
|
||||
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
|
||||
|
||||
MIT
|
||||
# CollabTable Server
|
||||
|
||||
The CollabTable backend is a small Go `net/http` service backed by PostgreSQL. It serves the
|
||||
Svelte dashboard and the Android synchronization API from one container.
|
||||
|
||||
## Features
|
||||
|
||||
- REST API for lists, fields, items, and item values
|
||||
- Transactional HTTP synchronization with soft-delete tombstones
|
||||
- Notification polling for background refreshes
|
||||
- Single-admin browser session with bcrypt-hashed credentials
|
||||
- Long-lived bearer API token for Android and other sync clients
|
||||
- Embedded SvelteKit dashboard
|
||||
- Idempotent PostgreSQL schema initialization
|
||||
|
||||
## Quick start with Docker
|
||||
|
||||
```bash
|
||||
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 \
|
||||
-e COOKIE_SECURE=false \
|
||||
collabtable-server
|
||||
```
|
||||
|
||||
Open `http://localhost:3000` to create the administrator account. Generate an API token from
|
||||
the dashboard's Settings menu and enter it in the Android app.
|
||||
|
||||
## Local development
|
||||
|
||||
The backend requires Go 1.26+ and PostgreSQL. The dashboard requires Node.js 22+.
|
||||
|
||||
```bash
|
||||
cp .env.example .env
|
||||
go test ./...
|
||||
go run .
|
||||
```
|
||||
|
||||
To build the dashboard for the embedded production binary:
|
||||
|
||||
```bash
|
||||
cd webui
|
||||
npm ci
|
||||
npm run check
|
||||
npm run build
|
||||
cd ..
|
||||
Copy-Item -Recurse -Force webui/build web
|
||||
go build -o collabtable-server .
|
||||
```
|
||||
|
||||
The Dockerfile performs those dashboard and Go build steps automatically.
|
||||
|
||||
## API
|
||||
|
||||
All Android synchronization routes are prefixed with `/api/` and require:
|
||||
|
||||
```text
|
||||
Authorization: Bearer <api-token>
|
||||
```
|
||||
|
||||
The Android app also sends `x-device-id`, which is used to avoid showing a device its own
|
||||
notification events.
|
||||
|
||||
### Authentication
|
||||
|
||||
- `GET /api/auth/status`
|
||||
- `POST /api/auth/setup`
|
||||
- `POST /api/auth/login`
|
||||
- `POST /api/auth/logout`
|
||||
- `POST /api/auth/credentials`
|
||||
- `GET /api/auth/token/status`
|
||||
- `POST /api/auth/token/regenerate`
|
||||
|
||||
Browser authentication uses the `collabtable_session` HTTP-only cookie. There is one admin
|
||||
account per database. API tokens are hashed at rest and shown only when generated.
|
||||
|
||||
### Data
|
||||
|
||||
- `GET|POST /api/lists`
|
||||
- `GET|PUT|DELETE /api/lists/:id`
|
||||
- `GET /api/fields/list/:listId`
|
||||
- `POST /api/fields`
|
||||
- `PUT|DELETE /api/fields/:id`
|
||||
- `GET /api/items/list/:listId`
|
||||
- `GET /api/items/:itemId/values`
|
||||
- `POST /api/items`
|
||||
- `POST /api/items/values`
|
||||
- `PUT|DELETE /api/items/:id`
|
||||
- `POST /api/sync`
|
||||
- `GET /api/notifications/poll?since=<timestamp-ms>`
|
||||
|
||||
### Dashboard and health
|
||||
|
||||
- `GET /health` is public.
|
||||
- `GET /web/data` requires the admin session.
|
||||
- `GET /web/info` requires the admin session.
|
||||
- `/`, `/about`, and `/lists/:id` serve the embedded dashboard SPA.
|
||||
|
||||
## Environment variables
|
||||
|
||||
- `PORT` — HTTP port, default `3000`
|
||||
- `APP_VERSION` — optional version displayed in the dashboard, default `dev`
|
||||
- `COOKIE_SECURE` — set to `true` when served over HTTPS
|
||||
- `DATABASE_URL` — PostgreSQL connection string, required
|
||||
- `DB_INIT_RETRY_INTERVAL_MS` — startup retry delay, default `5000`
|
||||
- `DB_INIT_RETRY_MAX_ATTEMPTS` — maximum startup attempts, `0` retries forever
|
||||
- `SYNC_MAX_FUTURE_SKEW_MS` — allowed client timestamp skew, default `0`
|
||||
- `NOTIF_RETENTION_MS` — notification retention period, default seven days
|
||||
|
||||
The Go server initializes the existing CollabTable tables in place, so switching from the
|
||||
previous server does not require a data export or schema conversion.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
Reference in new issue
Block a user