# timapelmesh/corax architecture

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

- Interactive diagram: https://gitdiagram.com/timapelmesh/corax
- Repository: https://github.com/timapelmesh/corax

## Overview

#### Purpose and Entry Points
CORAX is a LAN-oriented PC inventory and lightweight helpdesk system. Windows agents collect workstation snapshots; a FastAPI backend persists and operates on them; a React SPA provides administration.

- Server entry: `run.py`; development orchestration: `start_all.py`, `start_all.bat`.
- Backend application/lifecycle: `backend/app/main.py`.
- Browser entry and routing: `frontend/src/main.tsx`, `frontend/src/App.tsx`.
- Production serves built SPA and API from one backend process, normally on port 3000; development separates Vite UI (:3000) and API (:3001).

#### Backend API and Domain Boundary
FastAPI is the system-of-record boundary. It wires middleware, authentication, migrations, routers, and production static UI serving.

- Router layer: `backend/app/routers/`.
- ORM and shared data contracts: `backend/app/models.py`, `backend/app/schemas.py`.
- Configuration and environment/production-secret validation: `backend/app/config.py`.
- API supports `/api/v1` and compatibility `/api`; integrations should target v1.

Core domains include computers/inventory, dashboard/software, tags, users, service requests, building diagrams, printers, warehouse, agent token/bundle administration, and WikiRAG.

#### Persistence and Lifecycle
PostgreSQL is mandatory and is accessed asynchronously through SQLAlchemy/asyncpg.

- Connection/session infrastructure: `backend/app/database.py`, `backend/app/async_pool.py`.
- Startup schema evolution: `backend/app/migrations.py`.
- PostgreSQL bootstrap and legacy SQLite migration: `scripts/ensure_postgres.py`, `scripts/migrate_sqlite_to_postgres.py`.

Three configured URLs isolate logical concerns—main inventory, diagrams, and warehouse—though all normally point to one `inventory` database. Raw agent payloads, Bitrix diagnostics, and knowledge documents are filesystem-backed rather than primary database records.

#### Inventory-Agent Pipeline
Agents POST authenticated JSON inventory to the backend; server-side processing upserts a computer case-insensitively by hostname and replaces current software/peripheral snapshots.

- Ingestion endpoint/domain: `backend/app/routers/agent.py`.
- Per-agent token issuance, HMAC-hashed storage, revocation, and optional hostname binding: `backend/app/routers/agent_tokens.py`.
- Server-generated distributable agents: `backend/app/agent_bundle.py`, `backend/app/routers/agent_bundles.py`.
- Win10/11 PowerShell collection stages: `agent/v3/win10/InventoryClient.ps1`, `agent/v3/win10/lib/Collect-System.ps1`, `agent/v3/win10/lib/Invoke-Post.ps1`.
- Python/EXE agent basis: `agent/python/agent.py`.

This boundary uses Bearer agent tokens, payload-size/rate controls, and is exempt from browser CSRF.

#### Web Application and Collaboration
The React 19/Vite SPA uses a shared API client and auth context, with pages organized around operational domains.

- Cross-cutting API/auth state: `frontend/src/api.ts`, `frontend/src/AuthContext.tsx`.
- Shell/navigation: `frontend/src/pages/Layout.tsx`.
- Primary operational views: `frontend/src/pages/ComputersPage.tsx`, `frontend/src/pages/DashboardPage.tsx`, `frontend/src/pages/ServiceRequestsPage.tsx`.
- Building-map live updates: `backend/app/routers/diagrams.py`, `backend/app/diagram_live.py`, `frontend/src/useDiagramLive.ts`.

Local CORAX users authenticate via JWT cookies plus CSRF for unsafe browser operations; role boundaries are observer, editor, and admin.

#### Operations, Integrations, and External Services
Service requests connect users, computers, categories, templates, GLPI CSV exchange, and PDF output through `backend/app/routers/service_requests.py` and `backend/app/service_request_tickets.py`. Printer discovery/polling uses SNMP: `backend/app/printer_scheduler.py`, `backend/app/printer_snmp.py`.

Optional boundaries are LDAP directory synchronization (`backend/app/ldap_auth.py`), Bitrix24 webhooks/bot events (`backend/app/routers/bitrix24_incoming.py`), LibreOffice conversion/export for diagrams (`backend/app/svg_export.py`), and LM Studio’s local OpenAI-compatible endpoint for knowledge-base RAG (`backend/app/wikirag_lm.py`, `backend/app/wikirag_corax.py`).

#### Deployment Runtime
Material runtimes are Python 3.12+, Node 18+/20, PostgreSQL 14+/16, Windows PowerShell agents, and optionally LibreOffice/LM Studio. Linux production uses systemd units in `deploy/corax-backend.service`; Windows can use the provided batch launchers. PostgreSQL backups are implemented around `backend/app/pg_backup.py`.

Main entry points: [`backend/app/routers/__init__.py`](https://github.com/timapelmesh/corax/blob/main/backend/app/routers/__init__.py) (API domain routers), [`backend/app/main.py`](https://github.com/timapelmesh/corax/blob/main/backend/app/main.py) (Application lifecycle), [`backend/app/models.py`](https://github.com/timapelmesh/corax/blob/main/backend/app/models.py) (ORM and contracts), [`backend/app/routers/agent.py`](https://github.com/timapelmesh/corax/blob/main/backend/app/routers/agent.py) (Inventory ingestion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_agents["Endpoint agents"]
  node_agent_collection["PowerShell collection<br/>Windows collector"]
  node_agent_post["Authenticated upload<br/>PowerShell transport<br/>[Invoke-Post.ps1]"]
end

subgraph group_backend["FastAPI backend"]
  node_backend_app["Application lifecycle<br/>FastAPI app<br/>[main.py]"]
  node_api_routers["API domain routers<br/>FastAPI routers<br/>[__init__.py]"]
  node_data_contracts["ORM and contracts<br/>SQLAlchemy models<br/>[models.py]"]
  node_database_layer["Async database access<br/>SQLAlchemy async<br/>[database.py]"]
  node_migrations["Schema evolution<br/>startup migrations<br/>[migrations.py]"]
  node_agent_ingestion["Inventory ingestion<br/>agent router<br/>[agent.py]"]
  node_agent_tokens["Agent token administration<br/>token router<br/>[agent_tokens.py]"]
  node_agent_bundles["Agent bundle generator<br/>bundle service<br/>[agent_bundle.py]"]
  node_diagram_live["Live building diagrams<br/>realtime updates<br/>[diagram_live.py]"]
  node_service_requests["Service request workflow<br/>ticket integration"]
  node_printer_polling["Printer discovery and polling<br/>SNMP scheduler"]
end

subgraph group_web["React SPA"]
  node_browser_entry["SPA entry and routing<br/>React/Vite<br/>[main.tsx]"]
  node_web_shell["Operational UI shell<br/>React pages<br/>[Layout.tsx]"]
  node_web_api_auth["API client and auth<br/>React state<br/>[api.ts]"]
end

subgraph group_external["External services"]
  node_ldap{{"LDAP directory<br/>optional integration<br/>[ldap_auth.py]"}}
  node_bitrix{{"Bitrix24<br/>webhook integration"}}
  node_lm_studio{{"LM Studio<br/>local AI endpoint<br/>[wikirag_lm.py]"}}
end

node_server_entry["Server entry<br/>Python launcher<br/>[run.py]"]
node_postgres[("Inventory PostgreSQL")]

node_server_entry -->|"starts"| node_backend_app
node_backend_app -->|"runs at startup"| node_migrations
node_migrations -->|"evolves schema"| node_postgres
node_backend_app -->|"mounts"| node_api_routers
node_api_routers -->|"uses"| node_data_contracts
node_data_contracts -->|"persists through"| node_database_layer
node_database_layer -->|"asyncpg"| node_postgres
node_agent_collection -->|"produces snapshot"| node_agent_post
node_agent_post -->|"Bearer JSON POST"| node_agent_ingestion
node_agent_tokens -->|"authorizes"| node_agent_ingestion
node_agent_ingestion -->|"updates inventory"| node_data_contracts
node_agent_bundles -.->|"distributes"| node_agent_collection
node_browser_entry -->|"renders"| node_web_shell
node_web_shell -->|"uses"| node_web_api_auth
node_web_api_auth -->|"versioned API calls"| node_api_routers
node_backend_app -.->|"serves production build"| node_browser_entry
node_diagram_live -->|"diagram updates"| node_api_routers
node_service_requests -->|"stores requests"| node_data_contracts
node_printer_polling -->|"updates printers"| node_data_contracts
node_ldap -.->|"optional directory auth"| node_backend_app
node_bitrix -.->|"webhook events"| node_api_routers
node_api_routers -.->|"WikiRAG queries"| node_lm_studio

click node_server_entry "https://github.com/timapelmesh/corax/blob/main/run.py"
click node_backend_app "https://github.com/timapelmesh/corax/blob/main/backend/app/main.py"
click node_api_routers "https://github.com/timapelmesh/corax/blob/main/backend/app/routers/__init__.py"
click node_data_contracts "https://github.com/timapelmesh/corax/blob/main/backend/app/models.py"
click node_database_layer "https://github.com/timapelmesh/corax/blob/main/backend/app/database.py"
click node_migrations "https://github.com/timapelmesh/corax/blob/main/backend/app/migrations.py"
click node_agent_collection "https://github.com/timapelmesh/corax/blob/main/agent/v3/win10/InventoryClient.ps1"
click node_agent_post "https://github.com/timapelmesh/corax/blob/main/agent/v3/win10/lib/Invoke-Post.ps1"
click node_agent_ingestion "https://github.com/timapelmesh/corax/blob/main/backend/app/routers/agent.py"
click node_agent_tokens "https://github.com/timapelmesh/corax/blob/main/backend/app/routers/agent_tokens.py"
click node_agent_bundles "https://github.com/timapelmesh/corax/blob/main/backend/app/agent_bundle.py"
click node_browser_entry "https://github.com/timapelmesh/corax/blob/main/frontend/src/main.tsx"
click node_web_shell "https://github.com/timapelmesh/corax/blob/main/frontend/src/pages/Layout.tsx"
click node_web_api_auth "https://github.com/timapelmesh/corax/blob/main/frontend/src/api.ts"
click node_diagram_live "https://github.com/timapelmesh/corax/blob/main/backend/app/diagram_live.py"
click node_service_requests "https://github.com/timapelmesh/corax/blob/main/backend/app/service_request_tickets.py"
click node_printer_polling "https://github.com/timapelmesh/corax/blob/main/backend/app/printer_scheduler.py"
click node_ldap "https://github.com/timapelmesh/corax/blob/main/backend/app/ldap_auth.py"
click node_bitrix "https://github.com/timapelmesh/corax/blob/main/backend/app/routers/bitrix24_incoming.py"
click node_lm_studio "https://github.com/timapelmesh/corax/blob/main/backend/app/wikirag_lm.py"

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_agent_collection,node_agent_post toneBlue
class node_backend_app,node_api_routers,node_data_contracts,node_database_layer,node_migrations,node_agent_ingestion,node_agent_tokens,node_agent_bundles,node_diagram_live,node_service_requests,node_printer_polling toneAmber
class node_browser_entry,node_web_shell,node_web_api_auth toneMint
class node_ldap,node_bitrix,node_lm_studio toneRose
class node_server_entry,node_postgres toneNeutral
```

## Components

### Endpoint agents

- [PowerShell collection](https://github.com/timapelmesh/corax/blob/main/agent/v3/win10/InventoryClient.ps1): Windows collector · `agent/v3/win10/InventoryClient.ps1`
- [Authenticated upload](https://github.com/timapelmesh/corax/blob/main/agent/v3/win10/lib/Invoke-Post.ps1): PowerShell transport · `agent/v3/win10/lib/Invoke-Post.ps1`

### FastAPI backend

- [Application lifecycle](https://github.com/timapelmesh/corax/blob/main/backend/app/main.py): FastAPI app · Wires middleware, auth, migrations, routers, and production SPA serving. · `backend/app/main.py`
- [API domain routers](https://github.com/timapelmesh/corax/blob/main/backend/app/routers/__init__.py): FastAPI routers · Serves versioned inventory, operations, and administration APIs. · `backend/app/routers/__init__.py`
- [ORM and contracts](https://github.com/timapelmesh/corax/blob/main/backend/app/models.py): SQLAlchemy models · `backend/app/models.py`
- [Async database access](https://github.com/timapelmesh/corax/blob/main/backend/app/database.py): SQLAlchemy async · `backend/app/database.py`
- [Schema evolution](https://github.com/timapelmesh/corax/blob/main/backend/app/migrations.py): startup migrations · `backend/app/migrations.py`
- [Inventory ingestion](https://github.com/timapelmesh/corax/blob/main/backend/app/routers/agent.py): agent router · Upserts computers by hostname and replaces current snapshots. · `backend/app/routers/agent.py`
- [Agent token administration](https://github.com/timapelmesh/corax/blob/main/backend/app/routers/agent_tokens.py): token router · Issues, revokes, and hostname-binds hashed Bearer tokens. · `backend/app/routers/agent_tokens.py`
- [Agent bundle generator](https://github.com/timapelmesh/corax/blob/main/backend/app/agent_bundle.py): bundle service · Generates distributable endpoint agents. · `backend/app/agent_bundle.py`
- [Live building diagrams](https://github.com/timapelmesh/corax/blob/main/backend/app/diagram_live.py): realtime updates · `backend/app/diagram_live.py`
- [Service request workflow](https://github.com/timapelmesh/corax/blob/main/backend/app/service_request_tickets.py): ticket integration · Connects requests to templates, GLPI CSV exchange, and PDF output. · `backend/app/service_request_tickets.py`
- [Printer discovery and polling](https://github.com/timapelmesh/corax/blob/main/backend/app/printer_scheduler.py): SNMP scheduler · `backend/app/printer_scheduler.py`

### React SPA

- [SPA entry and routing](https://github.com/timapelmesh/corax/blob/main/frontend/src/main.tsx): React/Vite · `frontend/src/main.tsx`
- [Operational UI shell](https://github.com/timapelmesh/corax/blob/main/frontend/src/pages/Layout.tsx): React pages · Hosts inventory, dashboard, and service-request workflows. · `frontend/src/pages/Layout.tsx`
- [API client and auth](https://github.com/timapelmesh/corax/blob/main/frontend/src/api.ts): React state · Uses JWT cookies and CSRF for unsafe browser operations. · `frontend/src/api.ts`

### External services

- [LDAP directory](https://github.com/timapelmesh/corax/blob/main/backend/app/ldap_auth.py): optional integration · `backend/app/ldap_auth.py`
- [Bitrix24](https://github.com/timapelmesh/corax/blob/main/backend/app/routers/bitrix24_incoming.py): webhook integration · `backend/app/routers/bitrix24_incoming.py`
- [LM Studio](https://github.com/timapelmesh/corax/blob/main/backend/app/wikirag_lm.py): local AI endpoint · Optional OpenAI-compatible endpoint for WikiRAG. · `backend/app/wikirag_lm.py`

### Other components

- [Server entry](https://github.com/timapelmesh/corax/blob/main/run.py): Python launcher · `run.py`
- **Inventory PostgreSQL**: PostgreSQL · Main, diagrams, and warehouse URLs normally target this database.

## Connections

- Server entry → Application lifecycle: starts
- Application lifecycle → Schema evolution: runs at startup
- Schema evolution → Inventory PostgreSQL: evolves schema
- Application lifecycle → API domain routers: mounts
- API domain routers → ORM and contracts: uses
- ORM and contracts → Async database access: persists through
- Async database access → Inventory PostgreSQL: asyncpg
- PowerShell collection → Authenticated upload: produces snapshot
- Authenticated upload → Inventory ingestion: Bearer JSON POST
- Agent token administration → Inventory ingestion: authorizes
- Inventory ingestion → ORM and contracts: updates inventory
- Agent bundle generator → PowerShell collection: distributes
- SPA entry and routing → Operational UI shell: renders
- Operational UI shell → API client and auth: uses
- API client and auth → API domain routers: versioned API calls
- Application lifecycle → SPA entry and routing: serves production build
- Live building diagrams → API domain routers: diagram updates
- Service request workflow → ORM and contracts: stores requests
- Printer discovery and polling → ORM and contracts: updates printers
- LDAP directory → Application lifecycle: optional directory auth
- Bitrix24 → API domain routers: webhook events
- API domain routers → LM Studio: WikiRAG queries
