# studyfi/studyfi-backend architecture

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

- Interactive diagram: https://gitdiagram.com/studyfi/studyfi-backend
- Repository: https://github.com/studyfi/studyfi-backend

## Overview

##### Project Type and Purpose
- This is a microservices‐based backend for a social media platform (“StudyFi”). It consists of multiple Spring Boot services, an API Gateway (Spring Cloud Gateway), a Eureka Discovery Server, and several domain microservices (User & Group, Content & News, Notification).

##### Main Components
a. API Gateway
- Spring Cloud Gateway service listening on port 8080.
- Routes external API calls to downstream services via Eureka.
- Handles cross‐cutting concerns (auth, rate‐limiting, logging).
b. Discovery Server
- Eureka Server on port 8761.
- Central registry for all microservices.
c. User & Group Service
- Manages user accounts, groups, image uploads (Cloudinary), email workflows (SMTP).
- Exposes REST endpoints for user registration, authentication, profile & group management.
- Persists to MySQL.
- Outbound: WebClient → Notification service.
- Inbound: other services may query user/group info.
d. Content & News Service
- Manages news articles, posts, likes, comments.
- File uploads via Cloudinary.
- Persists to MySQL.
- Outbound: WebClient → User & Group service (for author info), Notification service (for events).
e. Notification Service
- Receives notification requests (email) from other services.
- Sends via JavaMailSender/SMTP.
- Persists notifications to MySQL.

##### External Dependencies
- MySQL database (possibly one schema per service or shared).
- Cloudinary (file hosting).
- SMTP server (Google SMTP).

##### Interactions & Dataflows
1. Client → API Gateway → routed to appropriate microservice (User, Content, Notification).
2. Gateway & services register with Eureka → on startup.
3. Service discovery: Gateway and services call Eureka to resolve service instances (via `lb://SERVICE-NAME`).
4. User & Group Service → Notification Service (via WebClient) to send verification / reset emails.
5. Content & News Service →
- User & Group Service (via WebClient) to fetch author metadata.
- Notification Service to send “new comment” or “like” notifications.

##### Architectural Patterns & Principles
- Microservices with single responsibility.
- Service discovery (Eureka) + client‐side load balancing.
- API Gateway pattern for a unified entry point.
- Asynchronous HTTP communication (WebClient).
- Externalized configuration (application.properties).
- Cloud‐based file storage (Cloudinary).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Client
    Client["Client<br/>(Web/Mobile)"]:::external

    %% API Layer
    subgraph "API Layer"
        Gateway["API Gateway<br/>Spring Cloud Gateway<br/>Port 8080"]:::gateway
    end

    %% Infrastructure
    subgraph "Infrastructure"
        Eureka["Eureka Discovery Server<br/>Port 8761"]:::infra
    end

    %% Domain Services
    subgraph "Domain Services"
        UserService["User & Group Service<br/>Spring Boot, JPA, WebClient"]:::service
        ContentService["Content & News Service<br/>Spring Boot, JPA, WebClient"]:::service
        NotificationService["Notification Service<br/>Spring Boot, JPA, JavaMailSender"]:::service
    end

    %% Databases
    subgraph "Databases"
        UserDB["User DB<br/>MySQL"]:::db
        ContentDB["Content DB<br/>MySQL"]:::db
        NotificationDB["Notifications DB<br/>MySQL"]:::db
    end

    %% External Systems
    subgraph "External Services"
        Cloudinary["Cloudinary"]:::external
        SMTP["SMTP Server"]:::external
    end

    %% Client to Gateway
    Client -->|"REST calls"| Gateway

    %% Gateway <-> Eureka
    Gateway -->|"Eureka registration & discovery"| Eureka
    Eureka -->|"Service resolution"| Gateway

    %% Gateway to Services
    Gateway -->|"Routed API calls"| UserService
    Gateway -->|"Routed API calls"| ContentService
    Gateway -->|"Routed API calls"| NotificationService

    %% Services to Eureka
    UserService -->|"Eureka registration & discovery"| Eureka
    ContentService -->|"Eureka registration & discovery"| Eureka
    NotificationService -->|"Eureka registration & discovery"| Eureka

    %% Service to DB
    UserService -->|"JDBC/MySQL"| UserDB
    ContentService -->|"JDBC/MySQL"| ContentDB
    NotificationService -->|"JDBC/MySQL"| NotificationDB

    %% Service to Cloudinary
    UserService -->|"Cloudinary API"| Cloudinary
    ContentService -->|"Cloudinary API"| Cloudinary

    %% Inter-service WebClient calls
    UserService -->|"WebClient (async)"| NotificationService
    ContentService -->|"WebClient (async)"| UserService
    ContentService -->|"WebClient (async)"| NotificationService

    %% Notification to SMTP
    NotificationService -->|"SMTP/Email"| SMTP

    %% Click Events
    click Gateway "https://github.com/studyfi/studyfi-backend/blob/main/apigateway/src/main/java/com/apigateway/apigateway/ApiGatewayApplication.java"
    click Eureka "https://github.com/studyfi/studyfi-backend/blob/main/discoveryserver/src/main/java/com/discoveryserver/discoveryserver/DiscoveryServerApplication.java"
    click UserService "https://github.com/studyfi/studyfi-backend/blob/main/userandgroup/src/main/java/com/studyfi/userandgroup/UserandgroupApplication.java"
    click CloudinaryConfig1 "https://github.com/studyfi/studyfi-backend/blob/main/userandgroup/src/main/java/com/studyfi/userandgroup/config/CloudinaryConfig.java"
    click MailConfig "https://github.com/studyfi/studyfi-backend/blob/main/userandgroup/src/main/java/com/studyfi/userandgroup/config/MailConfig.java"
    click WebClientConfig1 "https://github.com/studyfi/studyfi-backend/blob/main/userandgroup/src/main/java/com/studyfi/userandgroup/config/WebClientConfig.java"
    click UserController "https://github.com/studyfi/studyfi-backend/blob/main/userandgroup/src/main/java/com/studyfi/userandgroup/user/controller/UserController.java"
    click EmailVerificationController "https://github.com/studyfi/studyfi-backend/blob/main/userandgroup/src/main/java/com/studyfi/userandgroup/user/controller/EmailVerificationController.java"
    click GroupController "https://github.com/studyfi/studyfi-backend/blob/main/userandgroup/src/main/java/com/studyfi/userandgroup/group/controller/GroupController.java"
    click ContentService "https://github.com/studyfi/studyfi-backend/blob/main/contentandnews/src/main/java/com/studyfi/contentandnews/ContentandnewsApplication.java"
    click CloudinaryConfig2 "https://github.com/studyfi/studyfi-backend/blob/main/contentandnews/src/main/java/com/studyfi/contentandnews/config/CloudinaryConfig.java"
    click WebClientConfig2 "https://github.com/studyfi/studyfi-backend/blob/main/contentandnews/src/main/java/com/studyfi/contentandnews/config/WebClientConfig.java"
    click ContentController "https://github.com/studyfi/studyfi-backend/blob/main/contentandnews/src/main/java/com/studyfi/contentandnews/controller/ContentController.java"
    click NewsController "https://github.com/studyfi/studyfi-backend/blob/main/contentandnews/src/main/java/com/studyfi/contentandnews/controller/NewsController.java"
    click ChatController "https://github.com/studyfi/studyfi-backend/blob/main/contentandnews/src/main/java/com/studyfi/contentandnews/chat/controller/ChatController.java"
    click NotificationService "https://github.com/studyfi/studyfi-backend/blob/main/notification/src/main/java/com/studyfi/notification/NotificationApplication.java"
    click WebClientConfig3 "https://github.com/studyfi/studyfi-backend/blob/main/notification/src/main/java/com/studyfi/notification/config/WebClientConfig.java"
    click NotificationController "https://github.com/studyfi/studyfi-backend/blob/main/notification/src/main/java/com/studyfi/notification/controller/NotificationController.java"
    click NotificationServiceImpl "https://github.com/studyfi/studyfi-backend/blob/main/notification/src/main/java/com/studyfi/notification/service/NotificationService.java"
    click NotificationRepo "https://github.com/studyfi/studyfi-backend/blob/main/notification/src/main/java/com/studyfi/notification/repo/NotificationRepo.java"

    %% Styles
    classDef gateway fill:#f9f,stroke:#333,stroke-width:1px
    classDef infra fill:#bbf,stroke:#333,stroke-width:1px
    classDef service fill:#bfb,stroke:#333,stroke-width:1px
    classDef db fill:#ffb,stroke:#333,stroke-width:1px
    classDef external fill:#fdd,stroke:#333,stroke-width:1px
```
