# mbeka02/lyra_backend architecture

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

- Interactive diagram: https://gitdiagram.com/mbeka02/lyra_backend
- Repository: https://github.com/mbeka02/lyra_backend

## Overview

##### Main Components
- Entry Point: Represent the cmd/server as the primary executable, which bootstraps the entire service.
- The HTTP handler layer (found in internal/server/handler) managing routes for appointments, availability, doctors, patients, payments, and users.
- Middleware for authentication (internal/server/middleware/auth).
- Routing setup (internal/server/routes.go) that maps endpoints to handlers.
- Service Layer: Include a distinct component for the internal/server/service where business logic is implemented for each major domain (appointments, availability, doctor, patient, payment, and user services).
- Repository Layer: Add a component for the internal/server/repository where data access logic resides. This layer interacts with the database models and executes queries.
- Database Layer: Emphasize the internal/database and associated SQL queries (in sql/queries and sql/schema). Note that this layer handles data models, migrations, and SQL generation (through tools like sqlc as inferred from sqlc.yaml).
- External Integrations:
- Object Storage: Represent the internal/objstore component handling storage (likely leveraging Google Cloud based on “gcloud.go”).
- Mailer: Include the internal/mailer module as the service sending out emails.
- Configuration: Include the configuration module (config/viper.go) that reads environment variables and provides settings across the system.

##### Relationships and Data Flow
- How the service layer calls the repository layer to interact with the Database.
- Interactions between the authentication module and both the API and service layers.
- Interactions from services to external integrations like the Mailer, Payment Gateway, and Object Storage.
- Optionally, indicate the flow between configuration and all major components that rely on environment setups (e.g., initialization routines in the main function).

##### Architectural Patterns and Design Principles
- Separation of Concerns: Emphasize three main layers—API/Handler layer, Service/Business Logic layer, and Repository/Data Access layer.
- Clean/Hexagonal Architecture: Indicate that components like authentication, payment, and mailer, though external to the main service logic, are injected into the core functionality, supporting modularity and testability.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Initialization Layer
    subgraph "Initialization Layer"
        EP("Entry Point (cmd/server)"):::entry
        CFG("Configuration"):::config
    end

    %% API/HTTP Layer
    subgraph "API/HTTP Layer"
        HH("HTTP Handlers"):::api
        AM("Auth Middleware"):::api
        RT("Routing Setup"):::api
    end

    %% Service Layer
    subgraph "Service Layer"
        SV("Service Layer"):::service
    end

    %% Repository Layer
    subgraph "Repository Layer"
        RP("Repository Layer"):::repository
    end

    %% Authentication Module
    subgraph "Authentication Module"
        AU("Authentication (JWT/PASETO)"):::auth
    end

    %% Database Layer
    subgraph "Database Layer"
        DB("Database"):::database
    end

    %% External Integrations
    subgraph "External Integrations"
        OS("Object Storage"):::external
        PY("Payment Processing"):::external
        ML("Mailer"):::external
    end

    %% Utility Layer
    subgraph "Utility Layer"
        UT("Utility Functions"):::utility
    end

    %% Connections between layers
    EP --> CFG
    EP --> HH
    CFG --> HH
    CFG --> SV
    CFG --> RP
    CFG --> AU
    CFG --> DB
    CFG --> UT

    HH --> AM
    HH --> RT
    RT --> SV
    AM --> AU

    SV --> RP
    RP --> DB

    SV --> AU

    SV --> OS
    SV --> PY
    SV --> ML

    %% Click Events
    click EP "https://github.com/mbeka02/lyra_backend/blob/main/cmd/server/main.go"
    click HH "https://github.com/mbeka02/lyra_backend/tree/main/internal/server/handler"
    click AM "https://github.com/mbeka02/lyra_backend/blob/main/internal/server/middleware/auth.go"
    click RT "https://github.com/mbeka02/lyra_backend/blob/main/internal/server/routes.go"
    click SV "https://github.com/mbeka02/lyra_backend/tree/main/internal/server/service"
    click RP "https://github.com/mbeka02/lyra_backend/tree/main/internal/server/repository"
    click AU "https://github.com/mbeka02/lyra_backend/tree/main/internal/auth"
    click DB "https://github.com/mbeka02/lyra_backend/tree/main/internal/database"
    click OS "https://github.com/mbeka02/lyra_backend/tree/main/internal/objstore"
    click PY "https://github.com/mbeka02/lyra_backend/tree/main/internal/payment"
    click ML "https://github.com/mbeka02/lyra_backend/tree/main/internal/mailer"
    click UT "https://github.com/mbeka02/lyra_backend/tree/main/internal/util"
    click CFG "https://github.com/mbeka02/lyra_backend/blob/main/config/viper.go"

    %% Styles
    classDef entry fill:#f9f,stroke:#333,stroke-width:2px;
    classDef config fill:#ddf,stroke:#333,stroke-width:2px;
    classDef api fill:#bbf,stroke:#333,stroke-width:2px;
    classDef service fill:#bfb,stroke:#333,stroke-width:2px;
    classDef repository fill:#ffe,stroke:#333,stroke-width:2px;
    classDef auth fill:#fdd,stroke:#333,stroke-width:2px;
    classDef database fill:#ccc,stroke:#333,stroke-width:2px,stroke-dasharray: 5, 5;
    classDef external fill:#dfd,stroke:#333,stroke-width:2px;
    classDef utility fill:#fed,stroke:#333,stroke-width:2px;
```
