From 950f22df7046763b941991a9bfde0cfbb01eebd1 Mon Sep 17 00:00:00 2001 From: Refferic Date: Tue, 17 Nov 2026 22:16:18 +0100 Subject: [PATCH] Update to fit stack --- .env.example | 26 +++++------ .gitignore | 10 ++-- README.md | 82 ++++++++++++++++++++++----------- app/main.py | 5 +- app/templates/base.html | 8 ++-- compose.yaml | 62 +++++++++++++++++++++++++ traefik/certs/.gitkeep | 0 traefik/dynamic/middlewares.yml | 22 --------- traefik/dynamic/tls.yml | 10 ---- traefik/traefik.yml | 43 ----------------- 10 files changed, 141 insertions(+), 127 deletions(-) create mode 100644 compose.yaml delete mode 100644 traefik/certs/.gitkeep delete mode 100644 traefik/dynamic/middlewares.yml delete mode 100644 traefik/dynamic/tls.yml delete mode 100644 traefik/traefik.yml diff --git a/.env.example b/.env.example index 9d0285f..7dc82e3 100644 --- a/.env.example +++ b/.env.example @@ -1,26 +1,22 @@ -# --- Hostnames (point these at the Docker host, e.g. in your router/Pi-hole DNS) --- -FINANCE_HOST=finance.home.lan -AUTH_HOST=auth.home.lan +# Spendr, ~/docker/spendr/.env (copy from .env.example) +SPENDR_HOST=spendr.dv3.app TZ=Europe/Amsterdam -# --- Secrets: generate with openssl rand -base64 48 | tr -d '\n' --- -AUTHENTIK_SECRET_KEY=change-me -AUTHENTIK_DB_PASSWORD=change-me -FINANCE_DB_PASSWORD=change-me +# Password for Spendr's own Postgres database (only used between the two containers). +# Generate one with: openssl rand -base64 32 | tr -d '\n/+=' +SPENDR_DB_PASSWORD=change-me -# --- Versions --- -AUTHENTIK_TAG=2026.8.5 -TRAEFIK_TAG=v3.6 +# Optional: pin the DB volume name (e.g. when moving the stack to Komodo) +# PG_VOLUME=spendr-db -# --- Enable Banking (optional automatic sync for ING + Revolut) --- -# App ID from https://enablebanking.com/cp/applications ; put the private key at ./secrets/enablebanking.pem +# --- Enable Banking (optional automatic ING + Revolut sync) --- +# App ID from https://enablebanking.com/cp/applications ; private key goes in $SECRETS_DIR/enablebanking.pem EB_APP_ID= +# Host folder with enablebanking.pem (default ./secrets next to compose.yaml) +# SECRETS_DIR=/etc/komodo/secrets/spendr EB_SYNC_HOURS=6 # --- Prices --- # Optional free "Demo" key from CoinGecko for higher rate limits COINGECKO_API_KEY= PRICE_REFRESH_MINUTES=60 - -# Only if you enable the Let's Encrypt DNS challenge in traefik/traefik.yml -CF_DNS_API_TOKEN= diff --git a/.gitignore b/.gitignore index cbdce3f..2f051d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .env -secrets/*.pem -authentik/ -traefik/certs/* -!traefik/certs/.gitkeep +.env.* +!.env.example +secrets/* +!secrets/.gitkeep __pycache__/ +*.db +app/static/chart.umd.min.js diff --git a/README.md b/README.md index 9756066..b3e08a2 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# Finance: a self-hosted dashboard for ING, Revolut, investments, gold and debts +# Spendr: a self-hosted dashboard for ING, Revolut, investments, gold and debts ``` -browser ──https──▶ Traefik ──forward-auth──▶ authentik (login, MFA) +browser ──https──▶ Traefik (existing) ──authentik@docker──▶ Authentik (existing, auth.dv3.app) │ - └──▶ finance (FastAPI) ──▶ Postgres + └──▶ spendr (FastAPI) ──▶ spendr-db (Postgres) │ ├─ Yahoo Finance (stocks/ETFs, gold spot, FX) ├─ CoinGecko (crypto) @@ -23,30 +23,58 @@ browser ──https──▶ Traefik ──forward-auth──▶ authentik (logi | **Joint account** | Give the ING shared account a "your share %" (e.g. 50) and its spending is scaled in the totals. | | **Live numbers** | Every 30 s (and whenever you come back to the tab) the page fetches `/api/live`. Changed totals, prices and P/L count up or down and flash green or red, with no reload. *Refresh prices* runs in place. When new transactions arrive, a "new data · reload" hint appears so charts can be redrawn. | | **Dark mode** | Auto (follows the system), Light or Dark toggle in the sidebar, remembered per browser. | -| **Mobile** | Desktop-first layout. On phones the sidebar turns into a scrolling top bar and tables scroll sideways. | +| **Mobile** | Desktop-first layout. On phones there's a bottom tab bar and a slide-out menu, and tables scroll sideways. | -## Setup +## Setup (DV3 server) -1. **DNS**: point `finance.home.lan` and `auth.home.lan` (or your own names) at the Docker host, e.g. in Pi-hole, AdGuard or your router. -2. **Configure** +This stack uses the Traefik and Authentik you already run. It only adds two containers: `spendr` and `spendr-db`. + +```bash +mkdir -p ~/docker/spendr && cd ~/docker/spendr +# copy the contents of this folder here (compose.yaml, .env.example, app/, secrets/) +cp .env.example .env +nano .env # set SPENDR_DB_PASSWORD: openssl rand -base64 32 | tr -d '\n/+=' +sudo docker compose up -d --build +``` + +- **DNS / TLS**: `spendr.dv3.app` gets its certificate from the existing `letsencrypt` resolver, like your other services. +- **Login**: the router uses `authentik@docker`. Your domain-level forward-auth provider (cookie domain `dv3.app`) protects it with no extra setup. Optional: create an Authentik application `spendr` and bind a policy or group if only certain users should get in. +- **Why forward auth and not OIDC**: the app has no login of its own, so forward auth is the right fit (see your conventions). +- **Sign out**: the sidebar button goes to `https://auth.dv3.app/outpost.goauthentik.io/sign_out` (set via `SIGN_OUT_URL` in `compose.yaml`). +- **Komodo / Gitea**: see *Deploy with Gitea + Komodo* below (recommended). + +## Deploy with Gitea + Komodo (GitOps) + +Same flow as YoteInvest: push to Gitea, then Komodo redeploys. + +1. **Gitea**: create an empty repo `Mitchell/Spendr` on git.dv3.app (no README), then from the unzipped folder: ```bash - cp .env.example .env - # fill in hostnames + three secrets: openssl rand -base64 48 | tr -d '\n' - mkdir -p authentik/data authentik/certs authentik/custom-templates secrets - docker compose up -d --build + cd spendr + git init -b main && git add . && git commit -m "Spendr: initial version" + git remote add origin https://git.dv3.app/Mitchell/Spendr.git + git push -u origin main ``` -3. **authentik first run**: open `https://auth.home.lan/if/flow/initial-setup/` and create the admin user. Turn on TOTP or a passkey for it, because this app holds your bank data. -4. **Protect the app in authentik** (Admin → Applications → *Create with provider*): - - Provider type: **Proxy Provider** → **Forward auth (single application)** - - External host: `https://finance.home.lan` - - Then go to **Outposts → authentik Embedded Outpost → edit** and add the application. - - Optional: bind a policy or group so only you (and your partner, if you like) can open it. -5. Open `https://finance.home.lan`. You should be sent to the authentik login and then back to the app. + `.gitignore` keeps `.env` and the Enable Banking key out of the repo. +2. **Secrets folder on the NUC** (only needed for Enable Banking): + ```bash + sudo mkdir -p /etc/komodo/secrets/spendr + sudo cp enablebanking.pem /etc/komodo/secrets/spendr/ # later, once you have it + ``` +3. **Komodo → Stacks → New stack** `spendr`: + - Source: **Git Repo** → provider `git.dv3.app`, repo `Mitchell/Spendr`, branch `main`. The file is `compose.yaml`, so no File Paths needed. + - Turn on **Run Build** (the app image is built from `./app`), so a push also rebuilds the image. + - **Environment** (secrets live here, not in the repo): + ``` + SPENDR_DB_PASSWORD= finance-$(date +%F).sql.gz`. Back up `authentik-db` the same way. -- **Logs**: `docker compose logs -f finance` -- **Not exposed**: the app has no published port. It is only reachable through Traefik and authentik. Keep it that way. +- **Backup**: `sudo docker compose exec spendr-db pg_dump -U spendr spendr | gzip > spendr-$(date +%F).sql.gz`. Never run `docker compose down -v`, because it deletes the database volume. +- **Logs**: `sudo docker compose logs -f spendr` +- **Not exposed**: the app has no published port. It is only reachable through Traefik and Authentik. Keep it that way. - Prices refresh every `PRICE_REFRESH_MINUTES`. Yahoo's endpoint is unofficial. If a ticker fails, the holding shows "price error" and you can set a manual price. - The schema is created on start (`create_all`). Add Alembic if you start changing models. diff --git a/app/main.py b/app/main.py index 03efea4..e880ea7 100644 --- a/app/main.py +++ b/app/main.py @@ -70,7 +70,7 @@ async def lifespan(app: FastAPI): sched.shutdown(wait=False) -app = FastAPI(title="Finance", lifespan=lifespan, docs_url=None, redoc_url=None) +app = FastAPI(title="Spendr", lifespan=lifespan, docs_url=None, redoc_url=None) app.mount("/static", StaticFiles(directory=os.path.join(BASE_DIR, "static")), name="static") templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates")) @@ -100,7 +100,8 @@ def icon(name: str, cls: str = "") -> Markup: templates.env.filters.update(eur=eur, num=num, pct=pct) -templates.env.globals.update(icon=icon) +templates.env.globals.update(icon=icon, sign_out_url=os.getenv( + "SIGN_OUT_URL", "https://auth.dv3.app/outpost.goauthentik.io/sign_out")) templates.env.globals.update(today=date.today, rec_state=rec_mod.state, monthly_eq=rec_mod.monthly_equivalent) diff --git a/app/templates/base.html b/app/templates/base.html index 5d40c04..e131373 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -4,7 +4,7 @@ - {% block title %}Finance{% endblock %} · Finance + {% block title %}Finance{% endblock %} · Spendr @@ -19,7 +19,7 @@
@@ -53,7 +53,7 @@
-
Finance
+
Spendr
{% if msg %}
{{ icon('circle-alert') }}{{ msg }}
{% endif %} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..a24b2f5 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,62 @@ +# Spendr: personal finance dashboard. ~/docker/spendr/compose.yaml +# Uses the existing Traefik (external network "proxy") and Authentik forward auth at auth.dv3.app. + +services: + spendr: + build: ./app + container_name: spendr + restart: unless-stopped + environment: + DATABASE_URL: postgresql+psycopg://spendr:${SPENDR_DB_PASSWORD:?set SPENDR_DB_PASSWORD in .env}@spendr-db:5432/spendr + TZ: ${TZ:-Europe/Amsterdam} + PUBLIC_URL: https://${SPENDR_HOST:-spendr.dv3.app} + SIGN_OUT_URL: https://auth.dv3.app/outpost.goauthentik.io/sign_out + # Enable Banking (optional automatic ING/Revolut sync, see README) + EB_APP_ID: ${EB_APP_ID:-} + EB_KEY_PATH: /run/secrets/enablebanking.pem + EB_SYNC_HOURS: ${EB_SYNC_HOURS:-6} + # Prices + COINGECKO_API_KEY: ${COINGECKO_API_KEY:-} + PRICE_REFRESH_MINUTES: ${PRICE_REFRESH_MINUTES:-60} + volumes: + # Folder (not a single file) holding enablebanking.pem. With Komodo, point this at a host folder + # outside the cloned repo, e.g. SECRETS_DIR=/etc/komodo/secrets/spendr + - ${SECRETS_DIR:-./secrets}:/run/secrets:ro + depends_on: + spendr-db: + condition: service_healthy + networks: [proxy, internal] + labels: + traefik.enable: "true" + traefik.docker.network: proxy + traefik.http.routers.spendr.rule: Host(`${SPENDR_HOST:-spendr.dv3.app}`) + traefik.http.routers.spendr.entrypoints: websecure + traefik.http.routers.spendr.tls.certresolver: letsencrypt + traefik.http.routers.spendr.middlewares: secure-headers@docker,authentik@docker + traefik.http.services.spendr.loadbalancer.server.port: "8000" + + spendr-db: + image: postgres:16-alpine + container_name: spendr-db + restart: unless-stopped + environment: + POSTGRES_USER: spendr + POSTGRES_DB: spendr + POSTGRES_PASSWORD: ${SPENDR_DB_PASSWORD} + volumes: + - spendr-db:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -d spendr -U spendr"] + interval: 10s + timeout: 5s + retries: 5 + networks: [internal] + +networks: + proxy: + external: true + internal: + +volumes: + spendr-db: + name: ${PG_VOLUME:-spendr-db} diff --git a/traefik/certs/.gitkeep b/traefik/certs/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/traefik/dynamic/middlewares.yml b/traefik/dynamic/middlewares.yml deleted file mode 100644 index d822786..0000000 --- a/traefik/dynamic/middlewares.yml +++ /dev/null @@ -1,22 +0,0 @@ -http: - middlewares: - # Forward every request to authentik's embedded outpost; unauthenticated users get redirected to login. - authentik: - forwardAuth: - address: http://authentik-server:9000/outpost.goauthentik.io/auth/traefik - trustForwardHeader: true - authResponseHeaders: - - X-authentik-username - - X-authentik-groups - - X-authentik-email - - X-authentik-name - - X-authentik-uid - - secure-headers: - headers: - frameDeny: true - contentTypeNosniff: true - referrerPolicy: same-origin - stsSeconds: 31536000 - customResponseHeaders: - X-Robots-Tag: "noindex, nofollow" diff --git a/traefik/dynamic/tls.yml b/traefik/dynamic/tls.yml deleted file mode 100644 index 61f2acf..0000000 --- a/traefik/dynamic/tls.yml +++ /dev/null @@ -1,10 +0,0 @@ -# Optional: use your own certificate (e.g. made with mkcert for *.home.lan). -# Put the files in ./traefik/certs and uncomment. Without this (and without Let's Encrypt) -# Traefik serves its built-in self-signed certificate. -# -# tls: -# stores: -# default: -# defaultCertificate: -# certFile: /certs/local.crt -# keyFile: /certs/local.key diff --git a/traefik/traefik.yml b/traefik/traefik.yml deleted file mode 100644 index 0797a29..0000000 --- a/traefik/traefik.yml +++ /dev/null @@ -1,43 +0,0 @@ -# Traefik static configuration -api: - dashboard: false - -log: - level: INFO - -entryPoints: - web: - address: ":80" - http: - redirections: - entryPoint: - to: websecure - scheme: https - websecure: - address: ":443" - http: - tls: {} - # Uncomment to get real certificates via Let's Encrypt DNS challenge - # (works for LAN-only hosts as long as you own the domain): - # tls: - # certResolver: letsencrypt - # domains: - # - main: "example.com" - # sans: ["*.example.com"] - -providers: - docker: - exposedByDefault: false - network: finance_proxy - file: - directory: /etc/traefik/dynamic - watch: true - -# certificatesResolvers: -# letsencrypt: -# acme: -# email: you@example.com -# storage: /acme/acme.json -# dnsChallenge: -# provider: cloudflare # set CF_DNS_API_TOKEN in .env (or use another provider) -# resolvers: ["1.1.1.1:53"]