# djandres04/backend1 architecture

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

- Interactive diagram: https://gitdiagram.com/djandres04/backend1
- Repository: https://github.com/djandres04/backend1

## Overview

##### Project type and purpose
- This is a single-service backend written in Python using Flask. Its primary role is to expose REST endpoints (Home, Devices, Door, Light, Temperature), handle authentication via JWT, perform business logic, interact with a database, and return JSON messages.

##### File structure
- Top-level:
- .dockerignore, requirements.txt, README.md
- src/ – all application code
- src/ contains:
- app.py – Flask application entry point
- config.py – reads environment variables (SECRET_KEY, JWT_KEY, DB_* variables, SERVER_REST)
- database/db.py – database connection and session management
- models/ – ORM models and entity definitions (ModelDevices, ModelUser, entities/User)
- routes/ – route handlers, one module per resource (DevicesRoute, DoorRoute, HomeRoute, LightRoute, TemperatureRoute)
- utils/ – cross-cutting utilities (AuthenticateJWT, ConverterTime, Encrypt, JsonMessage, token)

##### README for additional insights
- Describes installation, environment variables, Python/Flask versions.

a. Main components
1. External Clients – any consumer (web/mobile/IoT dashboards, curl/Postman)
2. Flask Application
– app.py and config.py initialize server and load env vars
3. API Layer (Routes)
– route modules map HTTP verbs and paths to controller logic
4. Authentication Middleware
– AuthenticateJWT utility intercepts protected routes
5. Business Logic / Services
– Model classes (ModelDevices, ModelUser) orchestrate operations
– ConverterTime, Encrypt, token, JsonMessage utilities
6. Data Layer
– database/db.py manages engine, sessions
– ORM entities in models/entities
7. Persistence Store
– External database (hosted per DB_HOST, DB_PORT, etc.)

b. Relationships and interactions
- External Client → HTTP request → Flask app
- Flask app loads config → dispatches to Routes
- Route entry → (optional) Authentication via AuthenticateJWT → passes user context
- Route handler calls Model* classes + utils
- Model layer uses database/db.py session to query or persist ORM entities
- Utilities (Encrypt, ConverterTime, token, JsonMessage) support data transformation, token generation, payload formatting
- Model returns result → Route formats JSON response → Flask returns HTTP response to client

c. Architectural patterns and design principles
- Layered architecture: Presentation (API/Routes), Business (Models + Utils), Data Access (db.py + Entities), Config
- Separation of concerns: each directory focuses on one responsibility
- Stateless JWT authentication
- Dependency injection of configuration via environment variables
- Modularization by resource (one route file per domain object)

d. Key technologies
- Python 3.8+, Flask 1.1.2+
- JWT for auth (AuthenticateJWT, token utils)
- SQLAlchemy or similar ORM in database/db.py (inferred)
- Custom utilities for encryption, time conversion, JSON message formatting

##### Detail
- Config.py feeding secrets/DB settings into Flask app at startup.
- Utilities as shared services connected to the business layer.
- Entire request-response cycle, from HTTP entry to DB transaction to JSON response.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["External Client\n(Web/Mobile/IoT)"]:::external
    subgraph "Backend Service"
        subgraph "Config & App"
            Config["config.py"]:::config
            App["app.py"]:::app
        end
        subgraph "API Layer"
            Home["HomeRoute"]:::route
            Devices["DevicesRoute"]:::route
            Door["DoorRoute"]:::route
            Light["LightRoute"]:::route
            Temp["TemperatureRoute"]:::route
        end
        Auth["AuthenticateJWT"]:::middleware
        subgraph "Business Logic / Models"
            MD["ModelDevices"]:::model
            MU["ModelUser"]:::model
            UE["User Entity"]:::entity
        end
        subgraph "Utilities"
            CT["ConverterTime"]:::util
            ENC["Encrypt"]:::util
            JSONM["JsonMessage"]:::util
            TOKEN["token"]:::util
        end
        subgraph "Data Layer"
            DBLayer["db.py"]:::datalayer
        end
    end
    DB["External Database"]:::storage

    Client -->|"HTTP Request"| App
    Config -->|"loads env vars"| App
    App -->|"routes to"| Home
    App -->|"routes to"| Devices
    App -->|"routes to"| Door
    App -->|"routes to"| Light
    App -->|"routes to"| Temp

    Home -->|"protects via"| Auth
    Devices -->|"protects via"| Auth
    Door -->|"protects via"| Auth
    Light -->|"protects via"| Auth
    Temp -->|"protects via"| Auth

    Auth -->|"calls"| MD
    Auth -->|"calls"| MU

    MD -->|"uses entity"| UE
    MU -->|"uses entity"| UE

    MD -->|"DB Session"| DBLayer
    MU -->|"DB Session"| DBLayer

    DBLayer -->|"connects to"| DB

    MD -->|"utilizes"| CT
    MD -->|"utilizes"| ENC
    MD -->|"utilizes"| JSONM
    MD -->|"utilizes"| TOKEN
    MU -->|"utilizes"| CT
    MU -->|"utilizes"| ENC
    MU -->|"utilizes"| JSONM
    MU -->|"utilizes"| TOKEN

    Home -->|"JSON Response"| Client
    Devices -->|"JSON Response"| Client
    Door -->|"JSON Response"| Client
    Light -->|"JSON Response"| Client
    Temp -->|"JSON Response"| Client

    click App "https://github.com/djandres04/backend1/blob/main/src/app.py"
    click Config "https://github.com/djandres04/backend1/blob/main/src/config.py"
    click DBLayer "https://github.com/djandres04/backend1/blob/main/src/database/db.py"
    click MD "https://github.com/djandres04/backend1/blob/main/src/models/ModelDevices.py"
    click MU "https://github.com/djandres04/backend1/blob/main/src/models/ModelUser.py"
    click UE "https://github.com/djandres04/backend1/blob/main/src/models/entities/User.py"
    click Home "https://github.com/djandres04/backend1/blob/main/src/routes/HomeRoute.py"
    click Devices "https://github.com/djandres04/backend1/blob/main/src/routes/DevicesRoute.py"
    click Door "https://github.com/djandres04/backend1/blob/main/src/routes/DoorRoute.py"
    click Light "https://github.com/djandres04/backend1/blob/main/src/routes/LightRoute.py"
    click Temp "https://github.com/djandres04/backend1/blob/main/src/routes/TemperatureRoute.py"
    click Auth "https://github.com/djandres04/backend1/blob/main/src/utils/AuthenticateJWT.py"
    click CT "https://github.com/djandres04/backend1/blob/main/src/utils/ConverterTime.py"
    click ENC "https://github.com/djandres04/backend1/blob/main/src/utils/Encrypt.py"
    click JSONM "https://github.com/djandres04/backend1/blob/main/src/utils/JsonMessage.py"
    click TOKEN "https://github.com/djandres04/backend1/blob/main/src/utils/token.py"

    classDef external fill:#FFD700,stroke:#333,stroke-width:1px
    classDef app fill:#87CEFA,stroke:#333,stroke-width:1px
    classDef config fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef route fill:#98FB98,stroke:#333,stroke-width:1px
    classDef middleware fill:#FFA07A,stroke:#333,stroke-width:1px
    classDef model fill:#E6E6FA,stroke:#333,stroke-width:1px
    classDef entity fill:#D8BFD8,stroke:#333,stroke-width:1px
    classDef util fill:#F0E68C,stroke:#333,stroke-width:1px
    classDef datalayer fill:#B0C4DE,stroke:#333,stroke-width:1px
    classDef storage fill:#8FBC8F,stroke:#333,stroke-width:1px
```
