Backend configuration
Precedence: CLI flags > config file > defaults (--help lists all flags), except where the reserved __backend collection carries a section — see Config from the database below. Start from hakobackend.example.toml.
host = "0.0.0.0"
port = 3000
# CHANGE DATABASE: edit 2 lines + POST /api/admin/reload. No rebuild.
driver = "hako" # hako | hakocluster | sqlite | postgres | mysql | rethinkdb
data = "./data/hako.ub" # path (hako/sqlite), comma-separated dirs (hakocluster), DSN (postgres/mysql)
# Socket peering with sibling backends (hako only, unix only, single-db):
# sync_serve = "/run/hakobackend/hako.sock"
# sync_peer = ["/run/other/hako.sock"]
# Cluster N+1 sync tailer (hakocluster driver only, default database
# only, file-only, boot-time). At most one lane.
# [cluster.tailer_mesh]
# name = "box-a"
# room_key = "shared-room-secret"
# port = 17891
# [cluster.tailer_cloud]
# mode = "client" # or "server"
# client_id = "box-a"
# auth_token = "shared-secret"
# room_name = "ops"
# room_key = "shared-secret"
# server_url = "ws://hub:8080" # client mode
# bind_addr = "0.0.0.0:8080" # server mode
auth = "off" # off | local | chain:github,local | ./custom.toml
# admin_uids = ["local:root"]
rules = "./policy.toml" # hot-reloaded; empty = OPEN dev mode (never prod)
[[indexes]] # created at startup + reload; nothing is inferred
collection = "posts"
fields = ["age"]
kind = "simple" # simple | composite | fts
limit_global = 600 # req/min per IP (+ _burst); /api/health always open
limit_auth = 20 # stricter layer for /api/auth/*
Notes:
- Co-located SQL over unix socket (lower overhead than TCP): postgres
data = "postgres://user:pass@%2Fvar%2Frun%2Fpostgresql/dbname", mysql with?socket=.... - Multidatabase:
[databases]adds named DBs (?db=on every route);datais alwaysdefault. Policy uses dotted names (akademik.mahasiswa, quoted in TOML). - Reload vs restart:
POST /api/admin/reload(admin UIDs only) picks up DB/auth/policy/limits/indexes — and the__backendconfig sections (below). Port/listen/TLS/socket changes need a restart. - Secrets never live in config files: provider params via env (
UB_LOCAL_JWT_SECRETmin 32 chars,UB_FIREBASE_PROJECT,UB_OIDC_ISSUER, …).
Config from the database
Policy, path aliases and service keys can live as docs in a reserved __backend collection in the default database — editable at runtime through the admin API, no file edit per change. One doc per section:
__backend/policy {"toml": "<policy.toml content>"}
__backend/aliases {"decl": [{"pattern": "/api/alias/x/:id", "target_path": "/api/collections/x", "target_query": ""}]}
__backend/service {"keys": ["<key>"], "allow": ["coll:read"]}
Rules of the road:
- A DB section that is present and valid wins over the file for that section; the file stays the bootstrap source and untouched sections keep their file values. The driver/path, listen address and the auth chain itself stay file/env-only — the database cannot hold its own connection info.
- Manage via the admin API (the
__prefix is never addressable on the data plane):GET/PUT/DELETE /api/admin/backend/{policy|aliases|service}— PUT validates first (policy parse, alias compile), then apply withPOST /api/admin/reload. Every call is audited (admin.backendevents). - Rollback:
DELETEa section, reload — the config file’s value for that section takes over again (404 = the section was never set). - Fail-closed: a malformed section is rejected at PUT time; a bad section found at reload fails the reload with 400 and the old config is fully retained. A database without
__backendis silent — the file keeps everything. - Policy from DB is reload-driven: once adopted, the policy file’s per-request watch is disabled and the
__backend/policydoc owns the policy until the next reload says otherwise. The DB policy’s[identity]feeds provider construction like the file one. admin_uidsstays file-only — a DB-carried policy must not be able to make itself admin; secrets stay env-only.
Common mistakes
- Production without
rules. Empty policy = open server with a WARN you stopped reading. Copypolicy.standard.tomlfirst, open later per collection. trust_proxy = truewithout a stripping proxy. ForgedX-Forwarded-Forthen defeats rate limits. Default false; enable only behind your own proxy.