# mynameiskry/noname-forum architecture

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

- Interactive diagram: https://gitdiagram.com/mynameiskry/noname-forum
- Repository: https://github.com/mynameiskry/noname-forum

## Overview

- Frontend (client): Represent the Next.js application. This includes:
- Core pages (authentication and main sections)
- Sub-components like Header, Footer, Sidebar, Post, and others.
- Utility components and hooks.
- Backend (server): Represent the Fastify server.
- Controllers (auth, post, comment, user, category)
- Route definitions split between public and private endpoints (authentication, posts, etc.)
- Middlewares (authentication, isAdmin)
- Utilities (token generation, caching, sending mail)
- Database: Represent the Sqlite3 database accessed via Prisma.
- Schema definition (via Prisma’s schema.prisma)
- External Services: For example, Google OAuth2, email services (Gmail) and possibly OTP verification.

##### Relationships and interactions
- The Backend handles these HTTP/REST API calls (using Fastify routes) and, when needed, performs business logic via controllers and services.
- That the Backend communicates with the Database through Prisma ORM.
- External integrations (Google OAuth2 for authentication, email services for verification).

##### Architectural patterns
- The separation of concerns for client and server is clear: a distinct frontend (Next.js app) and backend (Fastify server).
- On the backend, there’s a modular approach using controllers, services, and middleware which resembles a layered architecture.
- The RESTful API routing (public/private routes) demonstrates a clear separation of endpoints.
- The use of Prisma encapsulates the interaction with the database, a best practice for data access abstraction.

##### Important technologies
- Frontend: Next.js, MUI, Tailwind CSS, and FontAwesome.
- Backend: Node.js with Fastify.
- Database: Sqlite3 managed via Prisma.
- Authentication flows: Implemented using JWT tokens, Google OAuth2, and OTP (indicated by the auth and refresh token routes).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Actor
    User["User"]:::external

    %% Frontend (Client)
    subgraph "Frontend (Client)"
        %% Authentication Pages
        subgraph "Authentication Pages"
            FE_Login["Login Page"]:::frontend
            FE_Register["Register Page"]:::frontend
            FE_Verify["Verify Page"]:::frontend
        end
        %% Main Forum Pages
        subgraph "Main Forum Pages"
            FE_Main["Main Layout"]:::frontend
            FE_Post["Post Pages"]:::frontend
            FE_CreatePost["Create Post Page"]:::frontend
        end
        %% User Profile Pages
        subgraph "User Profile Pages"
            FE_UserProfile["User Profile"]:::frontend
            FE_UserComponents["User Components"]:::frontend
        end
        %% Shared Components & Utilities
        subgraph "Shared Components & Utilities"
            FE_Shared["Shared Components"]:::frontend
            FE_Utils["Utility Components & Hooks"]:::frontend
        end
    end

    %% Backend (Server)
    subgraph "Backend (Server)"
        BE_Entry["Fastify Entry (index.ts)"]:::backend
        %% Controllers
        subgraph "Controllers"
            BE_AuthController["Auth Controller"]:::backend
            BE_PostController["Post Controller"]:::backend
            BE_CommentController["Comment Controller"]:::backend
            BE_UserController["User Controller"]:::backend
            BE_CategoryController["Category Controller"]:::backend
        end
        %% Routes
        subgraph "Routes"
            BE_Public["Public Routes"]:::backend
            BE_Private["Private Routes"]:::backend
        end
        %% Middleware & Services
        subgraph "Middleware & Services"
            BE_Middleware["Middlewares (Auth, isAdmin)"]:::backend
            BE_Services["Services (Business Logic)"]:::backend
        end
        %% Utilities
        subgraph "Utilities"
            BE_Token["Token Generation"]:::backend
            BE_Cache["Caching Utility"]:::backend
            BE_Mail["Mail Sending Utility"]:::backend
            BE_Search["Search Engine Utility"]:::backend
        end
    end

    %% Database
    subgraph "Database"
        DB["Prisma & Sqlite3"]:::database
    end

    %% External Services
    subgraph "External Services"
        EXT_OAuth["Google OAuth2"]:::external
        EXT_Email["Email Service (Gmail)"]:::external
        EXT_OTP["OTP Verification"]:::external
    end

    %% Relationships
    User -->|"interacts" | FE_Login
    User -->|"interacts" | FE_Main
    User -->|"interacts" | FE_UserProfile

    %% Frontend internal calls
    FE_Login -->|"APIcall" | BE_Entry
    FE_Register -->|"APIcall" | BE_Entry
    FE_Verify -->|"APIcall" | BE_Entry
    FE_Main -->|"APIcall" | BE_Entry
    FE_Post -->|"APIcall" | BE_Entry
    FE_CreatePost -->|"APIcall" | BE_Entry
    FE_UserProfile -->|"APIcall" | BE_Entry
    FE_UserComponents -->|"rendered by" | FE_UserProfile
    FE_Shared -->|"used by" | FE_Main
    FE_Utils -->|"used by" | FE_Shared

    %% Backend routing
    BE_Entry -->|"routes" | BE_Public
    BE_Entry -->|"routes" | BE_Private

    %% Public Routes dispatching
    BE_Public -->|"dispatches" | BE_AuthController
    BE_Public -->|"dispatches" | BE_PostController
    BE_Public -->|"dispatches" | BE_CommentController
    BE_Public -->|"dispatches" | BE_UserController
    BE_Public -->|"integrates" | EXT_OAuth

    %% Private Routes dispatching
    BE_Private -->|"dispatches" | BE_CategoryController

    %% Controllers using services
    BE_AuthController -->|"calls" | BE_Services
    BE_PostController -->|"calls" | BE_Services
    BE_CommentController -->|"calls" | BE_Services
    BE_UserController -->|"calls" | BE_Services
    BE_CategoryController -->|"calls" | BE_Services

    %% Middleware secures routes
    BE_Middleware -->|"secures" | BE_Public
    BE_Middleware -->|"secures" | BE_Private

    %% Services interacting with Database
    BE_Services -->|"data access" | DB

    %% Utilities usage
    BE_Token -->|"generates" | BE_AuthController
    BE_Cache -->|"optimizes" | BE_Services
    BE_Mail -->|"sends" | EXT_Email
    BE_Search -->|"assists" | BE_PostController

    %% External Services integration in backend
    BE_AuthController -->|"verifies" | EXT_OTP

    %% Direct database connection via ORM
    BE_Entry -->|"uses Prisma" | DB

    %% Click Events
    click FE_Login "https://github.com/mynameiskry/noname-forum/blob/main/client/src/app/(authentication)/auth/login/page.tsx"
    click FE_Register "https://github.com/mynameiskry/noname-forum/blob/main/client/src/app/(authentication)/auth/register/page.tsx"
    click FE_Verify "https://github.com/mynameiskry/noname-forum/blob/main/client/src/app/(authentication)/auth/verify/page.tsx"
    click FE_Main "https://github.com/mynameiskry/noname-forum/blob/main/client/src/app/(main)/page.tsx"
    click FE_CreatePost "https://github.com/mynameiskry/noname-forum/blob/main/client/src/app/(main)/post/create/page.tsx"
    click FE_UserProfile "https://github.com/mynameiskry/noname-forum/blob/main/client/src/app/(main)/user/[username]/page.tsx"
    click FE_Shared "https://github.com/mynameiskry/noname-forum/tree/main/client/src/components"
    click FE_Utils "https://github.com/mynameiskry/noname-forum/blob/main/client/src/app/hooks/useEdit.ts"
    click BE_Entry "https://github.com/mynameiskry/noname-forum/blob/main/server/src/index.ts"
    click BE_AuthController "https://github.com/mynameiskry/noname-forum/blob/main/server/src/controllers/authController.ts"
    click BE_PostController "https://github.com/mynameiskry/noname-forum/blob/main/server/src/controllers/postController.ts"
    click BE_CommentController "https://github.com/mynameiskry/noname-forum/blob/main/server/src/controllers/commentController.ts"
    click BE_UserController "https://github.com/mynameiskry/noname-forum/blob/main/server/src/controllers/userController.ts"
    click BE_CategoryController "https://github.com/mynameiskry/noname-forum/blob/main/server/src/controllers/categoryController.ts"
    click BE_Public "https://github.com/mynameiskry/noname-forum/tree/main/server/src/routes/public"
    click BE_Private "https://github.com/mynameiskry/noname-forum/tree/main/server/src/routes/private"
    click BE_Middleware "https://github.com/mynameiskry/noname-forum/tree/main/server/src/middlewares"
    click BE_Services "https://github.com/mynameiskry/noname-forum/tree/main/server/src/services"
    click BE_Token "https://github.com/mynameiskry/noname-forum/blob/main/server/src/utils/generateToken.ts"
    click BE_Cache "https://github.com/mynameiskry/noname-forum/blob/main/server/src/utils/createCache.ts"
    click BE_Mail "https://github.com/mynameiskry/noname-forum/blob/main/server/src/utils/sendMail.ts"
    click BE_Search "https://github.com/mynameiskry/noname-forum/blob/main/server/src/utils/searchEngine.ts"
    click DB "https://github.com/mynameiskry/noname-forum/blob/main/server/prisma/schema.prisma"
    click EXT_OAuth "https://github.com/mynameiskry/noname-forum/blob/main/server/src/routes/public/oauth2Route.ts"
    click EXT_Email "https://github.com/mynameiskry/noname-forum/blob/main/server/src/utils/sendMail.ts"

    %% Class Definitions for Colors
    classDef frontend fill:#AEDFF7,stroke:#333,stroke-width:2px;
    classDef backend fill:#F7D6AE,stroke:#333,stroke-width:2px;
    classDef database fill:#C7F7C4,stroke:#333,stroke-width:2px;
    classDef external fill:#F7AEC7,stroke:#333,stroke-width:2px;
```
