# giyomdabin/moyak-server architecture

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

- Interactive diagram: https://gitdiagram.com/giyomdabin/moyak-server
- Repository: https://github.com/giyomdabin/moyak-server

## Overview

- Application Entry Point: Highlight "MoyakApplication.java" as the central starting point.
- Controllers:
- AuthController (handles user authentication endpoints)
- ChatController (manages chat-related endpoints)
- Services:
- AuthService (business logic for authentication, interacting with OAuth tokens and profiles)
- ChatService (core logic related to chat operations)
- ChatGptService (integration with ChatGPT API, handling AI-specific requests/responses)
- S3Service (file management, suggesting integration with AWS S3)
- Data Models and DTOs:
- DTOs for Chat, ChatGPT (request and response), Auth (KakaoProfile, OAuthToken), and User objects, as these structure the data flowing in the application.
- Repositories:
- ChatRepository, ChatMessageRepository, EachPillRepository, UserRepository (for persistence with the database)
- External Integrations:
- OAuth (Kakao) for authentication
- ChatGPT API for AI chat responses
- Amazon S3 for storage
- Configurations & Exception Handling:
- Config packages (WebConfig, S3Config) to show configuration of web settings and S3 connections.
- Exception packages (CustomException, CustomExceptionHandler, ErrorResponse) representing centralized error handling.

##### Relationships and Data Flow
- The Controller interacts with a Service layer that implements the core business logic.
- The Services then interact with Repositories to perform CRUD operations on the database.
- For functionalities like AI chat response or file uploads, Services interface with external APIs (ChatGPT and AWS S3).
- Flows for authentication where the AuthController communicates with an external OAuth provider (for example, Kakao) via models like KakaoProfile and OAuthToken.
- Exception handling as a cross-cutting concern that intercepts errors from any of these interactions.

##### Architectural Patterns and Design Principles
- Layered (MVC-like) Architecture: Clearly separate controllers, services, and repository layers.
- External API Integration: Treat services like ChatGptService and S3Service as integration adapters, encapsulating communication logic with third-party services.
- Dependency Injection: Implicit in the design (likely through Spring’s DI container) that connects controllers with their respective services.
- Exception Management: Centralized exception handling for better manageability and maintainability.

##### Additional Advice
- Data flow: It is crucial to represent how data moves from a request to processing and then storage or external API calls.

## Diagram (Mermaid)

```mermaid
flowchart TD 
    %% Client Interaction Layer
    subgraph "Client Interaction Layer"
        Client["Client (Web/Mobile)"]:::client
    end

    %% Application Bootstrap
    subgraph "Application Bootstrap"
        App["MoyakApplication"]:::application
    end

    %% Controller Layer
    subgraph "Controller Layer"
        AuthCtrl["AuthController"]:::controller
        ChatCtrl["ChatController"]:::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        AuthSvc["AuthService"]:::service
        ChatSvc["ChatService"]:::service
        ChatGPTSvc["ChatGptService"]:::service
        S3Svc["S3Service"]:::service
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        ChatRepo["ChatRepository"]:::repository
        ChatMsgRepo["ChatMessageRepository"]:::repository
        EachPillRepo["EachPillRepository"]:::repository
        UserRepo["UserRepository"]:::repository
    end

    %% Data Models & DTOs
    subgraph "Data Models & DTOs"
        KakaoProfile["KakaoProfile"]:::dto
        OAuthToken["OAuthToken"]:::dto
        ChatGptRequestDto["ChatGptRequestDto"]:::dto
        ChatGptResponseDto["ChatGptResponseDto"]:::dto
        Message["Message"]:::dto
    end

    %% External Integrations
    subgraph "External Integrations"
        OAuth["OAuth (Kakao)"]:::integration
        ChatGPTAPI["ChatGPT API"]:::integration
        AWSS3["AWS S3"]:::integration
    end

    %% Configurations & Exception Handling
    subgraph "Configurations & Exception Handling"
        Config["WebConfig & S3Config"]:::config
        Exception["CustomException, CustomExceptionHandler, ErrorResponse"]:::exception
    end

    %% Relationships
    Client -->|"HTTPRequest"| App
    App -->|"Initializes"| AuthCtrl
    App -->|"Initializes"| ChatCtrl

    AuthCtrl -->|"HandlesAuth"| AuthSvc
    ChatCtrl -->|"HandlesChat"| ChatSvc

    AuthSvc -->|"Uses"| KakaoProfile
    AuthSvc -->|"Uses"| OAuthToken
    AuthSvc -->|"PersistsWith"| UserRepo
    AuthSvc -->|"IntegratesWith"| OAuth

    ChatSvc -->|"Uses"| ChatGptRequestDto
    ChatSvc -->|"Uses"| ChatGptResponseDto
    ChatSvc -->|"Uses"| Message
    ChatSvc -->|"PersistsWith"| ChatRepo
    ChatSvc -->|"PersistsWith"| ChatMsgRepo
    ChatSvc -->|"PersistsWith"| EachPillRepo
    ChatSvc -->|"DelegatesTo"| ChatGPTSvc
    ChatSvc -->|"DelegatesTo"| S3Svc

    ChatGPTSvc -->|"Calls"| ChatGPTAPI
    S3Svc -->|"Calls"| AWSS3

    %% Cross-Cutting Configurations & Exception Handling
    AuthCtrl --- Exception
    ChatCtrl --- Exception
    AuthSvc --- Config
    ChatSvc --- Config

    %% Click Events
    click App "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/MoyakApplication.java"
    click AuthCtrl "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/auth/controller/AuthController.java"
    click ChatCtrl "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/chat/controller/ChatController.java"
    click AuthSvc "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/auth/service/AuthService.java"
    click ChatSvc "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/chat/service/ChatService.java"
    click ChatGPTSvc "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/ai/chatgpt/service/ChatGptService.java"
    click S3Svc "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/chat/service/S3Service.java"
    click KakaoProfile "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/auth/model/KakaoProfile.java"
    click OAuthToken "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/auth/model/OAuthToken.java"
    click ChatGptRequestDto "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/ai/chatgpt/dto/request/ChatGptRequestDto.java"
    click ChatGptResponseDto "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/ai/chatgpt/dto/response/ChatGptResponseDto.java"
    click Message "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/ai/chatgpt/dto/Message.java"
    click ChatRepo "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/chat/repository/ChatRepository.java"
    click ChatMsgRepo "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/chat/repository/ChatMessageRepository.java"
    click EachPillRepo "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/chat/repository/EachPillRepository.java"
    click UserRepo "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/user/repository/UserRepository.java"
    click Config "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/config/WebConfig.java"
    click Config "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/config/S3Config.java"
    click Exception "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/config/exception/CustomException.java"
    click Exception "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/config/exception/CustomExceptionHandler.java"
    click Exception "https://github.com/giyomdabin/moyak-server/blob/main/src/main/java/study/moyak/config/exception/ErrorResponse.java"

    %% Styles
    classDef client fill:#cce5ff,stroke:#0275d8,stroke-width:2px;
    classDef application fill:#dff0d8,stroke:#3c763d,stroke-width:2px;
    classDef controller fill:#f7c6c7,stroke:#a94442,stroke-width:2px;
    classDef service fill:#d9edf7,stroke:#31708f,stroke-width:2px;
    classDef repository fill:#fcf8e3,stroke:#8a6d3b,stroke-width:2px;
    classDef dto fill:#e2e2e2,stroke:#555555,stroke-width:2px;
    classDef integration fill:#f0e68c,stroke:#b8860b,stroke-width:2px;
    classDef config fill:#ffe4b5,stroke:#d2691e,stroke-width:2px;
    classDef exception fill:#f2dede,stroke:#a94442,stroke-width:2px;
```
