# mutual-de-servicios-al-policia/microservice-gateway architecture

> Architecture diagram of the GitHub repository mutual-de-servicios-al-policia/microservice-gateway, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mutual-de-servicios-al-policia/microservice-gateway
- Repository: https://github.com/mutual-de-servicios-al-policia/microservice-gateway

## Overview

Based on our analysis of the file tree and README content, this project is a microservice gateway built with NestJS. Its purpose is to act as a central point that routes requests and communicates with various microservices via NATS (a lightweight messaging system). The codebase is organized using NestJS modules, controllers, and services, separating functionality into logical areas (such as affiliates, auth, general, kiosk, and persons). There’s also an integration with a Postgres database (as suggested by the database and migrations folders) and configurations for logging and environment variables.

##### Main Components
- Microservice Gateway (Core Application): This is the central application, built in NestJS. It is represented by src/app.module.ts and other feature modules (affiliates, auth, general, kiosk, persons).
- Feature Modules: Represent each domain by a separate module (e.g., affiliates, auth, persons, general, kiosk). Each one hosts its own controllers and services.
- Database: Represent the persistence layer (extracted from the src/database folder and associated migration files). Although this is not explicitly defined in the README, it is a core part of the data handling logic.
- External Microservices: Indicate the external microservices that need to be running (as mentioned in the README step “Tener levantados los microservicios que se van a consumir”). They communicate with the gateway via NATS.

##### Relationships and Interactions
- That external clients (or other consumers) interact with the Microservice Gateway through HTTP endpoints.
- The gateway then delegates various operations to its modules (auth, persons, etc.).
- Important: illustrate that the gateway uses NATS for asynchronous messaging to communicate with external microservices, forming a service mesh where messages are exchanged.
- That the Database is accessed by one or more of these modules (tracked in the src/database folder) for persistence and query operations.
- Optionally, include configuration and logging components (e.g., winston-audit-logger.ts) to show supporting services that help in diagnostics and monitoring.

##### Architectural Patterns and Design Principles
- Use of NestJS modular architecture, showing the separation of concerns via feature modules.
- Emphasis on microservices-based design using a message broker (NATS) for inter-service communication.
- Dockerization: Include Docker images (dockerfile, dockerfile.prod, and .dockerignore) that indicate containerization for development and production.
- Environment-driven configuration through .env files and configuration modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Interface Layer
    subgraph "Client Interface"
        Client["Clients"]:::external
    end

    %% Business Logic Layer (Core Application and Feature Modules)
    subgraph "Business Logic Layer"
        Gateway["Microservice Gateway (NestJS App)"]:::coreApp
        subgraph "Feature Modules"
            Affiliates["Affiliates Module"]:::module
            Auth["Auth Module"]:::module
            General["General Module"]:::module
            Kiosk["Kiosk Module"]:::module
            Persons["Persons Module"]:::module
        end
    end

    %% Messaging Layer
    subgraph "Messaging Layer"
        NATS["NATS Server"]:::external
    end

    %% External Microservices Layer
    subgraph "External Microservices"
        External["External Microservices"]:::external
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        Database["Database (Postgres)"]:::database
    end

    %% Configuration & Logging Layer
    subgraph "Configuration & Logging"
        Env["Environment Config (.env)"]:::config
        Logger["Winston Logger"]:::config
    end

    %% Containerization / Deployment Layer
    subgraph "Containerization"
        NATS_Docker["NATS Docker Config"]:::docker
    end

    %% Arrows and Relationships
    Client -->|"HTTP_request"| Gateway
    Gateway -->|"routes_to"| Affiliates
    Gateway -->|"routes_to"| Auth
    Gateway -->|"routes_to"| General
    Gateway -->|"routes_to"| Kiosk
    Gateway -->|"routes_to"| Persons

    Gateway -->|"publishes_to"| NATS
    NATS -->|"forwards_to"| External
    Gateway -->|"db_operations"| Database
    Gateway -->|"uses"| Env
    Gateway -->|"logs_with"| Logger
    NATS --> NATS_Docker

    %% Click Events
    click Gateway "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/app.module.ts"
    click Affiliates "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/affiliates/affiliates.module.ts"
    click Auth "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/auth/auth.module.ts"
    click General "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/general/general.module.ts"
    click Kiosk "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/kiosk/kiosk.module.ts"
    click Persons "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/persons/persons.module.ts"
    click NATS "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/common/nats/nats.service.ts"
    click Database "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/database/database.module.ts"
    click Env "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/.env.template"
    click Logger "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/blob/main/src/config/winston-audit-logger.ts"
    click NATS_Docker "https://github.com/mutual-de-servicios-al-policia/microservice-gateway/tree/main/nats/dockerfile"

    %% Styles
    classDef coreApp fill:#a2d9ce,stroke:#1abc9c,stroke-width:2px;
    classDef module fill:#d4e6f1,stroke:#2874a6,stroke-width:2px;
    classDef external fill:#fcf3cf,stroke:#f1c40f,stroke-width:2px;
    classDef database fill:#fadbd8,stroke:#e74c3c,stroke-width:2px;
    classDef config fill:#f9e79f,stroke:#f1c40f,stroke-width:2px;
    classDef docker fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
```
