# md-atif-khan/codegen architecture

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

- Interactive diagram: https://gitdiagram.com/md-atif-khan/codegen
- Repository: https://github.com/md-atif-khan/codegen

## Overview

##### Project Type and Purpose
- CodeGen is a full-stack web application.
- Client: a Vite-powered React UI (with Tailwind CSS and ESLint).
- Server: an Express.js backend exposing REST/AI endpoints, persisting data in MongoDB, handling JWT-based auth.

##### File Structure
- Top-level directories:
– client/ (front-end)
– server/ (back-end)
- client/: React components, pages, context, hooks, services (API wrapper, validation), assets, config (vite, ESLint, PostCSS).
- server/: Express entry (server.js), MVC-style subdirectories—controllers, models, routes, services—plus middleware (auth, error, logger), config (database), and utils.
- render.yaml: deployment descriptor (e.g., Render.com).

##### README for Additional Insights
- Client runs on VITE dev server; connects to BACKEND via VITE_API_URL.
- Server uses MONGODB_URI, JWT_SECRET, API_KEY for AI routes.
- Communication: client ↔ server over REST; server ↔ MongoDB; server ↔ external AI service.

##### Main Components & Interactions
a. Frontend (React App)
– Components: CodeEditor, ClassStructure, CodeStructureViewer, CreateProject, Navbar, PrivateRoute.
– Pages: Home, Login/Signup, Dashboard, ProjectPage, ProjectEditor.
– Context/AuthContext manages JWT and user state.
– Hooks: useAuth for auth logic.
– Services: api.js wraps HTTP calls; validatePassword.js enforces client‐side rules.
b. Backend (Express API)
– Routes: authRoutes (login/signup), projectRoutes (CRUD), aiRoutes (AI code gen).
– Controllers delegate to services: authController → authService (JWT, password hashing); projectController → projectService (CRUD on Project model).
– Middleware: auth (protect routes by verifying JWT), logger (request logging), errorHandler (centralized error responses).
– Models: User, Project (Mongoose schemas).
c. Database
– MongoDB (single database) accessed via Mongoose.
d. External Services
– AI provider (invoked by aiRoutes using API_KEY).

##### Relationships & Data Flows
1. User interacts with React UI → triggers api.js calls.
2. HTTP(S) requests to Express endpoints.
3. Auth middleware checks JWT; controllers handle requests.
4. Services perform business logic, interact with Mongoose models to read/write MongoDB.
5. For code‐generation, projectController forwards to aiRoutes which call external AI API.
6. Responses flow back through middleware → sent to frontend.
7. UI updates via React state/context.

##### Architectural Patterns and Technologies
- Frontend: Component-based (React), SPA, Context API for state, hooks for logic separation, Vite build pipeline, Tailwind CSS for styling.
- Backend: MVC pattern (Models, Views are JSON, Controllers), services layer for business logic, middleware for cross-cutting concerns, environment-driven config.
- Security: JWT for stateless auth, CORS configured via CLIENT_URL.
- Deployment: Render.com descriptor (render.yaml).

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    subgraph "Client Layer"
        direction TB
        subgraph "React App" 
            direction TB
            CE["CodeEditor"]:::frontend
            CS["ClassStructure"]:::frontend
            CSV["CodeStructureViewer"]:::frontend
            CP["CreateProject"]:::frontend
            NB["Navbar"]:::frontend
            PR["PrivateRoute"]:::frontend
            HM["Home Page"]:::frontend
            LI["Login Page"]:::frontend
            SU["Signup Page"]:::frontend
            DB["Dashboard"]:::frontend
            PP["ProjectPage"]:::frontend
            PE["ProjectEditor"]:::frontend
            AC["AuthContext"]:::frontend
            UH["useAuth Hook"]:::frontend
            APIwr["API Wrapper (api.js)"]:::frontend
            VP["Password Validation (validatePassword.js)"]:::frontend
            VITE["Vite Dev Server & Bundler"]:::frontend
        end
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        SRV["Express Server (server.js)"]:::server
        subgraph "Middleware"
            direction TB
            MW1["Auth Middleware"]:::server
            MW2["Logger Middleware"]:::server
            MW3["ErrorHandler Middleware"]:::server
        end
        subgraph "Routes"
            direction TB
            R1["Auth Routes"]:::server
            R2["Project Routes"]:::server
            R3["AI Routes"]:::server
        end
        subgraph "Controllers"
            direction TB
            C1["Auth Controller"]:::server
            C2["Project Controller"]:::server
        end
        subgraph "Services"
            direction TB
            S1["Auth Service"]:::server
            S2["Project Service"]:::server
            S3["Password Validation Service"]:::server
        end
        subgraph "Models"
            direction TB
            M1["User Model"]:::server
            M2["Project Model"]:::server
        end
        CFG["Database Config"]:::server
        UT["Errors Utils"]:::server
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        DBMS["MongoDB"]:::database
    end

    %% External Services
    subgraph "External Service"
        direction TB
        AI["AI Provider"]:::external
    end

    %% Connections
    APIwr -->|"REST calls via VITE_API_URL"| SRV
    VITE -->|"serves React App"| CE
    CE --> APIwr
    CS --> APIwr
    CSV --> APIwr
    CP --> APIwr
    PR --> AC
    AC --> UH
    UH --> APIwr
    VP --> CP
    SRV --> MW1
    SRV --> MW2
    SRV --> MW3
    MW1 --> R1
    MW1 --> R2
    MW1 --> R3
    R1 --> C1
    R2 --> C2
    R3 --> C2
    C1 --> S1
    C2 --> S2
    C2 --> S3
    S1 --> M1
    S2 --> M2
    S3 --> M1
    S2 --> DBMS
    C2 --> AI
    R1 --> SRV
    SRV -->|"JSON responses"| CE

    %% Click Events - Client
    click CE "https://github.com/md-atif-khan/codegen/blob/main/client/src/components/CodeEditor/index.jsx"
    click CS "https://github.com/md-atif-khan/codegen/blob/main/client/src/components/ClassStructure/index.jsx"
    click CSV "https://github.com/md-atif-khan/codegen/blob/main/client/src/components/CodeStructureViewer/index.jsx"
    click CP "https://github.com/md-atif-khan/codegen/blob/main/client/src/components/CreateProject/index.jsx"
    click NB "https://github.com/md-atif-khan/codegen/blob/main/client/src/components/Navbar/index.jsx"
    click PR "https://github.com/md-atif-khan/codegen/blob/main/client/src/components/PrivateRoute/index.jsx"
    click HM "https://github.com/md-atif-khan/codegen/blob/main/client/src/pages/Home/index.jsx"
    click LI "https://github.com/md-atif-khan/codegen/blob/main/client/src/pages/Login/index.jsx"
    click SU "https://github.com/md-atif-khan/codegen/blob/main/client/src/pages/Signup/index.jsx"
    click DB "https://github.com/md-atif-khan/codegen/blob/main/client/src/pages/Dashboard/index.jsx"
    click PP "https://github.com/md-atif-khan/codegen/blob/main/client/src/pages/ProjectPage/index.jsx"
    click PE "https://github.com/md-atif-khan/codegen/blob/main/client/src/pages/ProjectEditor/index.jsx"
    click AC "https://github.com/md-atif-khan/codegen/blob/main/client/src/context/AuthContext.jsx"
    click UH "https://github.com/md-atif-khan/codegen/blob/main/client/src/hooks/useAuth.js"
    click APIwr "https://github.com/md-atif-khan/codegen/blob/main/client/src/services/api.js"
    click VP "https://github.com/md-atif-khan/codegen/blob/main/client/src/services/validatePassword.js"

    %% Click Events - Server
    click SRV "https://github.com/md-atif-khan/codegen/blob/main/server/server.js"
    click R1 "https://github.com/md-atif-khan/codegen/blob/main/server/src/routes/authRoutes.js"
    click R2 "https://github.com/md-atif-khan/codegen/blob/main/server/src/routes/projectRoutes.js"
    click R3 "https://github.com/md-atif-khan/codegen/blob/main/server/src/routes/aiRoutes.js"
    click C1 "https://github.com/md-atif-khan/codegen/blob/main/server/src/controllers/authController.js"
    click C2 "https://github.com/md-atif-khan/codegen/blob/main/server/src/controllers/projectController.js"
    click S1 "https://github.com/md-atif-khan/codegen/blob/main/server/src/services/authService.js"
    click S2 "https://github.com/md-atif-khan/codegen/blob/main/server/src/services/projectService.js"
    click S3 "https://github.com/md-atif-khan/codegen/blob/main/server/src/services/validatePassword.js"
    click MW1 "https://github.com/md-atif-khan/codegen/blob/main/server/src/middleware/auth.js"
    click MW2 "https://github.com/md-atif-khan/codegen/blob/main/server/src/middleware/logger.js"
    click MW3 "https://github.com/md-atif-khan/codegen/blob/main/server/src/middleware/errorHandler.js"
    click M1 "https://github.com/md-atif-khan/codegen/blob/main/server/src/models/User.js"
    click M2 "https://github.com/md-atif-khan/codegen/blob/main/server/src/models/Project.js"
    click CFG "https://github.com/md-atif-khan/codegen/blob/main/server/src/config/database.js"
    click UT "https://github.com/md-atif-khan/codegen/blob/main/server/src/utils/errors.js"
    click render "https://github.com/md-atif-khan/codegen/blob/main/render.yaml"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6
    classDef server fill:#D6FFD6,stroke:#22863a,color:#22863a
    classDef database fill:#FFE8A0,stroke:#735C0F,color:#735C0F
    classDef external fill:#E8D0FF,stroke:#6f42c1,color:#6f42c1
```
