# nhatnguyen150100/myceph-server architecture

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

- Interactive diagram: https://gitdiagram.com/nhatnguyen150100/myceph-server
- Repository: https://github.com/nhatnguyen150100/myceph-server

## Overview

##### Main Components
- Entry Point: The main application (app.js) that bootstraps the Express server.
- Configuration Modules: Under the “config” directory, which includes settings for the database (config.js, connectDB.js), email (mail.config.js), and logging (winston.js). These are key for initializing external services and environment-specific parameters.
- Controllers: Organized under the “controllers” (and its “auth” and “token” subdirectories), these handle requests and invoke the right business logic.
- Services: Located in the “services” folder, these encapsulate the business logic and data interactions, bridging controllers with the database.
- Models: The “models” directory holds Sequelize models corresponding to the database tables. Also included are migration scripts (in “migrations”) which define and modify the DB schema.
- Routes: Under “routers,” where HTTP endpoints are defined and mapped to controller actions.
- Middleware: In the “middleware” directory, which intercepts requests (for tasks like authentication or request validation) before they reach the controllers.
- Logging: Multiple directories (both in “build/src/log” and “src/log”) are dedicated to logging various types of events (e.g., authentication, clinic-related logs, treatment history), which supports monitoring and debugging.
- Common Utilities: Built-in functions and utilities in the “common” folders that support common operations for the whole application.
- Build Process: Note the use of Babel (with the .babelrc file) and scripts in “package.json” to build and deploy the production version.

##### Relationships and Interactions
- Data Flow: HTTP Clients send requests that hit the Express server via defined routes. These requests pass through middleware (for tasks like validation or recaptcha) and reach controllers.
- Controllers call Services: Controllers extract the required parameters and delegate the business logic to service methods.
- Services and Models: Services interact with the database through Sequelize models. The migrations ensure the models and database schema are aligned.
- Configuration Integration: The config modules are used early in the bootstrapping process (for connecting to the DB, setting up email services, and initializing logger with Winston).
- Logging: Throughout the request lifecycle, various events and errors get logged into dedicated log files to facilitate monitoring.
- Authentication: The auth controllers and token management (with RSA keys and refresh token mechanisms) manage security, and are integrated into the middleware for protecting routes.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clearly segmenting configuration, controller logic, service logic, data models, and middleware.
- Modular Design: Each domain (doctors, patients, clinics, treatments, etc.) has dedicated controllers, services, and routes making it easier to scale or modify specific functionalities.
- Use of ORM (Sequelize): To abstract the database interactions, ensuring migration scripts and model definitions are maintained separately from business logic.
- Logging & Error Handling: Integrated logging at various layers to aid in debugging and monitoring in production.
- Security Layers: Implementation of authentication (via passport) and token-based access control using public/private PEM keys.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client Layer
    subgraph "Client Layer"
        Client["HTTP Client"]:::external
    end

    %% Server Entry
    subgraph "Server Entry"
        App["API Server (app.js)"]:::entry
        Config["Configuration Modules"]:::config
    end

    %% Routing & Middleware
    subgraph "Routing & Middleware"
        Routes["Routes"]:::business
        Middleware["Middleware"]:::business
    end

    %% Business Logic
    subgraph "Business Logic"
        Controllers["Controllers"]:::business
        Services["Services"]:::business
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        Models["Models"]:::datalayer
        Migrations["Migrations"]:::datalayer
        DB["Database"]:::database
    end

    %% Infrastructure & Logging
    subgraph "Infrastructure & Logging"
        Logger["Logging"]:::log
        Email["Email Server"]:::external
        Render["Deployment (Render)"]:::external
    end

    %% Build Process & Deployment Configurations
    subgraph "Build Process & Deployment Configurations"
        BabelRC[".babelrc"]:::buildFile
        Package["package.json"]:::buildFile
        Docker["Dockerfile"]:::buildFile
    end

    %% Connections
    Client -->|"sends HTTP requests"| App
    App -->|"initializes"| Config
    App -->|"loads"| Routes
    Config -->|"configures"| Email
    Routes -->|"passes to"| Middleware
    Middleware -->|"invokes"| Controllers
    Controllers -->|"calls"| Services
    Services -->|"accesses"| Models
    Models -->|"CRUD operations"| DB
    Migrations -->|"updates"| Models

    %% Error logging feedback (dotted lines)
    App ---|"logs"| Logger
    Middleware ---|"logs"| Logger
    Controllers ---|"logs"| Logger
    Services ---|"logs"| Logger

    %% Build process deploys the server
    BabelRC -->|"transpiles via Babel"| App
    Package -->|"scripts/build"| App
    Docker -->|"containerizes"| Render

    %% Classes
    classDef external fill:#FFB6C1,stroke:#000,stroke-width:1px;
    classDef config fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef business fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef datalayer fill:#FFD700,stroke:#000,stroke-width:1px;
    classDef log fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef buildFile fill:#FFA07A,stroke:#000,stroke-width:1px;
    classDef entry fill:#E6E6FA,stroke:#000,stroke-width:1px;
    classDef database fill:#F08080,stroke:#000,stroke-width:1px;

    %% Click Events
    click App "https://github.com/nhatnguyen150100/myceph-server/blob/master/build/src/app.js"
    click Config "https://github.com/nhatnguyen150100/myceph-server/tree/master/build/src/config"
    click Routes "https://github.com/nhatnguyen150100/myceph-server/tree/master/build/src/routes"
    click Middleware "https://github.com/nhatnguyen150100/myceph-server/tree/master/build/src/middleware"
    click Controllers "https://github.com/nhatnguyen150100/myceph-server/tree/master/build/src/controllers"
    click Services "https://github.com/nhatnguyen150100/myceph-server/tree/master/build/src/services"
    click Models "https://github.com/nhatnguyen150100/myceph-server/tree/master/build/src/models"
    click Migrations "https://github.com/nhatnguyen150100/myceph-server/tree/master/build/src/migrations"
    click Logger "https://github.com/nhatnguyen150100/myceph-server/tree/master/build/src/log"
    click BabelRC "https://github.com/nhatnguyen150100/myceph-server/blob/master/.babelrc"
    click Package "https://github.com/nhatnguyen150100/myceph-server/blob/master/package.json"
    click Docker "https://github.com/nhatnguyen150100/myceph-server/tree/master/Dockerfile"
```
