# nsaltun/bitirici architecture

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

- Interactive diagram: https://gitdiagram.com/nsaltun/bitirici
- Repository: https://github.com/nsaltun/bitirici

## Overview

##### Main components of the system
- Entry Point: The "cmd" directory, specifically the main.go file, acts as the application's starting point.
- Controllers: Located in the "internal/controller" package, these handle the incoming requests and determine which business processes to trigger.
- Middleware: Found in the "internal/middleware" directory (e.g., auth_middleware.go), these components manage request authentication and other cross-cutting concerns.
- Models: The "internal/model" package defines the core data structures (e.g., user.go) that are used throughout the application.
- Services: The "internal/service" package contains the business logic (e.g., service.go, user_service.go) which orchestrates operations using the model and repository layers.
- Repositories: Under "internal/repository" and supplemented by "lib/db", these components abstract the data persistence layer (e.g., postgres integration via postgre.go and pg_repository.go).
- Library Components: The "lib/logging" package is used for application-wide logging, while other "lib" segments provide important infrastructure integrations.
- Shared/Utility Resources: The "pkg" directory houses utility functions or shared resources (here, for instance, resource.go) that may be consumed across other layers.
- Deployment & Configuration: Files like Dockerfile, docker-compose.yml, and the environment configuration files in "cmd/local_config.env" along with deployment yaml files indicate the project’s readiness for containerized deployment. The "deploy" directory organizes environment-specific configurations.
- Build and Dependency Management: Presence of go.mod, go.sum, and Makefile indicate dependency management and build automation.

##### Relationships and interactions
- The application initialization in "cmd/main.go" is responsible for bootstrapping and wiring the different components (e.g., router initialization from "internal/router/router.go").
- The router directs incoming HTTP requests to the appropriate controllers. The controllers perform any necessary request validation and then delegate the business logic to service layers.
- The services process business rules and then utilize the repository layer to interact with the database. This data interaction is abstracted further in the "lib/db" package where actual Postgres queries are executed.
- Middleware (e.g., authentication middleware) is integrated within the request pipeline to secure endpoints, ensuring that requests reach the controller only after passing the necessary checks.
- Abstract logging is handled by components in "lib/logging", ensuring that operations across the entire stack are properly logged for debugging and monitoring.

##### Important architectural patterns and design principles
- Layered/MVC architecture: Clear separation exists between request handling, business logic, data persistence, and cross-cutting concerns.
- Dependency Injection & Modular Design: Although not explicitly detailed, the structure implies that dependency injection is used to pass repositories into services, and services into controllers.
- Containerization: Through Dockerfile and docker-compose.yml, the project is designed to be containerized for consistent deployment across environments.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A("Client Request"):::client
    B("Entry Point (cmd/main.go)"):::entry
    C("Router (internal/router)"):::router
    D("Auth Middleware (internal/middleware/auth_middleware.go)"):::middleware

    subgraph "Controllers (internal/controller)"
        E("Generic Controller (internal/controller/controller.go)"):::controller
        F("User Controller (internal/controller/user.go)"):::controller
    end

    subgraph "Services (internal/service)"
        G("Generic Service (internal/service/service.go)"):::service
        H("User Service (internal/service/user_service.go)"):::service
    end

    subgraph "Repositories"
        I("Repository (internal/repository/repository.go)"):::repository
        J("User Repository (internal/repository/user.go)"):::repository
        K("PG Repository (lib/db/pg_repository.go)"):::repository
        L("Postgre (lib/db/postgre.go)"):::repository
    end

    M("Database"):::db

    N("Model: User (internal/model/user.go)"):::model

    O("Logging (lib/logging/logging.go)"):::logging

    P("Resource (pkg/resource.go)"):::shared

    subgraph "Deployment & Configuration"
        Q("Dockerfile"):::config
        R("docker-compose.yml"):::config
        S("Local Config (cmd/local_config.env)"):::config
        T("Deploy Dev (deploy/dev)"):::config
        U("Deploy Prod (deploy/prod)"):::config
    end

    subgraph "Build & Dependency"
        V("Go Modules (go.mod)"):::build
        W("Go Sum (go.sum)"):::build
        X("Makefile"):::build
    end

    A --> B
    B --> C
    C --> D
    D --> E
    D --> F

    E --> G
    F --> H

    G --> I
    H --> J

    I --> K
    J --> L

    I --> M
    J --> M
    K --> M
    L --> M

    B -->|initLogging| O
    B -->|initResources| P
    B -->|loadModels| N
    S --> B

    click B "https://github.com/nsaltun/bitirici/blob/main/cmd/main.go"
    click E "https://github.com/nsaltun/bitirici/blob/main/internal/controller/controller.go"
    click F "https://github.com/nsaltun/bitirici/blob/main/internal/controller/user.go"
    click D "https://github.com/nsaltun/bitirici/blob/main/internal/middleware/auth_middleware.go"
    click N "https://github.com/nsaltun/bitirici/blob/main/internal/model/user.go"
    click G "https://github.com/nsaltun/bitirici/blob/main/internal/service/service.go"
    click H "https://github.com/nsaltun/bitirici/blob/main/internal/service/user_service.go"
    click I "https://github.com/nsaltun/bitirici/blob/main/internal/repository/repository.go"
    click J "https://github.com/nsaltun/bitirici/blob/main/internal/repository/user.go"
    click K "https://github.com/nsaltun/bitirici/blob/main/lib/db/pg_repository.go"
    click L "https://github.com/nsaltun/bitirici/blob/main/lib/db/postgre.go"
    click O "https://github.com/nsaltun/bitirici/blob/main/lib/logging/logging.go"
    click P "https://github.com/nsaltun/bitirici/blob/main/pkg/resource.go"
    click Q "https://github.com/nsaltun/bitirici/tree/main/Dockerfile"
    click R "https://github.com/nsaltun/bitirici/blob/main/docker-compose.yml"
    click S "https://github.com/nsaltun/bitirici/blob/main/cmd/local_config.env"
    click T "https://github.com/nsaltun/bitirici/tree/main/deploy/dev"
    click U "https://github.com/nsaltun/bitirici/tree/main/deploy/prod"
    click V "https://github.com/nsaltun/bitirici/blob/main/go.mod"
    click W "https://github.com/nsaltun/bitirici/blob/main/go.sum"
    click X "https://github.com/nsaltun/bitirici/tree/main/Makefile"

    classDef client fill:#ffefd5,stroke:#333,stroke-width:2px
    classDef entry fill:#f9d423,stroke:#333,stroke-width:2px
    classDef router fill:#aed6f1,stroke:#333,stroke-width:2px
    classDef middleware fill:#f7dc6f,stroke:#333,stroke-width:2px
    classDef controller fill:#85c1e9,stroke:#333,stroke-width:2px
    classDef service fill:#82e0aa,stroke:#333,stroke-width:2px
    classDef repository fill:#f5b7b1,stroke:#333,stroke-width:2px
    classDef db fill:#d7bde2,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5
    classDef model fill:#e8daef,stroke:#333,stroke-width:2px
    classDef logging fill:#d6eaf8,stroke:#333,stroke-width:2px
    classDef shared fill:#ebf5fb,stroke:#333,stroke-width:2px
    classDef config fill:#f9e79f,stroke:#333,stroke-width:2px
    classDef build fill:#a9cce3,stroke:#333,stroke-width:2px
```
