# do-an-da-nganh-l06/be_farm architecture

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

- Interactive diagram: https://gitdiagram.com/do-an-da-nganh-l06/be_farm
- Repository: https://github.com/do-an-da-nganh-l06/be_farm

## Overview

##### Project type and purpose
- This is a Node.js + Express backend service (“BE_FARM”) for a smart-farming system.
- It provides REST endpoints for a frontend or other clients to read sensor data (temperature, humidity, light, soil moisture) and send control commands (pump, LED) via Adafruit IO.
- It persists data in a MySQL database and secures its APIs with JWT. It also provides Swagger-driven API documentation.

##### File structure
- Top-level files:
- server.js / app.js: entry points that wire up Express, middleware and Swagger.
- .env_sample: configuration template for DB, Adafruit IO feeds, JWT secret, PORT.
- config/: holds adafruit.js (connection to Adafruit IO) and db.js (MySQL connection).
- controllers/: express route handlers divided by concern (adafruitController, authController, controlController).
- services/: business-logic layer (adafruitService, authService).
- models/: data models and SQL schema wrappers (authModel, sensorModel, limitModel, graph.js for reporting).
- routes/: API routing definitions (adafruitRoutes, auth.js).
- middlewares/: JWT authentication and request validation.
- swaggerDocs.js / swaggerOption.js: Swagger setup.
- database_chinh.sql / database_morong.sql: DDL for creating MySQL schema.

##### README for additional insights
- Data flow: sensors ↔ Adafruit IO ↔ backend ↔ MySQL.
- Control flow: backend → Adafruit IO → pump/LED.
- Clients (FE or mobile) call backend’s REST endpoints.
- JWT-based auth secures private routes.
- Swagger UI allows BE/FE developers to explore and test APIs.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global entities
    Client["Client (FE / Mobile)"]:::external

    %% API Gateway & Routing
    subgraph "API Gateway & Routing"
        ServerJS["server.js"]:::api
        AppJS["app.js"]:::api
        Env[".env_sample"]:::config
        AuthRoute["routes/auth.js"]:::api
        SensorRoute["routes/adafruitRoutes.js"]:::api
        Middleware["middlewares/middleware.js"]:::api
        SwaggerDocs["swaggerDocs.js"]:::api
        SwaggerOpt["swaggerOption.js"]:::api
    end

    %% Controllers
    subgraph "Controllers"
        AuthController["controllers/authController.js"]:::api
        SensorController["controllers/adafruitController.js"]:::api
        ControlController["controllers/controlController.js"]:::api
    end

    %% Business Logic Services
    subgraph "Business Logic Services"
        AuthService["services/authService.js"]:::service
        AdafruitService["services/adafruitService.js"]:::service
    end

    %% Persistence & Models
    subgraph "Persistence & Models"
        DBConfig["config/db.js"]:::config
        AdafruitConfig["config/adafruit.js"]:::config
        AuthModel["models/authModel.js"]:::persistence
        SensorModel["models/sensorModel.js"]:::persistence
        LimitModel["models/limitModel.js"]:::persistence
        GraphModel["models/graph.js"]:::persistence
    end

    %% External Services
    subgraph "External Services"
        MySQL["MySQL Database"]:::external
        AdafruitIO["Adafruit IO"]:::external
    end

    %% Flows
    Client -->|"HTTP Request"| AppJS
    ServerJS -->|"imports"| AppJS
    ServerJS -->|"starts server"| AppJS
    AppJS -->|"uses routes"| AuthRoute
    AppJS -->|"uses routes"| SensorRoute
    AppJS -->|"uses middleware"| Middleware
    AppJS -->|"serves docs"| SwaggerDocs
    SwaggerDocs -->|"options from"| SwaggerOpt

    AuthRoute -->|routes to| AuthController
    SensorRoute -->|routes to| SensorController

    Middleware -->|validates JWT| AuthController

    AuthController -->|calls| AuthService
    SensorController -->|calls| AdafruitService
    ControlController -->|calls| AdafruitService

    AuthService -->|queries| AuthModel
    AuthModel -->|reads/writes| MySQL

    AdafruitService -->|reads/writes| AdafruitIO
    AdafruitService -->|persists sensor data| SensorModel
    SensorModel -->|writes| MySQL

    DBConfig -->|connects to| MySQL
    AdafruitConfig -->|credentials for| AdafruitService

    %% Click Events
    click AppJS "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/app.js"
    click ServerJS "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/server.js"
    click Env "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/.env_sample"
    click DBConfig "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/config/db.js"
    click AdafruitConfig "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/config/adafruit.js"
    click Middleware "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/middlewares/middleware.js"
    click AuthRoute "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/routes/auth.js"
    click SensorRoute "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/routes/adafruitRoutes.js"
    click AuthController "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/controllers/authController.js"
    click SensorController "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/controllers/adafruitController.js"
    click ControlController "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/controllers/controlController.js"
    click AuthService "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/services/authService.js"
    click AdafruitService "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/services/adafruitService.js"
    click AuthModel "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/models/authModel.js"
    click SensorModel "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/models/sensorModel.js"
    click LimitModel "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/models/limitModel.js"
    click GraphModel "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/models/graph.js"
    click SwaggerDocs "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/swaggerDocs.js"
    click SwaggerOpt "https://github.com/do-an-da-nganh-l06/be_farm/blob/main/swaggerOption.js"

    %% Styles
    classDef api fill:#cce5ff,stroke:#004085,color:#004085
    classDef service fill:#d4edda,stroke:#155724,color:#155724
    classDef persistence fill:#ffeeba,stroke:#856404,color:#856404
    classDef external fill:#e2e3e5,stroke:#6c757d,color:#6c757d
    classDef config fill:#f5c6cb,stroke:#721c24,color:#721c24
```
