# go-park-mail-ru/2024_2_bettercallfirewall architecture

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

- Interactive diagram: https://gitdiagram.com/go-park-mail-ru/2024_2_bettercallfirewall
- Repository: https://github.com/go-park-mail-ru/2024_2_bettercallfirewall

## Overview

##### Main Components
- Service Components: The project is divided into several distinct service domains. Create separate blocks/rectangles for each of the primary services:
- Auth Service (located in cmd/auth and internal/auth)
- Chat Service (cmd/chat and internal/chat)
- Community Service (cmd/community and internal/community)
- File Service (cmd/file and internal/fileService)
- Post Service (cmd/post and internal/post)
- Profile Service (cmd/profile and internal/profile)
- API Gateways / Adapters: There are internal/api/grpc components for each respective service as well as an ext_grpc layer. These indicate that the system exposes gRPC endpoints along with possible HTTP routers (under internal/router).
- Infrastructure Components:
- Database layer: DB migrations, normalized documents (ERdiagram.md and relations.md) indicate the presence of a relational database (likely PostgreSQL).
- Cache/Session Management: The presence of Redis repository implementations under internal/auth/repository/redis.
- Metrics: The internal/metrics directory and prometheus.yml demonstrate monitoring and instrumentation.
- Infrastructure tooling: Dockerfiles per service (e.g., Dockerfileauth, Dockerfilepostgrpc, etc.) coupled with docker-compose.yml to manage containerized deployments.

##### Relationships and Interactions
- Data Flow and Service Communication:
- How frontend requests (although frontend is a separate repository) target the backend API layer. The backend is designed with HTTP endpoints (routers) and gRPC endpoints (internal/api/grpc) that serve business logic.
- Internal interactions; for example, some services might communicate with each other (e.g., auth for managing sessions and user validations), and several services interact with the database and external adapters.
- A connection between the gRPC adapter (internal/ext_grpc) and its respective port receiving requests from external services.
- Request Processing Layers:
- Use distinct layers representing the layers in the application: API/Router layer, Controller layer (e.g., inside internal/*/controller), Service/Application layer (inside internal/app/* or internal/*/service), and persistence (repository patterns under postgres directories).
- Middleware components (internal/middleware) that intercept and log or monitor requests (access log, metrics, auth).

##### Architectural Patterns & Design Principles
- Microservices/Fine-grained Componentization (even if services run within one codebase, the “cmd” structure suggests multiple executables, potentially deployed as separate containers). Emphasize this by drawing each service as an independent deployable component.
- Use of gRPC and REST: Since there are dedicated packages for gRPC, include distinct representations for endpoints providing remote procedure calls (using different Dockerfiles for gRPC) as well as HTTP endpoints.
- Observability: Clearly indicate components for metrics collection (Prometheus) and logging through the middleware.

##### Additional Notes
- That each service contains its own sublayers (control, service, repository) and that there are testing files (app_test.go, router_test.go) emphasizing testability.
- Mention the distinct Dockerfiles; these imply that each service may be deployed as its own container. Represent them within the infrastructure layer and show deployment relations via docker-compose.
- Including the “proto” files indicates that the overall system design should capture how APIs are defined for gRPC communication.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top-level: Client Request
    CR("Client Request"):::adapter

    %% API Gateways / Adapters
    subgraph "API Gateways"
        HTTP_Router("HTTP Router"):::adapter
        GRPC_External("gRPC External"):::adapter
        Internal_API("Internal API (gRPC)"):::adapter
    end

    CR -->|"to_HTTP"| HTTP_Router
    CR -->|"to_gRPC"| GRPC_External

    %% Services
    subgraph "Services"
        %% Auth Service
        subgraph "Auth Service"
            Auth_CMD("Command Layer"):::service
            Auth_Internal("Internal Logic"):::service
            Auth_gRPC("gRPC API"):::service
            Auth_Docker_HTTP("Docker: Auth (HTTP)"):::devops
            Auth_Docker_gRPC("Docker: Auth (gRPC)"):::devops
            Auth_CMD --> Auth_Internal
            Auth_Internal --> Auth_gRPC
            Auth_Internal --- Auth_Docker_HTTP
            Auth_gRPC --- Auth_Docker_gRPC
        end

        %% Chat Service
        subgraph "Chat Service"
            Chat_CMD("Command Layer"):::service
            Chat_Internal("Internal Logic"):::service
            Chat_Docker("Docker: Chat"):::devops
            Chat_CMD --> Chat_Internal
            Chat_Internal --- Chat_Docker
        end

        %% Community Service
        subgraph "Community Service"
            Community_CMD("Command Layer"):::service
            Community_Internal("Internal Logic"):::service
            Community_Docker("Docker: Community"):::devops
            Community_CMD --> Community_Internal
            Community_Internal --- Community_Docker
        end

        %% File Service
        subgraph "File Service"
            File_CMD("Command Layer"):::service
            File_Internal("Internal Logic"):::service
            File_Docker("Docker: File"):::devops
            File_CMD --> File_Internal
            File_Internal --- File_Docker
        end

        %% Post Service
        subgraph "Post Service"
            Post_CMD("Command Layer"):::service
            Post_Internal("Internal Logic"):::service
            Post_gRPC("gRPC API"):::service
            Post_Docker_HTTP("Docker: Post (HTTP)"):::devops
            Post_Docker_gRPC("Docker: Post (gRPC)"):::devops
            Post_CMD --> Post_Internal
            Post_Internal --> Post_gRPC
            Post_Internal --- Post_Docker_HTTP
            Post_gRPC --- Post_Docker_gRPC
        end

        %% Profile Service
        subgraph "Profile Service"
            Profile_CMD("Command Layer"):::service
            Profile_Internal("Internal Logic"):::service
            Profile_GRPC("gRPC API"):::service
            Profile_Docker_HTTP("Docker: Profile (HTTP)"):::devops
            Profile_Docker_GRPC("Docker: Profile (gRPC)"):::devops
            Profile_CMD --> Profile_Internal
            Profile_Internal --> Profile_GRPC
            Profile_Internal --- Profile_Docker_HTTP
            Profile_GRPC --- Profile_Docker_GRPC
        end
    end

    %% Connections from API Gateways to Services
    HTTP_Router -->|"routes_to"| Auth_CMD
    HTTP_Router -->|"routes_to"| Chat_CMD
    HTTP_Router -->|"routes_to"| Community_CMD
    HTTP_Router -->|"routes_to"| File_CMD
    HTTP_Router -->|"routes_to"| Post_CMD
    HTTP_Router -->|"routes_to"| Profile_CMD

    GRPC_External -->|"calls"| Auth_gRPC
    GRPC_External -->|"calls"| Post_gRPC
    GRPC_External -->|"calls"| Profile_GRPC
    Internal_API ---|"internal_calls"| Auth_gRPC
    Internal_API ---|"internal_calls"| Post_gRPC
    Internal_API ---|"internal_calls"| Profile_GRPC

    %% Sample Inter-service Communication
    Post_Internal -- "user_details" --> Profile_Internal

    %% Infrastructure Components
    subgraph "Infrastructure"
        Database("PostgreSQL Database"):::infra
        RedisCache("Redis Cache"):::infra
        Metrics("Prometheus Metrics"):::infra
    end

    %% Data flow from Services to Infrastructure
    Auth_Internal -->|"DB_access"| Database
    Chat_Internal -->|"DB_access"| Database
    Community_Internal -->|"DB_access"| Database
    File_Internal -->|"DB_access"| Database
    Post_Internal -->|"DB_access"| Database
    Profile_Internal -->|"DB_access"| Database

    Auth_Internal -- "session_store" --> RedisCache

    %% Metrics connection from API layer
    HTTP_Router ---|"metrics"| Metrics

    %% DevOps / Containerization
    subgraph "DevOps"
        DockerCompose("Docker Compose"):::devops
    end

    %% Connect each Docker component to Docker Compose
    Auth_Docker_HTTP --- DockerCompose
    Auth_Docker_gRPC --- DockerCompose
    Chat_Docker --- DockerCompose
    Community_Docker --- DockerCompose
    File_Docker --- DockerCompose
    Post_Docker_HTTP --- DockerCompose
    Post_Docker_gRPC --- DockerCompose
    Profile_Docker_HTTP --- DockerCompose
    Profile_Docker_GRPC --- DockerCompose

    %% Click Events for Auth Service components
    click Auth_CMD "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/cmd/auth"
    click Auth_Internal "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/auth"
    click Auth_gRPC "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/api/grpc/auth_api"
    click Auth_Docker_HTTP "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfileauth"
    click Auth_Docker_gRPC "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfileauthgrpc"

    %% Click Events for Chat Service components
    click Chat_CMD "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/cmd/chat"
    click Chat_Internal "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/chat"
    click Chat_Docker "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfilechat"

    %% Click Events for Community Service components
    click Community_CMD "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/cmd/community"
    click Community_Internal "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/community"
    click Community_Docker "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfilecommunity"

    %% Click Events for File Service components
    click File_CMD "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/cmd/file"
    click File_Internal "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/fileService"
    click File_Docker "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfilefile"

    %% Click Events for Post Service components
    click Post_CMD "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/cmd/post"
    click Post_Internal "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/post"
    click Post_gRPC "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/api/grpc/post_api"
    click Post_Docker_HTTP "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfilepost"
    click Post_Docker_gRPC "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfilepostgrpc"

    %% Click Events for Profile Service components
    click Profile_CMD "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/cmd/profile"
    click Profile_Internal "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/profile"
    click Profile_GRPC "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/api/grpc/profile_api"
    click Profile_Docker_HTTP "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfileprofile"
    click Profile_Docker_GRPC "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/Dockerfileprofilegrpc"

    %% Click Events for API Gateways / Adapters
    click HTTP_Router "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/router"
    click GRPC_External "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/ext_grpc"
    click Internal_API "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/api/grpc"

    %% Click Events for Infrastructure
    click Database "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/DB"
    click RedisCache "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/auth/repository/redis"
    click Metrics "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/tree/main/internal/metrics"

    %% Click Event for DevOps
    click DockerCompose "https://github.com/go-park-mail-ru/2024_2_bettercallfirewall/blob/main/docker-compose.yml"

    %% Styles
    classDef service fill:#D0E6FF,stroke:#1A3E72,stroke-width:2px;
    classDef adapter fill:#FFF0B3,stroke:#B35D00,stroke-width:2px;
    classDef infra fill:#CCE8CC,stroke:#238A3B,stroke-width:2px;
    classDef devops fill:#F2D7D9,stroke:#9E2A2B,stroke-width:2px;
```
