# datpham2001/user-service-ms architecture

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

- Interactive diagram: https://gitdiagram.com/datpham2001/user-service-ms
- Repository: https://github.com/datpham2001/user-service-ms

## Overview

##### Project type and purpose
- This is a Go‑based User Service Microservice focused on authentication, registration, and OAuth2 integration (Google).
- Exposes a RESTful HTTP API (Gin) and has a basic gRPC server scaffold.
- Integrates with external systems: relational database, cache store, OAuth2 provider (Google), and message broker (RabbitMQ).

##### File structure
- Top‑level binaries under cmd/:
– http_server.go (starts Gin HTTP server)
– grpc_server.go (starts gRPC interface)
– main.go (wire up config, logging, servers)
- API definitions in api/v1/: route registration for /api/v1/auth endpoints.
- Configuration under config/: YAML loader (Viper) and sample config.
- internal/: implements a layered (clean/hexagonal) architecture:
– delivery/http/auth: HTTP handlers and route wiring
– service/auth, service/token: business logic, JWT & OAuth2 flows
– repository/auth & repository/common: database CRUD & common DB operations
– infra/database, infra/cache, infra/rabbitmq: external resource clients
– middleware: logging, request ID injection
– dto: request/response models
– errors: custom error types
– client/oauth: OAuth2 client wrapper
- pkg/: reusable libraries (HTTP client, logger, response formatting).

##### README for additional insights
- Technology stack: Go, Gin, Viper, Logrus, PostgreSQL/Redis (configurable), Google OAuth, gRPC.
- Core features: signup/login, password validation, email format checks, OAuth2 flow, structured logging, error handling.

##### Detailed capture of essential elements
- Dependency injection: e.g., “main.go → NewAuthHandler(deps) → Handler”.
– Lane 1: Transport (HTTP/gRPC)
– Lane 2: Orchestration (Handlers)
– Lane 3: Business (Services)
– Lane 4: Data (Repos, Infra)
– Lane 5: External (DB, Cache, Google OAuth, RabbitMQ)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Bootstrap Layer
    subgraph "Bootstrap"
        direction TB
        Main["main.go"]:::configstyle
        ConfigLoader["config/config.go\nconfig/load.go"]:::configstyle
        Logger["pkg/logger/logger.go"]:::libstyle
    end

    Main -->|loads config| ConfigLoader
    Main -->|initializes logger| Logger

    %% Transport Layer
    subgraph "Transport Layer"
        direction TB
        HTTPServer["HTTP Server\n(Gin)\ncmd/http_server.go"]:::delivery
        GRPCServer["gRPC Server\ncmd/grpc_server.go"]:::delivery
        APIRouter["API Router\n/api/v1"]:::delivery
    end

    Main -->|wires| HTTPServer
    Main -->|wires| GRPCServer
    HTTPServer -->|routes| APIRouter
    GRPCServer -->|serves gRPC| AuthService

    %% Delivery Layer
    subgraph "Delivery Layer"
        direction TB
        AuthHandler["AuthHandler\ninternal/delivery/http/auth/auth_handler.go"]:::delivery
    end

    APIRouter -->|handles /api/v1/auth| AuthHandler

    %% Business Layer
    subgraph "Business Layer"
        direction TB
        AuthService["AuthService\ninternal/service/auth/auth_service.go"]:::service
        JWTService["JWTTokenService\ninternal/service/token/jwt_token_service.go"]:::service
        OAuth2Service["OAuth2Service\ninternal/service/token/oauth2.go"]:::service
    end

    AuthHandler -->|calls| AuthService
    AuthHandler -->|calls| OAuth2Service
    AuthService -->|generates tokens| JWTService
    GRPCServer -.->|invokes| AuthService

    %% Data Layer
    subgraph "Repository Layer"
        direction TB
        AuthRepo["AuthRepository\ninternal/repository/auth/auth_repository.go"]:::repo
        CommonRepo["CommonRepository\ninternal/repository/common/common_repository.go"]:::repo
    end

    AuthService -->|CRUD| AuthRepo
    AuthService -->|uses| CommonRepo

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        DBClient["Database Client\ninternal/infra/database/database.go"]:::infra
        CacheClient["Cache Client\ninternal/infra/cache/cache.go"]:::infra
        RabbitClient["RabbitMQ Client\ninternal/infra/rabbitmq/rabbitmq.go"]:::infra
    end

    AuthRepo -->|queries| DBClient
    CommonRepo -->|common ops| DBClient
    AuthService -->|cache tokens| CacheClient
    AuthService -->|publish events| RabbitClient
    OAuth2Service -->|HTTP calls| HTTPClient

    %% Libraries
    subgraph "Shared Libraries"
        direction TB
        HTTPClient["HTTP Client\npkg/httpclient/http_client.go"]:::libstyle
        ResponseFmt["Response Formatter\npkg/response/response.go"]:::libstyle
    end

    AuthHandler -->|formats| ResponseFmt

    %% External Services
    subgraph "External"
        direction TB
        GoogleOAuth["Google OAuth2\ninternal/client/oauth/client.go"]:::external
    end

    OAuth2Service -->|redirect/callback| GoogleOAuth

    %% Click Events
    click Main "https://github.com/datpham2001/user-service-ms/blob/main/cmd/main.go"
    click HTTPServer "https://github.com/datpham2001/user-service-ms/blob/main/cmd/http_server.go"
    click GRPCServer "https://github.com/datpham2001/user-service-ms/blob/main/cmd/grpc_server.go"
    click APIRouter "https://github.com/datpham2001/user-service-ms/blob/main/api/v1/api_registar.go"
    click APIRouter "https://github.com/datpham2001/user-service-ms/blob/main/api/v1/auth_api.go"
    click AuthHandler "https://github.com/datpham2001/user-service-ms/blob/main/internal/delivery/http/auth/auth_handler.go"
    click AuthService "https://github.com/datpham2001/user-service-ms/blob/main/internal/service/auth/auth_service.go"
    click JWTService "https://github.com/datpham2001/user-service-ms/blob/main/internal/service/token/jwt_token_service.go"
    click OAuth2Service "https://github.com/datpham2001/user-service-ms/blob/main/internal/service/token/oauth2.go"
    click AuthRepo "https://github.com/datpham2001/user-service-ms/blob/main/internal/repository/auth/auth_repository.go"
    click CommonRepo "https://github.com/datpham2001/user-service-ms/blob/main/internal/repository/common/common_repository.go"
    click DBClient "https://github.com/datpham2001/user-service-ms/blob/main/internal/infra/database/database.go"
    click CacheClient "https://github.com/datpham2001/user-service-ms/blob/main/internal/infra/cache/cache.go"
    click RabbitClient "https://github.com/datpham2001/user-service-ms/blob/main/internal/infra/rabbitmq/rabbitmq.go"
    click GoogleOAuth "https://github.com/datpham2001/user-service-ms/blob/main/internal/client/oauth/client.go"
    click ConfigLoader "https://github.com/datpham2001/user-service-ms/blob/main/config/config.go"
    click ConfigLoader "https://github.com/datpham2001/user-service-ms/blob/main/config/load.go"
    click Logger "https://github.com/datpham2001/user-service-ms/blob/main/pkg/logger/logger.go"
    click HTTPClient "https://github.com/datpham2001/user-service-ms/blob/main/pkg/httpclient/http_client.go"
    click ResponseFmt "https://github.com/datpham2001/user-service-ms/blob/main/pkg/response/response.go"

    %% Styles
    classDef delivery fill:#D0E8FF,stroke:#0288D1,stroke-width:1px
    classDef service fill:#D5F5D5,stroke:#27AE60,stroke-width:1px
    classDef repo fill:#FDEBD0,stroke:#D35400,stroke-width:1px
    classDef infra fill:#E8DAEF,stroke:#8E44AD,stroke-width:1px
    classDef external fill:#D6DBDF,stroke:#7F8C8D,stroke-width:1px
    classDef configstyle fill:#AED6F1,stroke:#21618C,stroke-width:1px
    classDef libstyle fill:#FCF3CF,stroke:#B7950B,stroke-width:1px
```
