- Python 63.1%
- HTML 24.4%
- Shell 9.8%
- Dockerfile 1.4%
- Mako 0.6%
- Other 0.7%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| .forgejo/workflows | ||
| auth | ||
| deploy | ||
| main | ||
| migrations | ||
| static | ||
| stats | ||
| templates | ||
| tests | ||
| .dockerignore | ||
| .env.example | ||
| .gitignore | ||
| .python-version | ||
| admin.py | ||
| AGENTS.md | ||
| app.py | ||
| config.py | ||
| docker-compose.yml | ||
| docker-entrypoint.sh | ||
| Dockerfile | ||
| extensions.py | ||
| LICENSE | ||
| notes.txt | ||
| pyproject.toml | ||
| README.md | ||
| uv.lock | ||
| wsgi.py | ||
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_hashisHMAC(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.xmland non-200 responses are not counted; POSTs are not counted;browser/crawler/otheris the only user-agent information stored;- rows older than
STATS_RETENTION_DAYS(default 90) are removed withuv run flask --app app stats prune(add--days Nto 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.yamlruns the registry image pinned byIMAGE_TAGand publishes it on127.0.0.1:8000only.deploy/deploy.shupdates the tag, waits for the healthcheck and rolls back to the previous tag if the new image does not become healthy.deploy/README.mdis 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.