homeio
PublikFork dari doctor-io/homeio
backend-architecture.md
Backend Architecture (Raspberry Pi 4 optimized)
Shape: Modular Monolith
- Runtime: Next.js Node runtime.
- Modules:
system,apps,files,network,notifications,scheduled-tasks, shareddbandcacheinfrastructure. - Realtime:
- SSE at
/api/v1/system/streamfor one-way live metrics. - SSE at
/api/v1/network/events/streamfor live network state updates. - SSE at
/api/v1/docker/stats/stream,/api/v1/store/operations/[operationId]/stream,/api/v1/notifications/stream, and/api/v1/files/usb/streamfor module-specific updates.
- SSE at
Why this works on Pi 4
- Single deployable process reduces memory and orchestration overhead.
- Postgres handles durable state and future growth without immediate migration.
- In-memory LRU cache absorbs hot reads and smooths CPU usage.
- SSE is lightweight for dashboards and state updates.
- Large file uploads stream through the Next route and, on bare-metal installs, can be handled by the Go upload sidecar over a local Unix socket.
API Endpoints
GET /api/healthPOST /api/auth/register(enabled when no users exist, or whenAUTH_ALLOW_REGISTRATION=true)POST /api/auth/loginPOST /api/auth/logoutGET /api/auth/mePOST /api/auth/unlockGET /api/auth/statusGET /api/v1/system/metricsGET /api/v1/system/stream(SSE)GET /api/v1/system/infoGET/POST /api/v1/system/disks*GET /api/v1/network/events/stream(SSE)GET /api/v1/appsGET/POST /api/v1/files*GET/POST/PATCH/DELETE /api/v1/files/google-drive*GET/PUT/DELETE /api/v1/settings/google-oauthPOST /api/v1/terminal/executeGET /api/v1/logs
Auth Flow
- Single-user flow with bootstrap redirect:
- If no users in DB:
/registeris the entry route. - If users exist:
/loginis the entry route.
- If no users in DB:
usersandsessionsare persisted in Postgres.- App routes and protected API routes call shared auth helpers such as
requireApiSession(); there is no central middleware auth layer for all/api/v1/**routes. - Lock screen uses the same authenticated session and validates password via
POST /api/auth/unlock.
Observability & Performance Logging
- Structured JSON logs are emitted to terminal and
LOG_FILE_PATH. - Every API, DB query, and realtime connection is logged with:
timestamp,runtime,level,layer,action,statusdurationMs(where applicable),requestId, andmeta- serialized
errorpayload on failures
- Hook/client actions use the same structure and are ingested through
/api/v1/logs. - Useful env flags:
LOG_LEVELandNEXT_PUBLIC_LOG_LEVELfor verbosityLOG_TO_FILEto enable/disable file sinkNEXT_PUBLIC_CLIENT_LOG_INGESTto enable/disable hook log ingestion
Client Data Strategy
- TanStack Query is configured in
AppProviders. useSystemMetricsfetches baseline metrics.useSystemSsepushes live metric updates into query cache.- Terminal commands are executed via the terminal API route and rendered by the desktop terminal window.
Setup
- Copy
.env.examplevalues into your runtime env. - Initialize DB schema:
- run
npm run db:init
- run
- Install dependencies and run:
npm installnpm run dev