- Python 96.9%
- Dockerfile 3.1%
| .forgejo/workflows | ||
| app | ||
| k8s | ||
| tests | ||
| .dockerignore | ||
| .env.example | ||
| .gitignore | ||
| .python-version | ||
| AI-GROUNDING.md | ||
| Dockerfile | ||
| pyproject.toml | ||
| README.md | ||
| todo.md | ||
| uv.lock | ||
demo-app
this is a demonstration app for the digital fieldlab seminar
Run
uv sync
uv run uvicorn app.main:app --reload
Health check: curl localhost:8000/health. The root / returns app_name and a short message that points to /health.
The response has five parts:
app_name: a unique instance name generated on startup (APP_NAMEplus a random suffix, e.g.demo-app-GERTE1CV54), so you can see which instance answered behind a load balancer. It is also logged on startup.status:errorif any database component iserror, otherwiseok.components:localdbandremotedb. The inactive one isNA, the active one isokorerror.records: per active database, the number of demo records (count) and the records themselves (items). The inactive one isNA.config: the active settings. The password inREMOTE_DB_URLis masked as***.
With DB_MODE=0 both components are NA.
/health returns HTTP 200 when status is ok and HTTP 503 when it is error; the body is the same in both cases. Kubernetes uses it as readiness probe, so a pod without a working database gets no traffic. /livez always returns 200 while the process runs and is the liveness probe, so a database outage does not restart the pods.
On startup the app tries to reach the database DB_CONNECT_ATTEMPTS times, waiting DB_CONNECT_DELAY seconds and doubling the wait each time (at most 10 s). If that still fails, every /health call tries again, so the app recovers on its own when the database comes back.
Configuration
| Variable | Default | Purpose |
|---|---|---|
APP_NAME |
demo-app |
Prefix for the instance name. A random 10-character suffix is added on every startup, so each instance or pod gets a unique name |
DB_MODE |
1 |
0 = no database, 1 = local SQLite, 2 = remote PostgreSQL |
LOCAL_DB_PATH |
local.db |
Path to the SQLite file (mode 1) |
REMOTE_DB_URL |
- | PostgreSQL connection string (mode 2, contains credentials, keep it out of code) |
DB_CONNECT_ATTEMPTS |
10 |
Connection attempts on startup before giving up (then /health keeps retrying) |
DB_CONNECT_DELAY |
1.0 |
First wait in seconds between attempts, doubled each time, at most 10 s |
Copy .env.example to .env to set these values. The app does not load .env on its own, so run it with uv run --env-file .env uvicorn app.main:app --reload.
On startup each enabled database is created if needed and seeded with 3 demo records when empty.
Tests
Run from the demo-app folder, not from the parent fieldlab folder:
cd demo-app
uv run pytest
Docker
Build the image from the demo-app folder:
docker build -t demo-app:latest .
Run it with the local SQLite database (mode 1) and expose port 8000:
docker run --rm -p 8000:8000 -e DB_MODE=1 -e LOCAL_DB_PATH=/tmp/local.db demo-app:latest
Check it from another terminal: curl localhost:8000/health
For DB_MODE=2, pass the remote URL as an environment variable and keep it out of the image:
docker run --rm -p 8000:8000 -e DB_MODE=2 -e REMOTE_DB_URL="postgresql://user:pass@host:5432/db" demo-app:latest
The image is built and tested in Docker Desktop: /health returns 200 with the seeded records, and the console shows the startup logs.
To test load balancing, run several instances and check which one answers. Each instance gets its own random name on startup:
docker run --rm -d -p 8001:8000 demo-app:latest
docker run --rm -d -p 8002:8000 demo-app:latest
On Kubernetes, raise replicas in the Deployment; every pod reports its own app_name.
If docker is not available in your WSL distro, enable WSL integration in Docker Desktop, or build with the Windows client (docker.exe).
Deploy
Dockerfilebuilds the app image. It runs as non-root user10001and pins uv0.11.15..dockerignorekeeps.env, local databases, tests,k8s/and.forgejo/out of the image.k8s/base/is a Kustomize base with a ServiceAccount, ConfigMap, Deployment and a LoadBalancer Service. The placeholderREPLACE_IMAGE_NAME:REPLACE_IMAGE_TAGis filled in by the deploy workflow.- The default is
DB_MODE=1(SQLite in/tmp), with one replica. Each pod has its own database, and its ownapp_name. - For
DB_MODE=2, storeREMOTE_DB_URLin a Kubernetes Secret and inject it into the Deployment. Do not put it in the manifest.
CI/CD (Forgejo Actions)
.forgejo/workflows/ci-deploy.yml runs the tests and a manifest check, builds and pushes the image tagged with the commit SHA, and deploys to Fundament. A push to main deploys to development. Production needs a manual run with environment=production and confirm_production=yes.
Create these in Forgejo under Settings → Actions. Never put the values in Git:
| Name | Type | Level |
|---|---|---|
REGISTRY_USER, REGISTRY_TOKEN |
Secret | Organisation team-flevoland |
FUNDAMENT_API_KEY_DEV (and _PROD) |
Secret | Repository |
IMAGE_NAME (demo-app), FUNDAMENT_ORG_ID, FUNDAMENT_CLUSTER_ID_DEV, K8S_NAMESPACE_DEV (and _PROD) |
Variable | Repository |
The API key must be made for this pipeline only, with an expiry, by a user who is project admin of this project (not organisation admin). The kubeconfig holds no credential: the workflow creates it from the key with functl.
After the first deploy: kubectl get service demo-app -n <namespace> shows the EXTERNAL-IP. Open http://<EXTERNAL-IP>/ and /health. Create a DNS A record yourself if you want a hostname.
The workflow has not run on the Fundament runner yet. See the assumptions at the top of the file.