# khoavu1831/facebook_clone architecture

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

- Interactive diagram: https://gitdiagram.com/khoavu1831/facebook_clone
- Repository: https://github.com/khoavu1831/facebook_clone

## Overview

##### Project type and purpose
- This is a full‑stack “Facebook‑clone” social media application.
- Frontend: React SPA uses REST and WebSocket for UI, real‑time chat, posting, friend management.
- Backend: Spring Boot handles authentication, user/profile management, posts, comments, friends, real‑time messaging (STOMP over WebSocket).
- Database: JSON files in “database/” simulate persistent storage; likely will be replaced by a relational DB in production.
- File storage: “uploads/” directory for user‑uploaded images.

##### File structure
- Top level:
- /frontend → React application (components, pages, services, config)
- /backend → Maven‑based Spring Boot service (controller, service, repository, model, config)
- /database → JSON files representing data store
- /uploads → media storage
- Backend folders follow layered architecture (config, controller, service, repository, model).
- frontend/src configures API base URL, WebSocket client, React context for user state.
- Application.properties configures ports, CORS, WebSocket endpoints.

##### README for additional insights
- Commands to install and run frontend (`npm install`, `npm start`) and backend (`mvn spring-boot:run`).
- Mentions STOMP and SockJS for real‑time.

- Client (Web Browser) running React SPA
- Spring Boot Backend Service
- Data Store (JSON files or replaceable by RDBMS like MySQL/PostgreSQL)
- File Storage (uploads folder or external S3 bucket)
- Real‑time Messaging (WebSocket + STOMP endpoint)
- External services (e.g., authentication provider or CDN if any)

b. Relationships & interactions:
- React SPA → HTTP(S) REST calls → Spring Boot controllers
- React SPA → WebSocket (SockJS/STOMP) → Spring Boot WebSocketConfig → ChatController
- Spring Boot → Repository layer → Data Store
- Spring Boot → FileStorageService → Local uploads folder (or external)
- Sequence: User clicks “Post” → POST /posts → PostController → PostService → PostRepository → JSON DB → response → React updates UI

c. Architectural patterns & principles:
- Layered (Controller → Service → Repository → Model) in backend
- Component/Container in frontend (pages, UI components, context for state)
- Observer pattern via WebSocket subscriptions (chat messages)
- CORS configuration to allow cross‑origin between frontend and backend

d. Relevant technologies/frameworks:
- Frontend: React, Context API, STOMP.js, SockJS-client, Axios (or fetch), CSS modules
- Backend: Java, Spring Boot (Web, Data JPA if using RDBMS), Maven, WebSocket (STOMP), CORS
- Storage: Local file system (/uploads) or replaceable blob storage
- Mock DB: JSON files (could be MongoDB or SQL in future)

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Client (Web Browser)"
        ReactSPA["React SPA"]:::frontend
        index["index.js"]:::frontend
        apiCfg["API Config (api.js)"]:::frontend
        wsClient["WebSocket Config"]:::frontend
        userCtx["User Context"]:::frontend
        authFE["Auth Component"]:::frontend
        postForm["PostForm"]:::frontend
        postList["PostList"]:::frontend
        friendsFE["Friends Page"]:::frontend
        profileFE["Profile Page"]:::frontend
        chatForm["ChatForm"]:::frontend
    end

    subgraph "Backend Service (Spring Boot)"
        subgraph "Config"
            corsCfg["CorsConfig"]:::backend
            webCfg["WebConfig"]:::backend
            wsCfg["WebSocketConfig"]:::backend
        end
        subgraph "Controller Layer"
            authCtrl["AuthController"]:::backend
            postCtrl["PostController"]:::backend
            friendCtrl["FriendController"]:::backend
            profileCtrl["ProfileController"]:::backend
            chatCtrl["ChatController"]:::backend
        end
        subgraph "Service Layer"
            userSvc["UserService"]:::backend
            fileSvc["FileStorageService"]:::backend
        end
        subgraph "Repository Layer"
            userRepo["UserRepository"]:::backend
            postRepo["PostRepository"]:::backend
            friendRepo["FriendRepository"]:::backend
            chatRepo["ChatMessageRepository"]:::backend
        end
        subgraph "Model Layer"
            userMod["User"]:::backend
            postMod["Post"]:::backend
            commentMod["Comment & Request DTO"]:::backend
            friendMod["Friend"]:::backend
            profileMod["Profile"]:::backend
            chatMod["ChatMessage"]:::backend
        end
    end

    subgraph "Data Layer"
        db["JSON DB"]:::database
    end

    subgraph "File Storage"
        uploads["Uploads Directory"]:::storage
    end

    ReactSPA -->|REST| authCtrl
    ReactSPA -->|REST| postCtrl
    ReactSPA -->|REST| friendCtrl
    ReactSPA -->|REST| profileCtrl
    wsClient -->|"WebSocket STOMP"| wsCfg
    authCtrl --> userSvc
    postCtrl --> postRepo
    friendCtrl --> friendRepo
    profileCtrl --> profileRepo
    chatCtrl --> chatRepo
    userSvc --> userRepo
    fileSvc --> uploads
    userRepo --> db
    postRepo --> db
    friendRepo --> db
    profileRepo --> db
    chatRepo --> db

    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef backend fill:#E0FFE0,stroke:#28a745,color:#22863a;
    classDef database fill:#FFF5B1,stroke:#DBAB09,color:#735C0F,stroke-dasharray: 5 5;
    classDef storage fill:#F0F0F0,stroke:#6A737D,color:#6A737D;
    classDef communication stroke-dasharray: 2 2,color:#FF8C00;

    click ReactSPA "https://github.com/khoavu1831/facebook_clone/tree/main/frontend/"
    click index "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/index.js"
    click apiCfg "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/config/api.js"
    click wsClient "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/services/websocket.js"
    click userCtx "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/contexts/UserContext.js"
    click authFE "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/components/Auth/Auth.js"
    click postForm "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/components/Post/PostForm.js"
    click postList "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/components/Post/PostList.js"
    click friendsFE "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/pages/Friends/Friends.js"
    click profileFE "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/pages/Profile/Profile.js"
    click chatForm "https://github.com/khoavu1831/facebook_clone/blob/main/frontend/src/components/ChatForm.js"
    click authCtrl "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/controller/AuthController.java"
    click userSvc "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/service/UserService.java"
    click userRepo "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/repository/UserRepository.java"
    click userMod "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/model/User.java"
    click postCtrl "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/controller/PostController.java"
    click postRepo "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/repository/PostRepository.java"
    click postMod "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/model/Post.java"
    click friendCtrl "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/controller/FriendController.java"
    click friendRepo "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/repository/FriendRepository.java"
    click friendMod "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/model/Friend.java"
    click profileCtrl "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/controller/ProfileController.java"
    click profileRepo "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/repository/ProfileRepository.java"
    click profileMod "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/model/Profile.java"
    click chatCtrl "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/controller/ChatController.java"
    click chatRepo "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/repository/ChatMessageRepository.java"
    click chatMod "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/model/ChatMessage.java"
    click wsCfg "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/config/WebSocketConfig.java"
    click corsCfg "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/config/CorsConfig.java"
    click webCfg "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/config/WebConfig.java"
    click fileSvc "https://github.com/khoavu1831/facebook_clone/blob/main/backend/src/main/java/com/example/facebook_clone/service/FileStorageService.java"
    click uploads "https://github.com/khoavu1831/facebook_clone/tree/main/uploads/"
    click db "https://github.com/khoavu1831/facebook_clone/tree/main/database/"
```
