# drcount-root/securefabric architecture

> Architecture diagram of the GitHub repository drcount-root/securefabric, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/drcount-root/securefabric
- Repository: https://github.com/drcount-root/securefabric

## Overview

1. This repository is a Turborepo monorepo with three runnable apps and several shared packages. The main product surface is the `web` app, the backend is `api`, and `docs` is a separate Next.js app for documentation or marketing content.

2. The frontend boundary is split cleanly by concern:
   - `apps/web` is the authenticated product UI.
   - `apps/docs` is a standalone public-facing Next.js site.
   Both are App Router-based Next.js apps and share design primitives from `packages/ui`.

3. `apps/web` is organized around authentication and account management flows rather than a generic CRUD UI. Its routes include signup, login, password reset, MFA, mobile verification, profile completion, and a post-login dashboard with profile, security, and setup pages. `web/middleware.ts` suggests client-side route gating before pages render.

4. The backend lives in `apps/api` and is structured as an HTTP service with clear layers:
   - route definitions in `src/routes`
   - domain services in `src/services`
   - middleware in `src/middleware`
   - shared infrastructure in `src/lib`
   - startup/container wiring in `src/app.ts`, `src/index.ts`, and `src/container.ts`
   This is an Express-style architecture centered on auth/session workflows.

5. The API’s domain is identity-heavy. Separate route modules exist for auth, passwords, MFA, passkeys, sessions, and users. The service layer mirrors that split with dedicated services for sessions, users, passwords, OTP, passkeys, magic links, MFA, devices, notifications, and auditing.

6. The persistence layer is centralized in `packages/database`, which contains the Prisma schema and migrations. `apps/api` uses `src/lib/prisma.ts` plus the shared database package, so the API is the sole application boundary that should directly talk to the relational database.

7. Redis is a second infrastructure dependency in the API layer. It is likely used for session state, rate limiting, verification codes, and other ephemeral auth data. That fits the presence of `rate-limit` middleware, OTP, cleanup jobs, and session-oriented services.

8. Real-time session awareness is handled separately through `apps/api/src/ws/sessions.gateway.ts`. That indicates the backend can push session/device changes to clients, likely for security-related UI updates such as active session lists or logout events.

9. There is a background job subsystem in `apps/api/src/jobs`, including a cleanup task. Combined with audit/notification services, the backend is designed to manage lifecycle work outside request-response flow, such as expiring records, pruning state, or sending auth-related notifications.

10. Shared cross-cutting contracts live in `packages/types`. These type packages define auth, JWT, session, and user shapes that can be reused across the API and web app to keep client/server behavior aligned.

11. Shared UI and tooling are also first-class packages:
   - `packages/ui` provides reusable React components like buttons, inputs, cards, alerts, and spinners.
   - `packages/eslint-config` and `packages/typescript-config` standardize linting and TypeScript settings across all apps.
   This reduces duplication and keeps app-level code focused on product logic.

12. Deployment is containerized. The repo includes Dockerfiles for `api` and `web`, top-level `docker-compose` files, and an `nginx` configuration directory, implying a production topology where Nginx fronts one or more app containers and the API runs alongside the database/Redis stack.

13. The primary data flow is: browser UI in `web` initiates auth or account actions, `web/lib/api.ts` and `web/lib/socket.ts` connect to the API over HTTP and WebSocket, `api` applies validation/auth/rate-limiting middleware, then services read/write Prisma-backed persistence and Redis-backed ephemeral state, and finally the UI reflects the resulting session/user state.

14. The repository is not just code; it includes operational scaffolding. CI is defined in GitHub Actions, there are tests for the API auth flows, and root docs like `ROADMAP.md`, `ISSUES.md`, and `PROCESS_TO_RUN.md` suggest this is an actively managed product repo with explicit development/process guidance.

Main entry points: [`apps/web`](https://github.com/drcount-root/securefabric/tree/main/apps/web) (Web app), [`apps/api`](https://github.com/drcount-root/securefabric/tree/main/apps/api) (API service), [`docker-compose.yml`](https://github.com/drcount-root/securefabric/blob/main/docker-compose.yml) (Compose stack), [`apps/api/src/routes`](https://github.com/drcount-root/securefabric/tree/main/apps/api/src/routes) (API routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_apps["Apps"]
  node_web["Web app<br/>nextjs app"]
  node_docs["Docs app<br/>nextjs app"]
  node_api["API service<br/>express service"]
  node_web_routes["Web routes<br/>app router"]
  node_web_client["Web client<br/>browser adapters"]
  node_web_guard(("Route guard<br/>middleware<br/>[middleware.ts]"))
  node_docs_routes["Docs routes<br/>app router"]
  node_api_routes["API routes<br/>http routes"]
  node_api_services["Domain services<br/>service layer"]
  node_api_middleware(("API middleware<br/>request middleware"))
  node_api_lib["API lib<br/>infrastructure helpers"]
  node_api_jobs["Background jobs<br/>job runner"]
  node_api_ws{{"Session gateway<br/>websocket gateway"}}
end

subgraph group_packages["Packages"]
  node_database[("Database<br/>prisma package")]
  node_types["Types<br/>shared contracts"]
  node_ui["UI kit<br/>react components"]
  node_eslint_cfg["Lint config<br/>shared config"]
  node_ts_cfg["TS config<br/>shared config"]
end

subgraph group_infra["Infra"]
  node_compose["Compose stack<br/>deployment<br/>[docker-compose.yml]"]
  node_nginx["Nginx<br/>reverse proxy<br/>[nginx.conf]"]
end

node_web -->|"renders"| node_web_routes
node_web -->|"uses"| node_web_client
node_web -->|"protected by"| node_web_guard
node_web_routes -->|"shares UI"| node_ui
node_web_client -->|"calls"| node_api_routes
node_web_client -->|"subscribes"| node_api_ws
node_docs -->|"renders"| node_docs_routes
node_docs_routes -->|"shares UI"| node_ui
node_api_routes -->|"passes through"| node_api_middleware
node_api_routes -->|"delegates to"| node_api_services
node_api_services -->|"uses"| node_api_lib
node_api_lib -->|"persists via"| node_database
node_api_lib -->|"shares contracts"| node_types
node_api_services -->|"shares contracts"| node_types
node_api -->|"exposes"| node_api_routes
node_api -->|"runs"| node_api_jobs
node_api -->|"hosts"| node_api_ws
node_api -->|"depends on"| node_database
node_compose -->|"orchestrates"| node_api
node_compose -->|"orchestrates"| node_web
node_compose -->|"orchestrates"| node_docs
node_compose -->|"fronted by"| node_nginx
node_api -.->|"served through"| node_nginx
node_web -->|"shares contracts"| node_types
node_web -->|"uses"| node_ui
node_docs -->|"uses"| node_ui
node_web -->|"auth flow"| node_api

click node_web "https://github.com/drcount-root/securefabric/tree/main/apps/web"
click node_docs "https://github.com/drcount-root/securefabric/tree/main/apps/docs"
click node_api "https://github.com/drcount-root/securefabric/tree/main/apps/api"
click node_web_routes "https://github.com/drcount-root/securefabric/tree/main/apps/web/app"
click node_web_client "https://github.com/drcount-root/securefabric/tree/main/apps/web/lib"
click node_web_guard "https://github.com/drcount-root/securefabric/blob/main/apps/web/middleware.ts"
click node_docs_routes "https://github.com/drcount-root/securefabric/tree/main/apps/docs/app"
click node_api_routes "https://github.com/drcount-root/securefabric/tree/main/apps/api/src/routes"
click node_api_services "https://github.com/drcount-root/securefabric/tree/main/apps/api/src/services"
click node_api_middleware "https://github.com/drcount-root/securefabric/tree/main/apps/api/src/middleware"
click node_api_lib "https://github.com/drcount-root/securefabric/tree/main/apps/api/src/lib"
click node_api_jobs "https://github.com/drcount-root/securefabric/tree/main/apps/api/src/jobs"
click node_api_ws "https://github.com/drcount-root/securefabric/blob/main/apps/api/src/ws/sessions.gateway.ts"
click node_database "https://github.com/drcount-root/securefabric/tree/main/packages/database"
click node_types "https://github.com/drcount-root/securefabric/tree/main/packages/types"
click node_ui "https://github.com/drcount-root/securefabric/tree/main/packages/ui"
click node_eslint_cfg "https://github.com/drcount-root/securefabric/tree/main/packages/eslint-config"
click node_ts_cfg "https://github.com/drcount-root/securefabric/tree/main/packages/typescript-config"
click node_compose "https://github.com/drcount-root/securefabric/blob/main/docker-compose.yml"
click node_nginx "https://github.com/drcount-root/securefabric/blob/main/nginx/nginx.conf"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web,node_docs,node_api,node_web_routes,node_web_client,node_web_guard,node_docs_routes,node_api_routes,node_api_services,node_api_middleware,node_api_lib,node_api_jobs,node_api_ws toneBlue
class node_database,node_types,node_ui,node_eslint_cfg,node_ts_cfg toneAmber
class node_compose,node_nginx toneMint
```

## Components

### Apps

Runnable product surfaces and backend service

- [Web app](https://github.com/drcount-root/securefabric/tree/main/apps/web): nextjs app · Authenticated product UI with account and security flows · `apps/web`
- [Docs app](https://github.com/drcount-root/securefabric/tree/main/apps/docs): nextjs app · Public-facing documentation or marketing site · `apps/docs`
- [API service](https://github.com/drcount-root/securefabric/tree/main/apps/api): express service · Identity and session backend with HTTP, WebSocket, and background jobs · `apps/api`
- [Web routes](https://github.com/drcount-root/securefabric/tree/main/apps/web/app): app router · Signup, login, reset, MFA, onboarding, and dashboard pages · `apps/web/app`
- [Web client](https://github.com/drcount-root/securefabric/tree/main/apps/web/lib): browser adapters · HTTP and socket clients plus auth/session glue · `apps/web/lib`
- [Route guard](https://github.com/drcount-root/securefabric/blob/main/apps/web/middleware.ts): middleware · Pre-render access control for authenticated and onboarding flows · `apps/web/middleware.ts`
- [Docs routes](https://github.com/drcount-root/securefabric/tree/main/apps/docs/app): app router · Standalone public app pages and layout · `apps/docs/app`
- [API routes](https://github.com/drcount-root/securefabric/tree/main/apps/api/src/routes): http routes · Auth, password, MFA, passkey, session, and user endpoints · `apps/api/src/routes`
- [Domain services](https://github.com/drcount-root/securefabric/tree/main/apps/api/src/services): service layer · Auth, sessions, users, MFA, passkeys, passwords, notifications, audit, and devices · `apps/api/src/services`
- [API middleware](https://github.com/drcount-root/securefabric/tree/main/apps/api/src/middleware): request middleware · Auth, onboarding, profile, validation, rate limiting, and request IDs · `apps/api/src/middleware`
- [API lib](https://github.com/drcount-root/securefabric/tree/main/apps/api/src/lib): infrastructure helpers · Prisma, Redis, JWT, crypto, errors, and async handler utilities · `apps/api/src/lib`
- [Background jobs](https://github.com/drcount-root/securefabric/tree/main/apps/api/src/jobs): job runner · Cleanup and other lifecycle tasks outside request-response flow · `apps/api/src/jobs`
- [Session gateway](https://github.com/drcount-root/securefabric/blob/main/apps/api/src/ws/sessions.gateway.ts): websocket gateway · Pushes session and device state changes to clients · `apps/api/src/ws/sessions.gateway.ts`

### Packages

Shared libraries and platform contracts

- [Database](https://github.com/drcount-root/securefabric/tree/main/packages/database): prisma package · Shared schema, migrations, and Prisma client for relational persistence · `packages/database`
- [Types](https://github.com/drcount-root/securefabric/tree/main/packages/types): shared contracts · Auth, JWT, session, and user types shared across client and server · `packages/types`
- [UI kit](https://github.com/drcount-root/securefabric/tree/main/packages/ui): react components · Shared buttons, inputs, cards, alerts, code, and spinners · `packages/ui`
- [Lint config](https://github.com/drcount-root/securefabric/tree/main/packages/eslint-config): shared config · Common ESLint rules for app and package code · `packages/eslint-config`
- [TS config](https://github.com/drcount-root/securefabric/tree/main/packages/typescript-config): shared config · Shared TypeScript baselines for node, next, and React packages · `packages/typescript-config`

### Infra

Deployment and operational scaffolding

- [Compose stack](https://github.com/drcount-root/securefabric/blob/main/docker-compose.yml): deployment · Local and production container orchestration for app, database, and Redis · `docker-compose.yml`
- [Nginx](https://github.com/drcount-root/securefabric/blob/main/nginx/nginx.conf): reverse proxy · Fronts app containers in deployed topology · `nginx/nginx.conf`

## Connections

- Web app → Web routes: renders · Next.js app serves the authenticated route tree
- Web app → Web client: uses · UI calls backend over HTTP and WebSocket
- Web app → Route guard: protected by · Middleware gates access before rendering
- Web routes → UI kit: shares UI · Product pages reuse shared design primitives
- Web client → API routes: calls · HTTP client talks to backend endpoints
- Web client → Session gateway: subscribes · Socket client receives live session updates
- Docs app → Docs routes: renders · Standalone public Next.js route tree
- Docs routes → UI kit: shares UI · Docs app reuses the same component library
- API routes → API middleware: passes through · Requests are validated and guarded before handlers
- API routes → Domain services: delegates to · Route handlers orchestrate domain services
- Domain services → API lib: uses · Services rely on crypto, JWT, Redis, Prisma, and helpers
- API lib → Database: persists via · Prisma client reads and writes relational state
- API lib → Types: shares contracts · Backend uses shared auth and session shapes
- Domain services → Types: shares contracts · Service layer aligns domain payloads with shared types
- API service → API routes: exposes · HTTP server mounts route modules
- API service → Background jobs: runs · Service process executes cleanup and lifecycle work
- API service → Session gateway: hosts · Backend serves WebSocket session gateway
- API service → Database: depends on · API is the application boundary for relational persistence
- Compose stack → API service: orchestrates · Container stack runs backend service
- Compose stack → Web app: orchestrates · Container stack runs the product UI
- Compose stack → Docs app: orchestrates · Container stack can run the docs site
- Compose stack → Nginx: fronted by · Proxy terminates and routes external traffic
- API service → Nginx: served through · Deployment topology places the API behind proxying infrastructure
- Web app → Types: shares contracts · Frontend aligns auth and session state with backend types
- Web app → UI kit: uses · Frontend consumes shared component primitives
- Docs app → UI kit: uses · Docs site consumes shared component primitives
- Web app → API service: auth flow · Browser product flow drives backend identity operations
