# swm-fire/modoco-backend architecture

> Architecture diagram of the GitHub repository swm-fire/modoco-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/swm-fire/modoco-backend
- Repository: https://github.com/swm-fire/modoco-backend

## Overview

##### Main Components

a) Client Layer (External)
- Web Clients
- Mobile Clients (if any)

b) API Gateway Layer
- HTTP REST API Server (NestJS)
- WebSocket Server (Socket.IO)
- Swagger Documentation

c) Core Service Layer
- Authentication Service (Multiple OAuth providers)
- User Management Service
- Room Management Service
- Friends Service
- Session Management Service
- Email Service
- Records Service

d) Real-time Communication Layer
- WebSocket Gateways
- Room Gateway
- Lobby Gateway
- Playlist Gateway

e) Data Storage Layer
- PostgreSQL Database (via Prisma ORM)
- Redis (for session and real-time features)

f) External Services
- Kakao OAuth
- Google OAuth
- GitHub OAuth
- AWS SES (for email)

##### Relationship Flows to Highlight

- WebSocket connections between clients and Socket.IO server
- Database relationships with Prisma as the intermediary
- Redis connections for real-time features and caching
- Authentication flows with external OAuth providers

##### Important Details

- NestJS Modules structure
- Prisma ORM integration
- Redis session and message stores
- WebSocket event flow
- Authentication flow with multiple providers
- Database schema relationships (high-level)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Clients
        WC[Web Clients]
        MC[Mobile Clients]
    end

    subgraph "API Gateway"
        API["HTTP REST API Server"]:::api
        WS["WebSocket Server"]:::websocket
        SW["Swagger Documentation"]:::docs
    end
    click API "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/main.ts"
    click SW "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/config/swagger-config.ts"

    subgraph "Core Services"
        AS["Authentication Service"]:::service
        US["User Management"]:::service
        RS["Room Management"]:::service
        FS["Friends Service"]:::service
        SS["Session Management"]:::service
        ES["Email Service"]:::service
        RCS["Records Service"]:::service
    end
    click AS "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/auth/auth.service.ts"
    click US "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/users/users.service.ts"
    click RS "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/rooms/rooms.service.ts"
    click FS "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/friends/friends.service.ts"
    click SS "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/session/session.service.ts"
    click ES "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/email/email.service.ts"
    click RCS "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/records/records.service.ts"

    subgraph "Real-time Layer"
        RG["Room Gateway"]:::realtime
        LG["Lobby Gateway"]:::realtime
        PG["Playlist Gateway"]:::realtime
        MS["Message Store"]:::store
        SES["Session Store"]:::store
    end
    click RG "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/gateways/room.gateway.ts"
    click LG "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/lobbyGateways/lobby.gateway.ts"
    click PG "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/gateways/playlist.gateway.ts"
    click MS "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/gateways/class/redis-message.store.ts"
    click SES "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/gateways/class/redis-session.store.ts"

    subgraph "Security Layer"
        JG["JWT Guard"]:::security
        WSG["WebSocket JWT Guard"]:::security
        Guards["Auth Guards"]:::security
    end
    click JG "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/auth/guard/jwt.guard.ts"
    click WSG "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/auth/guard/wsJwt.guard.ts"
    click Guards "https://github.com/SWM-FIRE/modoco-backend/tree/main/src/auth/guard"

    subgraph "Data Storage"
        PS["Prisma Service"]:::db
        RD[(Redis)]:::db
        PG_DB[(PostgreSQL)]:::db
    end
    click PS "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/prisma/prisma.service.ts"
    click RD "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/redis/redis.module.ts"
    click PG_DB "https://github.com/SWM-FIRE/modoco-backend/blob/main/prisma/schema.prisma"

    subgraph "OAuth Providers"
        KO["Kakao OAuth"]:::external
        GO["Google OAuth"]:::external
        GHO["GitHub OAuth"]:::external
    end
    click KO "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/auth/strategy/kakao.strategy.ts"
    click GO "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/auth/strategy/google.strategy.ts"
    click GHO "https://github.com/SWM-FIRE/modoco-backend/blob/main/src/auth/strategy/github.strategy.ts"

    subgraph "External Services"
        AWS["AWS SES"]:::external
    end

    %% Connections
    WC --> API
    MC --> API
    WC --> WS
    MC --> WS
    
    API --> AS
    API --> US
    API --> RS
    API --> FS
    API --> SS
    API --> ES
    API --> RCS

    WS --> RG
    WS --> LG
    WS --> PG

    RG --> MS
    RG --> SES
    LG --> MS
    LG --> SES
    PG --> MS
    PG --> SES

    AS --> KO
    AS --> GO
    AS --> GHO
    
    ES --> AWS

    AS --> PS
    US --> PS
    RS --> PS
    FS --> PS
    SS --> PS
    RCS --> PS

    MS --> RD
    SES --> RD
    PS --> PG_DB

    %% Styles
    classDef api fill:#1E88E5,color:white
    classDef websocket fill:#673AB7,color:white
    classDef docs fill:#FDD835,color:black
    classDef service fill:#43A047,color:white
    classDef realtime fill:#E91E63,color:white
    classDef store fill:#FF9800,color:black
    classDef security fill:#F44336,color:white
    classDef db fill:#795548,color:white
    classDef external fill:#607D8B,color:white
```
