# kappy713/sports_backend architecture

> Architecture diagram of the GitHub repository kappy713/sports_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/kappy713/sports_backend
- Repository: https://github.com/kappy713/sports_backend

## Overview

##### Project Context and Overview
- This backend service is part of a full-stack application aimed at helping users record their daily exercise for visual analysis and habit formation.
- The README specifies that it interacts with a separate frontend (hosted in a different repository) and uses Go as its backend language, PostgreSQL for data persistence, and Docker for containerization.
- The system supports typical RESTful interactions via HTTP, with clearly separated layers for domain logic, interfaces (controllers), and infrastructure (e.g., database, router, middleware).

##### Main Components
a. Client (Frontend)
– Although not part of this repository, note its role and external communication with the backend API.

b. API Gateway / Entry Point
– The “main.go” file is the entry point of the backend.
– This component initializes the application, sets up routing, and integrates middleware.

c. Domain Layer
– Located in the “domain” directory.
– Contains core business logic and domain models (e.g., posts.go, relationships.go, users.go).

d. Infrastructure Layer
– Contains modules for database connectivity (“infrastructure/db/db.go”), middleware (“infrastructure/middleware/middleware.go”), and routing (“infrastructure/router/router.go”).
– This layer is responsible for handling cross-cutting concerns like request processing and establishing database connections.

e. Interfaces Layer
– Found in “interfaces/controller” with various controllers (e.g., bookmark.go, get_post.go, mypage.go, post.go, user_controller.go).
– These controllers expose the API endpoints and serve as the bridge between HTTP requests and the underlying domain logic.

f. External Services and Dockerization
– Docker configuration files (Dockerfile and docker-compose.yml) signify that the app is containerized.
– PostgreSQL is used as the DB (as indicated by documentation instructions) and is set up via docker-compose alongside the backend container.
– External integrations include environmental configurations through a .env file and JWT secret configuration for security.

##### Architectural Patterns & Design Principles
- Layered Architecture / Hexagonal: Notice the clear separation between domain (business rules), interfaces (delivery mechanism), and infrastructure (external integrations).
- Separation of Concerns: Each directory provides a dedicated role (business logic in “domain”, data access/middleware in “infrastructure”, presentation/HTTP in “interfaces”).
- Containerization & DevOps Integration: Docker and docker-compose indicate a containerized deployment with isolated components (backend service and DB).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client & API Gateway Layer
    subgraph "Client & API Gateway"
        direction TB
        Client["Client (Frontend)"]:::external
        APIGateway["API Gateway (main.go)"]:::api
    end
    Client -->|"HTTPRequest"| APIGateway

    %% Infrastructure Layer for Request Processing
    subgraph "Infrastructure Layer"
        direction TB
        Middleware["Middleware (infrastructure/middleware/middleware.go)"]:::infra
        Router["Router (infrastructure/router/router.go)"]:::infra
    end
    APIGateway -->|"initMiddleware"| Middleware
    Middleware -->|"routeRequest"| Router

    %% Interfaces Layer (Controllers)
    subgraph "Interfaces Layer"
        direction TB
        Bookmark["Bookmark Controller (bookmark.go)"]:::controller
        GetPost["GetPost Controller (get_post.go)"]:::controller
        Mypage["Mypage Controller (mypage.go)"]:::controller
        Post["Post Controller (post.go)"]:::controller
        UserController["User Controller (user_controller.go)"]:::controller
    end
    Router -->|"dispatch"| Bookmark
    Router -->|"dispatch"| GetPost
    Router -->|"dispatch"| Mypage
    Router -->|"dispatch"| Post
    Router -->|"dispatch"| UserController

    %% Domain Layer (Business Logic)
    subgraph "Domain Layer"
        direction TB
        Posts["Posts (domain/posts.go)"]:::domain
        Relationships["Relationships (domain/relationships.go)"]:::domain
        Users["Users (domain/users.go)"]:::domain
    end
    Bookmark -->|"calls"| Posts
    GetPost -->|"calls"| Posts
    Post -->|"calls"| Posts
    Mypage -->|"calls"| Users
    UserController -->|"calls"| Users
    UserController -->|"calls"| Relationships

    %% Infrastructure Database Connector
    subgraph "Infrastructure - Database"
        direction TB
        DB["DB Connector (infrastructure/db/db.go)"]:::infra
    end
    Posts -->|"persists"| DB
    Users -->|"persists"| DB
    Relationships -->|"persists"| DB

    %% Docker & External Services
    subgraph "Docker & External Services"
        direction TB
        BackendContainer["Backend Container"]:::docker
        PGContainer["PostgreSQL Container"]:::docker
        Dockerfile["Dockerfile"]:::docker
        DockerCompose["docker-compose.yml"]:::docker
        Env[".env"]:::docker
    end
    Dockerfile -->|"builds"| BackendContainer
    DockerCompose -->|"configures"| BackendContainer
    DockerCompose -->|"configures"| PGContainer
    BackendContainer -->|"runs"| APIGateway
    DB -->|"connectsTo"| PGContainer

    %% Click Events for API Gateway / Entry Point and Infrastructure Components
    click APIGateway "https://github.com/kappy713/sports_backend/blob/main/main.go"
    click Middleware "https://github.com/kappy713/sports_backend/blob/main/infrastructure/middleware/middleware.go"
    click Router "https://github.com/kappy713/sports_backend/blob/main/infrastructure/router/router.go"
    click Posts "https://github.com/kappy713/sports_backend/blob/main/domain/posts.go"
    click Relationships "https://github.com/kappy713/sports_backend/blob/main/domain/relationships.go"
    click Users "https://github.com/kappy713/sports_backend/blob/main/domain/users.go"
    click DB "https://github.com/kappy713/sports_backend/blob/main/infrastructure/db/db.go"
    click Bookmark "https://github.com/kappy713/sports_backend/blob/main/interfaces/controller/bookmark.go"
    click GetPost "https://github.com/kappy713/sports_backend/blob/main/interfaces/controller/get_post.go"
    click Mypage "https://github.com/kappy713/sports_backend/blob/main/interfaces/controller/mypage.go"
    click Post "https://github.com/kappy713/sports_backend/blob/main/interfaces/controller/post.go"
    click UserController "https://github.com/kappy713/sports_backend/blob/main/interfaces/controller/user_controller.go"
    click Dockerfile "https://github.com/kappy713/sports_backend/tree/main/Dockerfile"
    click DockerCompose "https://github.com/kappy713/sports_backend/blob/main/docker-compose.yml"
    click Env "https://github.com/kappy713/sports_backend/blob/main/.env"

    %% Styling
    classDef external fill:#f9c,stroke:#333,stroke-width:2px;
    classDef api fill:#bbf,stroke:#333,stroke-width:2px;
    classDef infra fill:#afa,stroke:#333,stroke-width:2px;
    classDef controller fill:#9cf,stroke:#333,stroke-width:2px;
    classDef domain fill:#ff9,stroke:#333,stroke-width:2px;
    classDef docker fill:#fcc,stroke:#333,stroke-width:2px;
```
