# krish-void/multi-telnet-saas architecture

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

- Interactive diagram: https://gitdiagram.com/krish-void/multi-telnet-saas
- Repository: https://github.com/krish-void/multi-telnet-saas

## Overview

#### Purpose and entry points
This is a SaaS core for isolated multi-tenant company administration with hierarchical RBAC, dual authentication, and database-enforced audit/integrity controls. The API entry point is `backend/main.py`; the operator UI is the Streamlit app at `frontend/app.py`. Database bootstrap runs through `backend/init_db.py`, directly or via container startup scripts.

#### Runtime and deployment topology
The system runs three services under Compose: MariaDB 10.6, a Python/FastAPI backend, and a Python/Streamlit frontend. Container definitions and orchestration are in `Dockerfile.backend`, `Dockerfile.frontend`, and `docker-compose.yml`. Backend initialization/start sequencing is handled by `backend/startup.sh` and `start_backend.sh`; frontend startup uses `frontend/startup.sh`.

#### Database as the integrity boundary
MariaDB/MySQL is the authoritative persistence and business-rule layer. `backend/init_db.py` creates and seeds companies, users, roles, system-admin credentials, and logs; it also establishes foreign-key behavior, audit triggers, a restricted user view, and a per-company counting function. Connection configuration is centralized in `backend/database.py`. Tenant identity is represented by company ownership and tenant UUIDs, while database-side constructs protect integrity even if API code is bypassed.

#### API composition
FastAPI assembles authentication, company, and user-management route groups in `backend/main.py` and `backend/routes/__init__.py`. Domain endpoint boundaries are separated into `backend/routes/auth.py`, `backend/routes/companies.py`, and `backend/routes/users.py`. Request payload validation and API-facing schemas live in `backend/models.py`, keeping route handlers distinct from raw SQL/database operations.

#### Authentication and authorization flow
Clients use separate super-admin and tenant-employee login flows exposed by `backend/routes/auth.py`. Credential verification, password hashing, and JWT/session-token behavior are implemented in `backend/auth.py`. Protected requests pass through RBAC and tenant-isolation checks in `backend/middleware.py`: tenant users are scoped to the company in their authenticated session, and role permissions gate user writes and deletes. This middleware-to-route boundary is the primary application-layer defense against cross-tenant reads or mutations.

#### Tenant administration flow
A privileged actor authenticates, receives authenticated session/token context, then calls company or user routes. Company management in `backend/routes/companies.py` defines tenant lifecycle operations; user operations in `backend/routes/users.py` operate within the resolved company scope and role policy. User creation persists through the database schema, whose trigger records audit entries automatically rather than relying on application handlers.

#### Frontend and contract artifacts
`frontend/app.py` is a separate Streamlit dashboard that consumes the backend rather than hosting the business model itself. The repository also contains TypeScript contract packages: OpenAPI source at `lib/api-spec/openapi.yaml`, generated client bindings in `lib/api-client-react/src/generated/api.ts`, and generated Zod contracts in `lib/api-zod/src/generated/api.ts`. These are integration artifacts for typed API consumers; the active UI runtime remains Streamlit. A separate TypeScript database package, including schema exports at `lib/db/src/schema/index.ts`, appears to support external/shared consumers rather than the Python backend’s primary MariaDB access path.

Main entry points: [`backend/routes/auth.py`](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/routes/auth.py) (Authentication routes), [`backend/main.py`](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/main.py) (FastAPI application), [`backend/routes/companies.py`](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/routes/companies.py) (Company & user routes), [`docker-compose.yml`](https://github.com/krish-void/multi-telnet-saas/blob/main/docker-compose.yml) (Compose orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Compose Runtime"]
  node_compose{{"Compose orchestration<br/>deployment<br/>[docker-compose.yml]"}}
  node_backend_container["Backend container<br/>container image<br/>[Dockerfile.backend]"]
  node_backend_startup["Backend startup sequence<br/>startup script<br/>[startup.sh]"]
end

subgraph group_backend["FastAPI Backend"]
  node_api_app["FastAPI application<br/>API entry point<br/>[main.py]"]
  node_route_registry["Route registry<br/>route composition<br/>[__init__.py]"]
  node_auth_routes["Authentication routes<br/>API routes<br/>[auth.py]"]
  node_admin_routes["Company &amp; user routes<br/>API routes<br/>[companies.py]"]
  node_schemas["Request schemas<br/>API models<br/>[models.py]"]
  node_auth_service["Auth token service<br/>authentication<br/>[auth.py]"]
  node_rbac_middleware["RBAC &amp; tenant isolation<br/>authorization middleware<br/>[middleware.py]"]
end

subgraph group_data["MariaDB Integrity Layer"]
  node_mariadb[("MariaDB 10.6<br/>database")]
  node_db_bootstrap["Schema bootstrap &amp; seed<br/>database bootstrap<br/>[init_db.py]"]
  node_db_connection["Database connection<br/>persistence adapter<br/>[database.py]"]
  node_db_controls["Integrity &amp; audit controls<br/>database rules<br/>[init_db.py]"]
end

subgraph group_clients["Clients &amp; Contracts"]
  node_frontend["Streamlit dashboard<br/>operator UI<br/>[app.py]"]
  node_openapi["OpenAPI contract<br/>API specification<br/>[openapi.yaml]"]
  node_react_client["Generated React client<br/>TypeScript client bindings<br/>[api.ts]"]
  node_zod_contracts["Generated Zod contracts<br/>TypeScript validation contracts<br/>[api.ts]"]
  node_shared_db_schema["Shared TypeScript DB schema<br/>external database package<br/>[index.ts]"]
end

node_compose -->|"runs"| node_mariadb
node_compose -->|"runs"| node_backend_container
node_compose -->|"runs"| node_frontend
node_backend_container -->|"executes"| node_backend_startup
node_backend_startup -->|"initializes"| node_db_bootstrap
node_db_bootstrap -->|"creates and seeds"| node_mariadb
node_db_bootstrap -->|"establishes"| node_db_controls
node_db_controls -->|"enforced by"| node_mariadb
node_backend_startup -->|"starts"| node_api_app
node_api_app -->|"assembles"| node_route_registry
node_route_registry -->|"registers"| node_auth_routes
node_route_registry -->|"registers"| node_admin_routes
node_auth_routes -->|"authenticates with"| node_auth_service
node_api_app -->|"protects requests through"| node_rbac_middleware
node_rbac_middleware -->|"authorizes and scopes"| node_admin_routes
node_auth_routes -->|"validates with"| node_schemas
node_admin_routes -->|"validates with"| node_schemas
node_auth_routes -->|"queries"| node_db_connection
node_admin_routes -->|"persists through"| node_db_connection
node_db_connection -->|"connects to"| node_mariadb
node_frontend -->|"consumes API"| node_api_app
node_openapi -->|"generates"| node_react_client
node_openapi -->|"generates"| node_zod_contracts

click node_compose "https://github.com/krish-void/multi-telnet-saas/blob/main/docker-compose.yml"
click node_db_bootstrap "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/init_db.py"
click node_db_connection "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/database.py"
click node_db_controls "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/init_db.py"
click node_backend_container "https://github.com/krish-void/multi-telnet-saas/blob/main/Dockerfile.backend"
click node_backend_startup "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/startup.sh"
click node_api_app "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/main.py"
click node_route_registry "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/routes/__init__.py"
click node_auth_routes "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/routes/auth.py"
click node_admin_routes "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/routes/companies.py"
click node_schemas "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/models.py"
click node_auth_service "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/auth.py"
click node_rbac_middleware "https://github.com/krish-void/multi-telnet-saas/blob/main/backend/middleware.py"
click node_frontend "https://github.com/krish-void/multi-telnet-saas/blob/main/frontend/app.py"
click node_openapi "https://github.com/krish-void/multi-telnet-saas/blob/main/lib/api-spec/openapi.yaml"
click node_react_client "https://github.com/krish-void/multi-telnet-saas/blob/main/lib/api-client-react/src/generated/api.ts"
click node_zod_contracts "https://github.com/krish-void/multi-telnet-saas/blob/main/lib/api-zod/src/generated/api.ts"
click node_shared_db_schema "https://github.com/krish-void/multi-telnet-saas/blob/main/lib/db/src/schema/index.ts"

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_compose,node_backend_container,node_backend_startup toneBlue
class node_api_app,node_route_registry,node_auth_routes,node_admin_routes,node_schemas,node_auth_service,node_rbac_middleware toneAmber
class node_mariadb,node_db_bootstrap,node_db_connection,node_db_controls toneMint
class node_frontend,node_openapi,node_react_client,node_zod_contracts,node_shared_db_schema toneRose
```

## Components

### Compose Runtime

- [Compose orchestration](https://github.com/krish-void/multi-telnet-saas/blob/main/docker-compose.yml): deployment · Runs database, backend, and frontend services. · `docker-compose.yml`
- [Backend container](https://github.com/krish-void/multi-telnet-saas/blob/main/Dockerfile.backend): container image · `Dockerfile.backend`
- [Backend startup sequence](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/startup.sh): startup script · Initializes and starts the API service. · `backend/startup.sh`

### FastAPI Backend

- [FastAPI application](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/main.py): API entry point · Assembles the API route groups. · `backend/main.py`
- [Route registry](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/routes/__init__.py): route composition · `backend/routes/__init__.py`
- [Authentication routes](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/routes/auth.py): API routes · Separate super-admin and tenant-employee login flows. · `backend/routes/auth.py`
- [Company & user routes](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/routes/companies.py): API routes · Tenant lifecycle and scoped user administration. · `backend/routes/companies.py`
- [Request schemas](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/models.py): API models · `backend/models.py`
- [Auth token service](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/auth.py): authentication · Verifies credentials, hashes passwords, and issues JWT/session tokens. · `backend/auth.py`
- [RBAC & tenant isolation](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/middleware.py): authorization middleware · Scopes tenant sessions and gates protected writes and deletes. · `backend/middleware.py`

### MariaDB Integrity Layer

- **MariaDB 10.6**: database · Authoritative persistence and database-rule boundary.
- [Schema bootstrap & seed](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/init_db.py): database bootstrap · Creates seed data, triggers, view, and counting function. · `backend/init_db.py`
- [Database connection](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/database.py): persistence adapter · `backend/database.py`
- [Integrity & audit controls](https://github.com/krish-void/multi-telnet-saas/blob/main/backend/init_db.py): database rules · Foreign keys, audit triggers, restricted view, and tenant-aware function. · `backend/init_db.py`

### Clients & Contracts

- [Streamlit dashboard](https://github.com/krish-void/multi-telnet-saas/blob/main/frontend/app.py): operator UI · Separate UI that consumes the backend API. · `frontend/app.py`
- [OpenAPI contract](https://github.com/krish-void/multi-telnet-saas/blob/main/lib/api-spec/openapi.yaml): API specification · `lib/api-spec/openapi.yaml`
- [Generated React client](https://github.com/krish-void/multi-telnet-saas/blob/main/lib/api-client-react/src/generated/api.ts): TypeScript client bindings · `lib/api-client-react/src/generated/api.ts`
- [Generated Zod contracts](https://github.com/krish-void/multi-telnet-saas/blob/main/lib/api-zod/src/generated/api.ts): TypeScript validation contracts · `lib/api-zod/src/generated/api.ts`
- [Shared TypeScript DB schema](https://github.com/krish-void/multi-telnet-saas/blob/main/lib/db/src/schema/index.ts): external database package · Supports external or shared consumers outside the Python MariaDB path. · `lib/db/src/schema/index.ts`

## Connections

- Compose orchestration → MariaDB 10.6: runs
- Compose orchestration → Backend container: runs
- Compose orchestration → Streamlit dashboard: runs
- Backend container → Backend startup sequence: executes
- Backend startup sequence → Schema bootstrap & seed: initializes
- Schema bootstrap & seed → MariaDB 10.6: creates and seeds
- Schema bootstrap & seed → Integrity & audit controls: establishes
- Integrity & audit controls → MariaDB 10.6: enforced by
- Backend startup sequence → FastAPI application: starts
- FastAPI application → Route registry: assembles
- Route registry → Authentication routes: registers
- Route registry → Company & user routes: registers
- Authentication routes → Auth token service: authenticates with
- FastAPI application → RBAC & tenant isolation: protects requests through
- RBAC & tenant isolation → Company & user routes: authorizes and scopes
- Authentication routes → Request schemas: validates with
- Company & user routes → Request schemas: validates with
- Authentication routes → Database connection: queries
- Company & user routes → Database connection: persists through
- Database connection → MariaDB 10.6: connects to
- Streamlit dashboard → FastAPI application: consumes API
- OpenAPI contract → Generated React client: generates
- OpenAPI contract → Generated Zod contracts: generates
