# sarojgrs/python_demo_proj architecture

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

- Interactive diagram: https://gitdiagram.com/sarojgrs/python_demo_proj
- Repository: https://github.com/sarojgrs/python_demo_proj

## Overview

##### Project type and purpose
- This is a Python-based Starlette web application example that demonstrates HTTP routing, WebSocket endpoints, dependency injection, and the use of design patterns. It serves both dynamic pages (via Jinja2 templates) and static assets (CSS/JS), and exposes REST-style controllers and WebSocket endpoints for real-time communication.

##### File structure
- Top-level files:
- app.py – creates the Starlette instance, wires in routes, middleware, DI container
- decorator.py, middleware.py – cross-cutting concerns (logging, auth, etc.)
- requirements.txt, .env – configuration and dependencies
- scripts/ – install and run helpers
- statics/, templates/ – client‐side assets and HTML templates
- websocketClient.js – sample client for WebSocket endpoints
- Controller/ – all HTTP and WebSocket “controller” classes (User, VoiceMessage, WebSocketClient, etc.)
- DependencyInjection/ – defines ApiClient, CustomerService, RetailService, and the DI container(s)
- DesignPattern/ – examples of Factory Method, Singleton, and other patterns used in service instantiation
- Routes/ – Route definitions (RoutesModules.py) that map URL paths to Controller methods
- Scarlette/ – wrappers for inbound/outbound WebSocket commands
- docs/ – screenshots and usage documentation

##### README for additional insights
- Confirms the workflow: clone → install → run → browse HTTP and error pages
- Emphasizes two modes: debug vs. production
- Shows 404/500 examples

##### Main components
a. Client Side
- Browser: HTML+CSS+JS (templates + statics folder)
- websocketClient.js for WebSocket interactions
b. Server Side (Starlette App)
- HTTP Server (uvicorn/Starlette)
- Middleware layer (middleware.py, decorator.py)
- Router (RoutesModules.py)
- Dependency Injection Container (dependencyInjection.py + withService wrappers)
- Controllers (Controller/*.py)
- Service layer (DependencyInjection/CustomerService.py, RetailService.py, ApiClient.py)
- Design Pattern Modules (FactoryMethod, Singleton)
- Template engine (Jinja2 via Starlette)
c. External Services
- Third-party APIs called via ApiClient
- Any backend microservices or databases (if added in future)

##### Relationships and interactions
- Router → Middleware → Controllers
- Controllers ← DI Container → Services
- Services → External API / business logic modules
- Controller → Template engine → HTML response → Browser
- Browser ↔ WebSocket handshake → WebSocketController (Scarlette/InBoundCommand/OutBound)
- WebSocket messages routed to specific command handlers, processed by controllers/services, responses pushed back

##### Architectural patterns and design principles
- Dependency Injection: promotes loose coupling of controllers and services
- Factory Method & Singleton: used for service instantiation and global resource management
- Separation of Concerns:
- Routing vs. Controllers vs. Services
- Static vs. Dynamic content
- HTTP vs. WebSocket traffic
- Layered Architecture: presentation (templates), API layer, business logic, external integration

##### Essential elements
- Component labels matching folder names

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Tier
    subgraph "Client Tier"
        direction TB
        Browser["Browser\n(HTML/CSS/JS)"]:::frontend
        StaticAssets["Static Assets\n(statics/)"]:::frontend
        Templates["Jinja2 Templates\n(templates/)"]:::frontend
        WSClientSample["WebSocket Sample Client\n(websocketClient.js)"]:::frontend
    end

    Browser -->|HTTP GET/POST| StaticAssets
    Browser -->|HTTP GET| Templates
    Browser -->|WS CONNECT / MESSAGE| WSClientSample

    click StaticAssets "https://github.com/sarojgrs/python_demo_proj/tree/main/statics/"
    click Templates "https://github.com/sarojgrs/python_demo_proj/tree/main/templates/"
    click WSClientSample "https://github.com/sarojgrs/python_demo_proj/blob/main/websocketClient.js"

    %% Presentation Tier
    subgraph "Presentation Tier"
        direction TB
        HTTPServer["Starlette App\n(app.py)"]:::backend
        TemplateEngine["Template Engine\n(Jinja2)"]:::backend
    end

    StaticAssets -->|served by| HTTPServer
    Templates -->|render via| HTTPServer
    HTTPServer --> TemplateEngine

    click HTTPServer "https://github.com/sarojgrs/python_demo_proj/blob/main/app.py"

    %% Application Tier
    subgraph "Application Tier"
        direction TB
        Router["Router\n(Routes/RoutesModules.py)"]:::backend
        MiddlewareLayer["Middleware Layer\n(middleware.py, decorator.py)"]:::controller
        Controllers["Controllers\n(Controller/)"]:::controller
        DIContainer["DI Container\n(DependencyInjection/)"]:::backend
        Services["Service Layer\n(DependencyInjection/)"]:::service
        DesignPatterns["Design Patterns\n(DesignPattern/)"]:::pattern
        WSWrappers["WebSocket Wrappers\n(Scarlette/)"]:::backend
    end

    HTTPServer -->|routes to| Router
    Router -->|passes through| MiddlewareLayer
    MiddlewareLayer -->|invokes| Controllers
    Controllers -->|resolve deps via| DIContainer
    DIContainer -->|provides| Services
    Services -->|use| DesignPatterns
    WSClientSample -->|WS CONNECT| WSWrappers
    WSWrappers -->|dispatch to| Controllers

    click Router "https://github.com/sarojgrs/python_demo_proj/blob/main/Routes/RoutesModules.py"
    click MiddlewareLayer "https://github.com/sarojgrs/python_demo_proj/blob/main/middleware.py"
    click MiddlewareLayer "https://github.com/sarojgrs/python_demo_proj/blob/main/decorator.py"
    click DIContainer "https://github.com/sarojgrs/python_demo_proj/blob/main/DependencyInjection/dependencyInjection.py"
    click DIContainer "https://github.com/sarojgrs/python_demo_proj/blob/main/DependencyInjection/withService.py"
    click Services "https://github.com/sarojgrs/python_demo_proj/blob/main/DependencyInjection/ApiClient.py"
    click Services "https://github.com/sarojgrs/python_demo_proj/blob/main/DependencyInjection/CustomerService.py"
    click Services "https://github.com/sarojgrs/python_demo_proj/blob/main/DependencyInjection/RetailService.py"
    click Controllers "https://github.com/sarojgrs/python_demo_proj/blob/main/Controller/User.py"
    click Controllers "https://github.com/sarojgrs/python_demo_proj/blob/main/Controller/VoiceMessageController.py"
    click Controllers "https://github.com/sarojgrs/python_demo_proj/blob/main/Controller/WebSocketClient.py"
    click Controllers "https://github.com/sarojgrs/python_demo_proj/blob/main/Controller/WebSocket.py"
    click Controllers "https://github.com/sarojgrs/python_demo_proj/blob/main/Controller/Abc.py"
    click Controllers "https://github.com/sarojgrs/python_demo_proj/blob/main/Controller/Base.py"
    click WSWrappers "https://github.com/sarojgrs/python_demo_proj/blob/main/Scarlette/InboundCommand.py"
    click WSWrappers "https://github.com/sarojgrs/python_demo_proj/blob/main/Scarlette/OutBound.py"
    click DesignPatterns "https://github.com/sarojgrs/python_demo_proj/blob/main/DesignPattern/FactoryMethodDesignPattern.py"
    click DesignPatterns "https://github.com/sarojgrs/python_demo_proj/blob/main/DesignPattern/FactoryMethodDesignPatternFileProcess.py"
    click DesignPatterns "https://github.com/sarojgrs/python_demo_proj/tree/main/DesignPattern/FactoryMethodDesignGeneric/"
    click DesignPatterns "https://github.com/sarojgrs/python_demo_proj/blob/main/DesignPattern/SingletonDesignPattern.py"

    %% Integration Tier
    subgraph "Integration Tier"
        direction TB
        ExternalAPI["External APIs / Services"]:::external
    end

    Services -->|HTTP/REST calls| ExternalAPI

    %% Legend (optional)
    classDef frontend fill:#D0E6A5,stroke:#333,stroke-width:1px;
    classDef backend fill:#A0C4FF,stroke:#333,stroke-width:1px;
    classDef controller fill:#FFD6A5,stroke:#333,stroke-width:1px;
    classDef service fill:#CCCCCC,stroke:#333,stroke-width:1px;
    classDef pattern fill:#E76F51,stroke:#333,stroke-width:1px;
    classDef external fill:#8AC926,stroke:#333,stroke-width:1px;
```
