flask boilerplate
  • Python 63.1%
  • HTML 24.4%
  • Shell 9.8%
  • Dockerfile 1.4%
  • Mako 0.6%
  • Other 0.7%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
dsh b799374e11
All checks were successful
CI / test (push) Successful in 30s
chore: add Apache-2.0 license
2026-11-26 07:50:39 +00:00
.forgejo/workflows feat: containerise flango and add the production deploy pipeline 2026-11-15 19:40:23 +00:00
auth feat: add CSRF protection and session-based auth for admin 2026-10-21 11:04:33 -04:00
deploy feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
main feat: add CSRF protection and session-based auth for admin 2026-10-21 11:04:33 -04:00
migrations feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
static feat: add progressive-enhancement HTMX task interactions 2026-10-21 11:04:33 -04:00
stats feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
templates feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
tests feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
.dockerignore feat: containerise flango and add the production deploy pipeline 2026-11-15 19:40:23 +00:00
.env.example feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
.gitignore docs: turn AGENTS.md into a map and split detail into AGENTS.d 2026-11-15 18:53:17 +00:00
.python-version initial commit 2026-08-06 23:35:09 +02:00
admin.py feat: add CSRF protection and session-based auth for admin 2026-10-21 11:04:33 -04:00
AGENTS.md feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
app.py feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
config.py feat(stats): first-party visit statistics in the admin console 2026-11-16 10:32:01 +00:00
docker-compose.yml feat: containerise flango and add the production deploy pipeline 2026-11-15 19:40:23 +00:00
docker-entrypoint.sh feat: containerise flango and add the production deploy pipeline 2026-11-15 19:40:23 +00:00
Dockerfile feat: containerise flango and add the production deploy pipeline 2026-11-15 19:40:23 +00:00
extensions.py feat: add CSRF protection and session-based auth for admin 2026-10-21 11:04:33 -04:00
LICENSE chore: add Apache-2.0 license 2026-11-26 07:50:39 +00:00
notes.txt . 2026-08-07 05:22:35 +02:00
pyproject.toml chore: add Apache-2.0 license 2026-11-26 07:50:39 +00:00
README.md chore: add Apache-2.0 license 2026-11-26 07:50:39 +00:00
uv.lock feat: containerise flango and add the production deploy pipeline 2026-11-15 19:40:23 +00:00
wsgi.py feat: containerise flango and add the production deploy pipeline 2026-11-15 19:40:23 +00:00

Flango

A Flask + HTMX boilerplate for server-rendered apps and JSON APIs, organised with a Django-inspired MVC layout (blueprints = apps, models / services / views layers). Includes CSRF protection, session-based login and a protected Flask-Admin.

See AGENTS.md for the agent contract and the map of this project: conventions, the trigger table and the traps that bite first. The deeper notes — architecture, deployment, forge and gotchas — live in AGENTS.d/, a private knowledge repo (agentic/flango) that is cloned next to the code and never committed here. A public clone works without it; AGENTS.md says which document wins when both exist.

Quick start

Requires Python 3.13 and uv.

uv sync
cp .env.example .env
uv run flask --app app db upgrade
uv run flask --app app create-admin --username admin   # prompts for a password
uv run flask --app app run --debug

Then open:

URL Description
/ HTMX task demo (create / toggle / delete)
/auth/login Login form
/auth/logout Logout (POST only)
/api/tasks JSON API (GET, POST)
/api/tasks/<id> JSON API (GET, PUT, DELETE)
/healthz JSON health probe (200 only with a working DB)
/admin Flask-Admin, protected (admin users only)
/admin/stats/ Visit statistics (admin users only)

Configuration

Configuration comes from environment variables (loaded from .env via python-dotenv):

Variable Default Notes
SECRET_KEY dev fallback Set a real secret in production.
DATABASE_URL sqlite:///main.db Relative SQLite paths resolve to instance/.
FLASK_DEBUG off Enables debug mode.
LOG_LEVEL INFO Written to stdout and instance/app.log.
SESSION_COOKIE_SECURE off Set to 1 when served over HTTPS.
SESSION_COOKIE_HTTPONLY on Keep on; JavaScript must not read the cookie.
SESSION_COOKIE_SAMESITE Lax Lax, Strict or None.
TRUST_PROXY off 1 only behind exactly one TLS proxy.
STATS_ENABLED on First-party visit statistics; 0 turns them off.
STATS_RETENTION_DAYS 90 Age at which flask stats prune deletes visits.

Runtime files (SQLite database, logs) live in instance/ and are git-ignored.

Authentication

Users live in the users table (auth/models.py) and log in at /auth/login with a hashed password (Werkzeug scrypt). Create the first admin with:

uv run flask --app app create-admin --username admin

Only users with is_admin=True can access /admin; regular users are redirected to the login page. There is no public sign-up or password reset.

CSRF protection

Flask-WTF's CSRFProtect is enabled globally. All HTML forms (tasks, login, logout, admin) render a csrf_token field. The JSON API under /api is CSRF-exempt because it is intended for non-browser clients; Flask-Admin keeps its own WTForms CSRF handling.

Visit statistics

The admin console has a read-only /admin/stats/ page: views, unique visitors per day, top pages, referrers and a separate crawler bucket. It is first-party and cookie-free — one row per counted GET, with no raw IP and no full user agent:

  • ip_hash is HMAC(SECRET_KEY, "<utc-date>:<ip>"), so the same person has a different id tomorrow and the IP cannot be recovered;
  • /static, /api, /admin, /healthz, robots.txt/sitemap.xml and non-200 responses are not counted; POSTs are not counted;
  • browser / crawler / other is the only user-agent information stored;
  • rows older than STATS_RETENTION_DAYS (default 90) are removed with uv run flask --app app stats prune (add --days N to override). Run it on a timer in production.

STATS_ENABLED=0 stops recording; the admin page remains and shows the rows already stored. The model and its migration are committed; the data lives in the normal app database.

Tests

uv run pytest

The suite uses in-memory SQLite databases and covers the HTML/HTMX flows, API CRUD, CSRF behaviour, login/logout, admin protection, create-admin and visit statistics (recording rules, hashing, uniques, prune, admin gating).

Database migrations

The migrations are committed, so a fresh clone only needs uv run flask --app app db upgrade.

After changing a model:

uv run flask --app app db migrate -m "describe the change"
uv run flask --app app db upgrade

flask db init is only needed if you start a project without a migrations/ folder.

Running in a container

The repo ships a multi-stage Dockerfile (uv build, non-root runtime) and a docker-compose.yml for a self-hosted demo:

cp .env.example .env        # set a real SECRET_KEY; FLASK_DEBUG=0 for a shared host
docker compose up -d --build
curl -fsS http://127.0.0.1:8000/healthz

On srvbtw 127.0.0.1:8000 is already used by the web-frontend dev container; use WEB_PORT=18001 docker compose up -d --build there.

docker-entrypoint.sh applies the committed migrations and starts Gunicorn (GUNICORN_WORKERS, default 2). SQLite and the log live in the instance volume. /healthz answers 200 only while the database answers; the image and compose use it as the healthcheck.

Production deployment

deploy/ is the reference for a single host behind a reverse proxy:

  • deploy/compose.yaml runs the registry image pinned by IMAGE_TAG and publishes it on 127.0.0.1:8000 only.
  • deploy/deploy.sh updates the tag, waits for the healthcheck and rolls back to the previous tag if the new image does not become healthy.
  • deploy/README.md is the host runbook: first setup, the Caddy site block, secrets, backups and the rollback procedure.

CI is in .forgejo/workflows/: ci.yml runs pytest on master, publish.yml builds and pushes git.pvlb.dev/web/flango:<version> on a v* tag, and deploy.yml is a manual dispatch that calls the host deploy script. No workflow uses a pull_request trigger — the shared runner hands jobs the host Docker socket. The registry path, host names and secret names are pvlb.dev-specific; adjust them for another host.

License

Apache-2.0. See LICENSE.