Skip to content

Database, NATS, and Storage

Database, NATS, file storage, and mail share one property: they are read at startup, so a wrong value either stops the process or needs a restart. Change behaviour in the admin UI, change dependencies by restarting.

  • Shell access to the host — none of this lives in an admin form.
  • Database and NATS reachable; see Environment Variables for the list.
VariableDefaultNotes
DB_TYPEsqlitesqlite / mysql / postgres
DB_DSNemptyFile path for sqlite; required for the others
DB_MAX_OPEN_CONNS / DB_MAX_IDLE_CONNS25 / 10Connection pool ceiling and idle keep
DB_CONN_MAX_LIFETIME / DB_CONN_MAX_IDLE_TIME300 / 60Max age and idle reclaim (seconds)

sqlite suits single-node installs; an empty DB_DSN puts the file at db/app.db under the backend working directory. Multi-instance deployments need postgres or mysql, because concurrent SQLite writes serialize behind locks.

Migrations run two ways: AutoMigrate at startup adds missing columns and indexes (repeatable), while hand-written SQL in app/server/db/migrations/ never runs automatically.

An empty NATS_URL makes the process exit, not degrade: NATS carries the KV storage for session caching, rate-limit counters, settings, OAuth state, and codes, plus the event bus for notifications, moderation, and webhooks. It must be external with JetStream enabled:

终端窗口
nats-server -js # local
docker run --rm -p 4222:4222 nats:2.10-alpine -js # container
VariableDefaultNotes
NATS_URLemptyRequired, e.g. nats://127.0.0.1:4222
NATS_BUCKETfeedchamber_kvJetStream KV bucket; must differ per environment
DataPrefixIf lost
Session and settings cachesess:, setting:Falls back to normal DB reads
Rate-limit windowsrl:Windows reset, limits briefly loosen
OAuth stateoauth:, oauth_state:In-flight authorization fails; retry
Email codes, MFA challengesemailcode:, mfa:login:Those codes become invalid
View-count buffervc:Unflushed counts are lost (60s flush)

The event bus guarantees “one consumption per event per cluster” via queue subscriptions, so multi-instance deployments do not duplicate notifications. Events use core pub/sub, so anything published during an outage is not replayed — a deliberate trade-off.

Storage is a startup singleton: read once, then fixed — restart after changing it.

Setting keyEnv fallbackDefault
storage_provider / storage_local_dirSTORAGE_PROVIDER / STORAGE_LOCAL_DIRlocal / uploads
s3_endpoint / s3_bucket / s3_regionsame namesempty / empty / us-east-1
s3_access_key_id / s3_access_key_secretsame namesempty
s3_use_ssl / s3_custom_domainS3_USE_SSL / S3_CUSTOM_DOMAINtrue / empty

Precedence is settings table → environment → default; the admin entry is /admin/settings?tab=storage. To switch to S3, fill endpoint, region, bucket, and keys, set the provider to s3, restart, and upload an image to check the returned URL.

Configured at /admin/settings?tab=user → “Email”. The master switch is email_enabled; with it off, requesting a code fails immediately.

KeyDefaultNotes
email_providerloglog / smtp / sendgrid / resend
email_from / email_from_nameempty / Feed ChamberSender address and display name
email_smtp_host / email_smtp_portempty / 587SMTP endpoint and port
email_username / email_passwordemptySMTP credentials
email_use_tlsfalsetrue = implicit TLS (465); false = STARTTLS (587)
email_api_keyemptySendGrid or Resend key

log writes the message to the server log instead of sending — the cheapest local baseline for “did SMTP actually send?”. resend and sendgrid fail loudly without email_api_key. Sending limits live with the codes: email_resend_cooldown (60s), email_daily_limit (10/day), email_code_ttl (600s); see Authentication and Security.

终端窗口
# Database: the server answers and reads settings
curl -s -X POST http://localhost:8080/api/v1/settings/public | jq '.code'
# NATS: startup log shows "EventBus/KV Store: external NATS at nats://..." and no KV error
# Storage: request a presigned upload and inspect the returned URL prefix
curl -s -X POST http://localhost:8080/api/v1/upload/presign \
-H 'Authorization: Bearer <sid>' -H 'Content-Type: application/json' \
-d '{"file_name":"t.png","content_type":"image/png"}' | jq '.data.fileUrl'
SymptomCauseFix
Exits with NATS_URL is requiredNot configured, or the name is wrongStart nats-server -js, then set NATS_URL
初始化 KV Store 失败JetStream is not enabledStart with -js
failed to initialize databaseBad DSN, missing database, no privilegesConnect with psql / mysql first
Random 429s with several instancesThey point at different NATS or bucketsAlign both variables
S3 set but uploads stay localStorage is a startup singletonRestart the backend
Uploads return 403Wrong keys, bucket policy, or SSL mismatchVerify with aws s3 ls