diff --git a/.env.example b/.env.example index fb4ecee..13a2788 100644 --- a/.env.example +++ b/.env.example @@ -37,11 +37,12 @@ # ============================================================================= # --- Postgres ---------------------------------------------------------------- -# One instance, three databases: POSTGRES_DB for the Go backend, -# POSTGRES_GITEA_DB for Gitea, POSTGRES_JOBS_DB for the job tracker (with its -# own role). Only POSTGRES_DB is auto-created on first init; the other two must -# be created by hand on a fresh volume (the job tracker's README has the -# commands). +# One instance, four databases: POSTGRES_DB for the Go backend, +# POSTGRES_GITEA_DB for Gitea, POSTGRES_JOBS_DB for the job tracker and +# POSTGRES_TRACKER_DB for the activity tracker (each of the last two with its +# own role). Only POSTGRES_DB is auto-created on first init; the others must +# be created by hand on a fresh volume (the job-tracker and ccp READMEs have +# the commands). # POSTGRES_HOST doubles as the db container's name. POSTGRES_USER= POSTGRES_PASSWORD= @@ -51,6 +52,9 @@ POSTGRES_HOST=db POSTGRES_JOBS_DB=jobs POSTGRES_JOBS_USER=jobs POSTGRES_JOBS_PASSWORD= +POSTGRES_TRACKER_DB=tracker +POSTGRES_TRACKER_USER=tracker +POSTGRES_TRACKER_PASSWORD= # --- Gitea ------------------------------------------------------------------- # GITEA_HOST is the container name AND the nginx upstream host. @@ -213,5 +217,13 @@ PYTHON_PORT=8000 # `docker compose pull jobs && docker compose up -d jobs`. JOBS_IMAGE_TAG= +# --- Activity tracker (/tracker/) -------------------------------------------- +# An image built and published by the ccp repo's CI. Blank runs :latest; set a +# commit sha to pin. Changing it needs +# `docker compose pull activity-tracker && docker compose up -d activity-tracker`. +ACTIVITY_TRACKER_IMAGE_TAG= +# IANA timezone that decides where a "day" starts in daily summaries. Blank is UTC. +TRACKER_TZ=Europe/London + # TfL Unified API key (500 req/min); used by the Python hotel finder TFL_APP_KEY= diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index edec32b..fb758fb 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -57,6 +57,7 @@ backend icecast2 gitea sb sb-pub python | | | silverbullet-git | n/a | n/a | none (timer loop) | no | | python (FastAPI) | `${PYTHON_HOST}` | `${PYTHON_PORT}` | `/py/`, docs at `/py/docs` | no | | jobs (job tracker, published image) | `jobs` | 8080 | `/jobs/` (admin-gated by the app itself) | no | +| activity-tracker (published image, adamf/ccp) | `activity-tracker` | 8080 | `/tracker/`, prefix stripped (per-device bearer keys) | no | | icecast2 + liquidsoap | `${ICECAST_HOST}` | `${ICECAST_PORT}` | `/radio/` | **harbor port only** | | gitea | `${GITEA_HOST}` | 3000, 2222 | `/gitea/` | **3000, 2222** | | vue | `vue` | 5173 (dev only) | `/` (dev only) | no | diff --git a/backend/services/env_manager_test.go b/backend/services/env_manager_test.go index b61f1a1..0bb8ed6 100644 --- a/backend/services/env_manager_test.go +++ b/backend/services/env_manager_test.go @@ -110,12 +110,12 @@ func composeServiceEnv(t *testing.T, service string) []string { // explicitEnvServices are the containers given an explicit `environment:` // list instead of the whole .env, so that they never hold the database -// password or the JWT secret: the Python service, and the job tracker (an -// image from its own repo, which gets its own database role besides). The +// password or the JWT secret: the Python service, and the job and activity +// trackers (images from their own repos, each with its own database role). The // cost is a third place a new variable for one of them has to be added — the // registry, .env.example and that list — and forgetting the third is silent: // the variable is simply empty inside the container. -var explicitEnvServices = []string{"python", "jobs"} +var explicitEnvServices = []string{"python", "jobs", "activity-tracker"} // So the registry's claim "this service reads this" and the compose list must // agree, in both directions: diff --git a/backend/services/env_registry.json b/backend/services/env_registry.json index a8aa761..4f7de88 100644 --- a/backend/services/env_registry.json +++ b/backend/services/env_registry.json @@ -45,6 +45,7 @@ {"name": "BACKEND_ENDPOINT", "group": "Infrastructure", "purpose": "URL prefix nginx mounts the API at", "kind": "text", "editable": false, "lockedReason": "The Vue app assumes /api."}, {"name": "PYTHON_HOST", "group": "Infrastructure", "purpose": "Python container name and nginx upstream", "kind": "text", "editable": false, "lockedReason": "Renames a container and repoints nginx."}, {"name": "PYTHON_PORT", "group": "Infrastructure", "purpose": "Python listen port", "kind": "text", "editable": false, "lockedReason": "Renames a container or repoints nginx."}, + {"name": "ACTIVITY_TRACKER_IMAGE_TAG", "group": "Infrastructure", "purpose": "Activity tracker image tag to run (default latest)", "kind": "text", "editable": false, "lockedReason": "Changing it needs a pull: docker compose pull activity-tracker && docker compose up -d activity-tracker."}, {"name": "JOBS_IMAGE_TAG", "group": "Infrastructure", "purpose": "Job tracker image tag to run (default latest)", "kind": "text", "editable": false, "lockedReason": "Changing it needs a pull: docker compose pull jobs && docker compose up -d jobs."}, {"name": "ICECAST_HOST", "group": "Infrastructure", "purpose": "Icecast container name and nginx upstream", "kind": "text", "editable": false, "lockedReason": "Renames a container and repoints nginx."}, {"name": "ICECAST_PORT", "group": "Infrastructure", "purpose": "Icecast listen port", "kind": "text", "editable": false, "lockedReason": "Renames a container or repoints nginx."}, @@ -65,6 +66,10 @@ {"name": "POSTGRES_JOBS_DB", "group": "Database", "purpose": "Job tracker database name", "kind": "text", "editable": false, "lockedReason": "Created by hand on first install; see the job-tracker README."}, {"name": "POSTGRES_JOBS_USER", "group": "Database", "purpose": "Job tracker database role", "kind": "text", "editable": false, "lockedReason": "Created by hand on first install; changing it here locks the tracker out."}, {"name": "POSTGRES_JOBS_PASSWORD", "group": "Database", "purpose": "Job tracker database password", "kind": "secret", "editable": false, "lockedReason": "Set on the role by hand; changing it here locks the tracker out."}, + {"name": "POSTGRES_TRACKER_DB", "group": "Database", "purpose": "Activity tracker database name", "kind": "text", "editable": false, "lockedReason": "Created by hand on first install; see the ccp README."}, + {"name": "POSTGRES_TRACKER_USER", "group": "Database", "purpose": "Activity tracker database role", "kind": "text", "editable": false, "lockedReason": "Created by hand on first install; changing it here locks the activity tracker out."}, + {"name": "POSTGRES_TRACKER_PASSWORD", "group": "Database", "purpose": "Activity tracker database password", "kind": "secret", "editable": false, "lockedReason": "Set on the role by hand; changing it here locks the activity tracker out."}, + {"name": "TRACKER_TZ", "group": "Activity tracker", "purpose": "Timezone where the activity tracker's day starts, e.g. Europe/London (blank is UTC)", "kind": "text", "editable": true, "services": ["activity-tracker"]}, {"name": "GITEA_HOST", "group": "Gitea", "purpose": "Gitea container name and nginx upstream", "kind": "text", "editable": false, "lockedReason": "Renames a container and repoints nginx."}, {"name": "GITEA_PORT", "group": "Gitea", "purpose": "Gitea HTTP port", "kind": "text", "editable": false, "lockedReason": "Renames a container or repoints nginx."}, diff --git a/docker-compose.yml b/docker-compose.yml index 94c4ce2..6e40a31 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -115,6 +115,7 @@ services: - silverbullet-public - python - jobs + - activity-tracker networks: - app-network ports: @@ -473,6 +474,29 @@ services: volumes: - ./jobs-data:/data + # The activity tracker (time spent per app, site, walk...), served at + # /tracker/ with the prefix stripped. Built and published by its own repo + # (adamf/ccp on this Gitea), whose CI pulls :latest into this service on + # every push to main; pin ACTIVITY_TRACKER_IMAGE_TAG to hold a version. + # + # Same shape as jobs: an explicit `environment:` list and a database role of + # its own, created by hand once (the ccp README has the command). It applies + # its own migrations on start. Collectors authenticate with per-device + # bearer keys made by `docker exec activity-tracker tracker apikey create`; + # nothing here touches the site's login. No state outside Postgres. + activity-tracker: + image: adam-french.co.uk/adamf/activity-tracker:${ACTIVITY_TRACKER_IMAGE_TAG:-latest} + container_name: activity-tracker + restart: always + depends_on: + db: + condition: service_healthy + networks: + - app-network + environment: + - DATABASE_URL=postgres://${POSTGRES_TRACKER_USER:-tracker}:${POSTGRES_TRACKER_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_TRACKER_DB:-tracker}?sslmode=disable + - TRACKER_TZ=${TRACKER_TZ:-} + # Self-hosted git, served at /gitea/ and on port 3000 directly. Also the CI # server: .gitea/workflows/deploy.yaml redeploys this whole stack on push to # main, executed by the act_runner that lives outside docker (gitea-runner/). diff --git a/nginx/nginx_routes.conf.template b/nginx/nginx_routes.conf.template index 9e74dc2..44dd8e7 100644 --- a/nginx/nginx_routes.conf.template +++ b/nginx/nginx_routes.conf.template @@ -77,6 +77,8 @@ # The job tracker's container name is fixed in docker-compose.yml, so # it needs no variable (and no ENVSUBST_VARS entry). set $upstream_jobs http://jobs:8080; + # Likewise the activity tracker's. + set $upstream_tracker http://activity-tracker:8080; # ---- Backend API ----------------------------------------------------- # Two patterns repeat through every proxied location below: @@ -363,6 +365,28 @@ proxy_set_header X-Forwarded-Proto $scheme; } + # ---- Activity tracker (/tracker/) ---- + # Its own container (the adamf/ccp repo): an ingest API for device + # collectors, which send batches of events with a per-device bearer + # key. The app does its own auth, so no auth_request. Unlike /jobs/ + # the prefix IS stripped: the server serves /api/v1/... and /readyz + # from /. Collectors must use the www host: the apex 301 would turn + # their POSTs into GETs. Health Auto Export bodies can be several MB; + # the server-wide 50M client_max_body_size covers them. + location = /tracker { + return 301 /tracker/; + } + + location ^~ /tracker/ { + limit_req zone=api burst=20 nodelay; + rewrite ^/tracker/(.*)$ /$1 break; + proxy_pass $upstream_tracker; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + # Where the tracker used to be. The SPA routes were client-side, so # these only catch bookmarks and typed URLs. The userscript's old # @updateURL lands on the third, which is how installed copies find