# nyzss/matcha architecture

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

- Interactive diagram: https://gitdiagram.com/nyzss/matcha
- Repository: https://github.com/nyzss/matcha

## Overview

##### Main Components

A. Client-Side (Frontend):
- React Application Layer
- Authentication Module
- Profile Management
- Chat System
- Onboarding Flow
- React Router for routing
- Mantine UI Framework
- Socket.IO Client
- State Management (Custom store)

B. Server-Side (Backend):
- Fastify Server
- Socket.IO Server
- Authentication Service
- User Service
- Chat Service
- Database Layer (ORM)
- Email Service

C. Infrastructure:
- Caddy Web Server (Reverse Proxy)
- HTTPS Layer
- WebSocket Gateway

**[Client Layer] <-> [Proxy Layer] <-> [Server Layer]**

##### Component Relationships

- Frontend to Backend communication:
- REST API calls (HTTPS)
- WebSocket connections (Socket.IO)
- Internal Backend connections:
- Controllers to Services
- Services to Database
- Authentication flow

##### Specific Implementation Details

A. Client Architecture:
**[React Components]**
- ↓
**[Context Providers (Auth, Socket)]**
- ↓
**[API/Socket Services]**
- ↓
**[State Management]**

B. Server Architecture:
**[Fastify Routes]**
- ↓
**[Controllers]**
- ↓
**[Services]**
- ↓
**[Database (ORM)]**

##### Important Flows to Highlight

A. Authentication Flow:
**Client -> Auth Routes -> Auth Controller -> Auth Service -> Database**

B. Real-time Chat Flow:
**Client Socket -> Socket.IO Gateway -> Chat Service -> Database**

##### Additional Elements

Specific components:
- Validation layers (Zod schemas)
- Middleware chain
- Error handling flow
- Security layers (HTTPS, JWT)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        direction TB
        ReactApp["React Application"]
        AuthModule["Authentication Module"]:::frontend
        ProfileMgmt["Profile Management"]:::frontend
        ChatSystem["Chat System"]:::frontend
        Onboarding["Onboarding Flow"]:::frontend
        ContextProviders["Context Providers"]:::frontend
        SocketClient["Socket.IO Client"]:::frontend
        StateManagement["State Management"]:::frontend
        
        ReactApp --> AuthModule
        ReactApp --> ProfileMgmt
        ReactApp --> ChatSystem
        ReactApp --> Onboarding
        ReactApp --> ContextProviders
        ContextProviders --> SocketClient
        ContextProviders --> StateManagement
    end

    subgraph Proxy
        direction TB
        CaddyServer["Caddy Server"]:::infrastructure
        HTTPS["HTTPS Layer"]:::infrastructure
        WSGateway["WebSocket Gateway"]:::infrastructure
        
        CaddyServer --> HTTPS
        CaddyServer --> WSGateway
    end

    subgraph Server
        direction TB
        FastifyServer["Fastify Server"]:::backend
        
        subgraph Services
            direction TB
            AuthService["Authentication Service"]:::backend
            UserService["User Service"]:::backend
            ChatService["Chat Service"]:::backend
            EmailService["Email Service"]:::backend
            SocketServer["Socket.IO Server"]:::backend
        end
        
        subgraph Middleware
            direction TB
            AuthMiddleware["Auth Middleware"]:::backend
            ValidationLayer["Validation Layer"]:::backend
            APIRoutes["API Routes"]:::backend
        end
        
        DatabaseORM["Database Layer (ORM)"]:::database
        
        FastifyServer --> Middleware
        Middleware --> Services
        Services --> DatabaseORM
        SocketServer --> ChatService
    end

    Client -- "REST API (HTTPS)" --> Proxy
    Client -- "WebSocket" --> Proxy
    Proxy --> Server

    %% Click Events
    click AuthModule "https://github.com/nyzss/matcha/tree/main/client/app/components/auth"
    click ProfileMgmt "https://github.com/nyzss/matcha/tree/main/client/app/components/profile"
    click ChatSystem "https://github.com/nyzss/matcha/tree/main/client/app/routes/messages"
    click Onboarding "https://github.com/nyzss/matcha/tree/main/client/app/components/onboard"
    click SocketClient "https://github.com/nyzss/matcha/blob/main/client/app/lib/socket.ts"
    click StateManagement "https://github.com/nyzss/matcha/blob/main/client/app/lib/store.ts"
    click AuthService "https://github.com/nyzss/matcha/blob/main/server/src/app/services/authService.ts"
    click UserService "https://github.com/nyzss/matcha/blob/main/server/src/app/services/userService.ts"
    click ChatService "https://github.com/nyzss/matcha/blob/main/server/src/app/services/chatService.ts"
    click DatabaseORM "https://github.com/nyzss/matcha/tree/main/server/src/app/schemas/orm"
    click EmailService "https://github.com/nyzss/matcha/blob/main/server/src/app/utils/mail.ts"
    click SocketServer "https://github.com/nyzss/matcha/blob/main/server/src/app/plugins/socketPlugin.ts"
    click CaddyServer "https://github.com/nyzss/matcha/tree/main/Caddyfile"
    click ValidationLayer "https://github.com/nyzss/matcha/tree/main/server/src/app/schemas/zod"
    click AuthMiddleware "https://github.com/nyzss/matcha/blob/main/server/src/app/middlewares/authMiddleware.ts"
    click APIRoutes "https://github.com/nyzss/matcha/tree/main/server/src/app/routes"
    click ContextProviders "https://github.com/nyzss/matcha/tree/main/client/app/contexts"

    %% Styling
    classDef frontend fill:#2196F3,color:white
    classDef backend fill:#4CAF50,color:white
    classDef database fill:#FFC107,color:black
    classDef infrastructure fill:#9E9E9E,color:white

    %% Legend
    subgraph Legend
        Frontend["Frontend Component"]:::frontend
        Backend["Backend Component"]:::backend
        Database["Database Component"]:::database
        Infrastructure["Infrastructure Component"]:::infrastructure
    end
```
