Single-binary self-hosted website analytics on Rust axum: collector API, dashboards, world map, and PDF reports.
analyticsaxumdockerrustself-hostedsqliteviteweb-analytics
1CARGO ?= $(HOME)/.cargo/bin/cargo
2PORT ?= 8000
3
4.DEFAULT_GOAL := run
5.PHONY: run build start clean push pull maps seed migrate
6
7# Dev: Vite watch + cargo run concurrently. Both die on Ctrl+C.
8run: frontend/node_modules dist/.vite/manifest.json static_maps/world.json
9 @trap 'kill 0' EXIT INT TERM; \
10 (cd frontend && bun run dev) & \
11 PORT=$(PORT) $(CARGO) run
12
13# Production build (Vite assets + release binary)
14build: frontend/node_modules static_maps/world.json
15 cd frontend && bun run build
16 $(CARGO) build --release
17
18# Run the release binary (after `make build`)
19start:
20 PORT=$(PORT) ./target/release/analytics
21
22# Force-rebuild the per-country topojson from natural earth.
23maps: frontend/node_modules
24 cd frontend && bun run build:maps
25
26# Lazy build for `make run`: only fetches maps if world.json is missing.
27# Use `make maps` to force a refresh.
28static_maps/world.json: frontend/node_modules
29 cd frontend && bun run build:maps
30
31# Nuke everything regenerable: build output, deps, and the local data dir
32# (sqlite + WAL/SHM, geoip mmdb, ua-parser regexes.yaml — all re-fetched on
33# next boot or via `make pull`). Also drops the natural-earth topojson, which
34# `make maps` (and `make run`) will rebuild.
35clean:
36 rm -rf target dist frontend/node_modules data static_maps
37
38push:
39 git remote | xargs -I R git push R master
40
41# Seed a "Seed Test" property with realistic fake events. Re-runnable.
42# Override defaults: `make seed SESSIONS=2000 DAYS=60`
43seed:
44 $(CARGO) run --bin seed -- $(SESSIONS) $(DAYS)
45
46# Import an existing Django analytics SQLite into the rust hot-field schema.
47# `make migrate FROM=../analytics/db.sqlite3` (add FORCE=1 to wipe first).
48migrate:
49 @if [ -z "$(FROM)" ]; then echo "usage: make migrate FROM=<path-to-django.sqlite3> [FORCE=1]"; exit 2; fi
50 $(CARGO) run -- migrate "$(FROM)" $(if $(FORCE),--force,)
51
52# Pull production data (db, geoip, media) for local dev
53pull:
54 @SERVER=$$(git config --get remote.server.url | sed 's|ssh://||' | cut -d ':' -f 1 | cut -d '/' -f 1); \
55 NAME=$$(basename $$(pwd)); \
56 mkdir -p data; \
57 rsync -avz $$SERVER:/srv/data/$$NAME/db/db.sqlite3 data/db.sqlite3; \
58 rsync -avz $$SERVER:/srv/data/$$NAME/db.mmdb data/db.mmdb
59
60frontend/node_modules:
61 cd frontend && bun install
62
63dist/.vite/manifest.json: frontend/node_modules
64 cd frontend && bun run build