feat: store leads in PostgreSQL instead of SQLite
- server.js uses pg (pinned 8.23.1), reads DATABASE_URL, creates the leads table on start, fails fast if Postgres is unreachable - tests run against a real Postgres in a throwaway schema - CI runs the test image against a throwaway Postgres on DinD - k8s: Postgres 18 StatefulSet (Longhorn, 2Gi), init container wait, password secret, network policy app-to-postgres; app no longer needs a volume or Recreate - README: DBeaver via port-forward, CSV export via docs/export-leads.sql Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfmHGGiYUjNcvJoyzdf
This commit is contained in:
1 parent
5bb2a7343d
commit
f78bd635a2
15 files changed
+508
-166
No files matched your search
@@ -1,86 +1,85 @@
|
||||
# INS Landing Page
|
||||
|
||||
Single landing page for Infinity Networks Security with a lead form. Leads are stored in a SQLite file; there is no admin UI, leads are exported manually.
|
||||
Landing page for Infinity Networks Security's hotel technology services, with a lead form. Leads are stored in PostgreSQL; there is no admin UI, leads are read in DBeaver or exported as CSV.
|
||||
|
||||
- `public/index.html`: the page (content from infinitynetworks.com.my)
|
||||
- `server.js`: serves the page and `POST /api/leads`, Node 22 built-ins only, no npm dependencies
|
||||
- `public/index.html`: the page
|
||||
- `server.js`: serves the page and `POST /api/leads`; creates the `leads` table on start if it is missing
|
||||
- `docs/schema.sql`: the table, for reference (a test keeps it in sync with `server.js`)
|
||||
- `docs/export-leads.sql`: CSV export query
|
||||
|
||||
Node 22, one dependency: [`pg`](https://www.npmjs.com/package/pg) (pinned in `package-lock.json`).
|
||||
|
||||
## Run locally
|
||||
|
||||
Requires Node 22.13 or newer.
|
||||
|
||||
```bash
|
||||
npm start # http://localhost:3000, database at ./data/leads.db
|
||||
npm test
|
||||
docker run -d --name ins-pg -e POSTGRES_USER=ins -e POSTGRES_DB=ins -e POSTGRES_PASSWORD=devpass \
|
||||
-p 127.0.0.1:5432:5432 postgres:18
|
||||
npm ci
|
||||
DATABASE_URL=postgres://ins:devpass@127.0.0.1:5432/ins npm start # http://localhost:3000
|
||||
TEST_DATABASE_URL=postgres://ins:devpass@127.0.0.1:5432/ins npm test
|
||||
```
|
||||
|
||||
Environment: `PORT` (default `3000`), `DB_PATH` (default `./data/leads.db`).
|
||||
|
||||
## Docker
|
||||
|
||||
```bash
|
||||
docker build -t ins-landing-page .
|
||||
docker run -p 3000:3000 -v ins-leads:/data ins-landing-page
|
||||
```
|
||||
Environment: `DATABASE_URL` (required), `PORT` (default `3000`). Tests create their own schema in `TEST_DATABASE_URL` and drop it afterwards; they never touch an existing `leads` table.
|
||||
|
||||
## Kubernetes (microk8s)
|
||||
|
||||
`k8s/` deploys everything into namespace `ins-landing-page`, the same way as dynamic-qr:
|
||||
|
||||
- `40-app.yaml`: Longhorn PVC (1 Gi, ReadWriteOnce) mounted at `/data`, Service, and Deployment. The Deployment runs **1 replica** with `strategy: Recreate`, because SQLite has a single writer. It runs as uid 1000 with a read-only root filesystem.
|
||||
- `20-postgres.yaml`: Postgres 18 StatefulSet with a Longhorn PVC (2 Gi, ReadWriteOnce) and a headless Service `postgres`.
|
||||
- `40-app.yaml`: Service and Deployment. The app waits in an init container until Postgres answers, then creates the `leads` table if it is missing. It runs as uid 1000 with a read-only root filesystem.
|
||||
- `50-gateway.yaml`:
|
||||
- A Let's Encrypt Certificate and a Traefik Gateway for `ins-landing.containerverse.dev`.
|
||||
- HTTP redirects to HTTPS.
|
||||
- `POST /api/leads` goes through the `lead-rate-limit` middleware: 5 per minute per visitor, burst 5, keyed on the Cloudflare visitor IP.
|
||||
- `60-network-policy.yaml`: denies all ingress except Traefik to port 3000, and Traefik to cert-manager's HTTP-01 solver.
|
||||
- `60-network-policy.yaml`: denies all ingress except Traefik to the app (port 3000), the app to Postgres (5432), and Traefik to cert-manager's HTTP-01 solver.
|
||||
|
||||
Prerequisite: DNS for `ins-landing.containerverse.dev` points at the cluster through Cloudflare, like the other `containerverse.dev` hosts.
|
||||
|
||||
First install, on the microk8s host (`k` = `microk8s kubectl`):
|
||||
|
||||
k apply -f k8s/00-namespace.yaml
|
||||
k -n ins-landing-page create secret generic ins-landing-page-db \
|
||||
--from-literal=POSTGRES_PASSWORD="$(openssl rand -hex 24)"
|
||||
k -n ins-landing-page create secret docker-registry regcred \
|
||||
--docker-server=gitea.containerverse.dev --docker-username=<user> --docker-password=<token>
|
||||
k apply -f k8s/
|
||||
k -n ins-landing-page rollout status statefulset/postgres
|
||||
k -n ins-landing-page rollout status deployment/ins-landing-page
|
||||
|
||||
Updates: every push to `main` runs the tests and builds `gitea.containerverse.dev/solvway/ins-landing-page:ins-landing-page-prd-latest`. CI then runs `./krrd.sh ins-landing-page ins-landing-page` on the deploy host. Manifest changes are applied by hand (`k apply -f k8s/`).
|
||||
`k apply -f k8s/` does not recurse, so `k8s/examples/secret.yaml` (an example, never the real value) is not applied.
|
||||
|
||||
Updates: every push to `main` runs the tests against a throwaway Postgres, then builds `gitea.containerverse.dev/solvway/ins-landing-page:ins-landing-page-prd-latest`. CI then runs `./krrd.sh ins-landing-page ins-landing-page` on the deploy host. Manifest changes are applied by hand (`k apply -f k8s/`).
|
||||
|
||||
CI needs the same repository secrets as dynamic-qr: `PACKAGE_USER`, `PACKAGE_RW_KEY`, `DRONECICD_SSH_*`, `TELEGRAM_*` and, optionally, `MICROSOFT_TEAMS_WEBHOOK_URL`. They come from the `solvway` org once the repo is transferred there.
|
||||
|
||||
## Opening the database in DBeaver
|
||||
|
||||
Postgres is not exposed outside the cluster. Forward a local port to it, on the microk8s host:
|
||||
|
||||
microk8s kubectl -n ins-landing-page port-forward svc/postgres 15432:5432
|
||||
|
||||
If DBeaver runs on another machine, tunnel to that host first: `ssh -L 15432:127.0.0.1:15432 <user>@<microk8s-host>`.
|
||||
|
||||
In DBeaver: **New Database Connection → PostgreSQL**:
|
||||
|
||||
- Host `127.0.0.1`, port `15432`, database `ins`, user `ins`.
|
||||
- Password: `microk8s kubectl -n ins-landing-page get secret ins-landing-page-db -o jsonpath='{.data.POSTGRES_PASSWORD}' | base64 -d`.
|
||||
- Tick **Read-only connection** (connection settings → General) unless you mean to change data. This is the live database.
|
||||
|
||||
Close the port-forward when you're done.
|
||||
|
||||
## Exporting leads
|
||||
|
||||
To CSV, from the running pod:
|
||||
To CSV, with times in Malaysia time:
|
||||
|
||||
```bash
|
||||
microk8s kubectl -n ins-landing-page exec deploy/ins-landing-page -- node --disable-warning=ExperimentalWarning -e "
|
||||
const {DatabaseSync}=require('node:sqlite');
|
||||
const rows=new DatabaseSync(process.env.DB_PATH,{readOnly:true}).prepare('SELECT * FROM leads ORDER BY id').all();
|
||||
const q=v=>v==null?'':'\"'+String(v).replace(/^[=+\-@]/,\"'\$&\").replace(/\"/g,'\"\"')+'\"';
|
||||
if(rows.length)console.log(Object.keys(rows[0]).join(','));
|
||||
for(const r of rows)console.log(Object.values(r).map(q).join(','));
|
||||
" > leads.csv
|
||||
microk8s kubectl -n ins-landing-page exec -i postgres-0 -- psql -q -U ins -d ins -f - < docs/export-leads.sql > leads.csv
|
||||
```
|
||||
|
||||
### Opening the database in DBeaver
|
||||
Read-only. Text cells starting with `= + - @` get a leading `'` so Excel can't run form input as a formula; phone numbers starting with `+` show it too.
|
||||
|
||||
SQLite is a file, not a network server, so DBeaver cannot connect to the pod. Take a consistent snapshot and open that copy:
|
||||
Leads are personal data (PDPA): keep exports out of shared drives and delete them when you're done.
|
||||
|
||||
```bash
|
||||
NS=ins-landing-page
|
||||
POD=$(microk8s kubectl -n $NS get pod -l app=ins-landing-page -o name | head -1 | cut -d/ -f2)
|
||||
# VACUUM INTO writes one complete file, including leads still in the WAL
|
||||
microk8s kubectl -n $NS exec $POD -- node --disable-warning=ExperimentalWarning -e \
|
||||
"new (require('node:sqlite').DatabaseSync)(process.env.DB_PATH).exec(\"VACUUM INTO '/data/snapshot.db'\")"
|
||||
microk8s kubectl -n $NS cp $POD:/data/snapshot.db ./leads-snapshot.db
|
||||
microk8s kubectl -n $NS exec $POD -- rm /data/snapshot.db
|
||||
```
|
||||
|
||||
In DBeaver: **New Database Connection → SQLite →** point **Path** at `leads-snapshot.db`. Query and export freely; it's your own copy.
|
||||
|
||||
Edits made in DBeaver stay in the copy. **Never copy a file back into the pod while the app is running**: it would overwrite leads submitted in the meantime. To fix or delete live rows, run the SQL in the pod (`exec ... node -e "..."`) and keep a snapshot first.
|
||||
|
||||
Leads are personal data (PDPA): delete snapshots and CSVs when you're done.
|
||||
|
||||
Schema: `docs/schema.sql`. Columns: `id, created_at (UTC), name, email, phone, company, service, referrer, utm_source, utm_medium, utm_campaign`.
|
||||
## Backups
|
||||
|
||||
The leads live only on the Postgres volume, and these manifests set up no backup. Confirm with the cluster manager that Longhorn snapshots or backups cover the `ins-landing-page-postgres` volume, or schedule a `pg_dump`.
|
||||
Reference in new issue
Block a user