# prasooncodes/harvesthub architecture

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

- Interactive diagram: https://gitdiagram.com/prasooncodes/harvesthub
- Repository: https://github.com/prasooncodes/harvesthub

## Overview

##### Project Type and Purpose
- HarvestHub is a full-stack agricultural recommendation and community forum platform.
- It consists of three major parts:
a. A React.js SPA (client/) for farmers to view recommendations, browse posts, and interact.
b. A Node.js/Express.js REST API backend (server/) implementing MVC patterns, user auth, forum endpoints, and a proxy to ML.
c. A Flask-based ML microservice (ML/) that loads trained crop-recommendation models and exposes prediction endpoints.

##### File Structure Analysis
- Top-level folders:
- client/ – React project with src/, public/, i18n, components, pages, util for localization, API wrappers, and UI.
- server/ – Express app with index.js entry, config/db.js, Models/, Controllers/, Routes/, Middlewares/, util for JWT.
- ML/ – Python service with app.py, pretrained models (*.pkl), dataset, notebooks.
- Key patterns:
- Backend uses MVC: Models (Mongoose schemas), Controllers (business logic), Routes (HTTP layer), Middlewares (auth).
- ML is decoupled as a microservice (Flask) that the backend can call over HTTP.
- Config & Deployment:
- client/vite.config.js, vercel.json for frontend hosting.
- server uses package.json scripts.
- ML uses pip-installed dependencies.

##### README Insights
- Confirms MERN + Flask + ML ensemble.
- Notes use of JWT/Bcrypt for security.
- Describes how to start each service locally on different ports (Node, React dev server, Flask).

a. Main Components
1. User’s Browser / React SPA
2. Node.js/Express REST API
3. Flask ML Service
4. MongoDB Database
5. (Optional) Hosting/CI – Vercel for client, Heroku/AWS/GCP for server & ML
6. Third-party libs: JWT, Bcrypt, Mongoose, Scikit-Learn, Pandas
b. Relationships & Data Flow
1. Browser → REST API: HTTP(S) calls for auth, forum, crop data.
2. REST API → MongoDB: Mongoose ORM queries for Users, Posts, Comments, Crops.
3. REST API → Flask ML: HTTP POST with soil params → JSON response with crop recommendation.
4. ML Service → (uses) preloaded model files (minmaxscaler.pkl, model.pkl, standscaler.pkl)
5. Browser ← REST API responses render in UI components.
c. Architectural Patterns
- Frontend: Component-based React, client-side routing, i18n.
- Backend: MVC, layered security (AuthMiddleware), token-based stateless sessions.
- Microservice: Flask service solely for ML inference.
- Data persistence: NoSQL MongoDB, using Mongoose ODM.
d. Key Technologies
- React (Vite), React Router, Context or direct API calls (api.js).
- Express.js, Node.js, JWT & Bcrypt for auth, Mongoose for data models.
- Flask, Scikit-Learn, pickled models for ML.

##### Detailed Elements to Capture
- Clear labels: “React SPA (port 3000)”, “Express API (port 5000)”, “Flask ML (port 5001)”.
- Distinguish internal modules vs. external dependencies (MongoDB Atlas, third-party APIs).
- Callouts for security: Bcrypt hash & JWT verify in AuthController/Middleware.
- Internationalization subsystem in React (i18n.js and locales).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer" 
        direction TB
        ReactSPA["React SPA (port 3000)"]:::frontend
        MainEntry["main.jsx"]:::frontend
        RootComp["App.jsx"]:::frontend
        Pages["Pages Directory"]:::frontend
        Components["UI Components"]:::frontend
        I18n["i18n.js & Locales"]:::frontend
        APIWrapper["api.js & url.js"]:::frontend
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        ExpressAPI["Express API (port 5000)"]:::backend
        APIEntry["index.js"]:::backend
        DBConfig["db.js"]:::backend
        Models["Models Directory"]:::backend
        Controllers["Controllers Directory"]:::backend
        Routes["Routes Directory"]:::backend
        AuthMiddleware["AuthMiddleware.js"]:::backend
        JWTUtil["SecretToken.js"]:::backend
    end

    %% ML Microservice
    subgraph "ML Microservice"
        direction TB
        MLService["Flask ML Service (port 5001)"]:::ml
        FlaskApp["app.py"]:::ml
        ModelPkl["model.pkl"]:::ml
        Scaler1["minmaxscaler.pkl"]:::ml
        Scaler2["standscaler.pkl"]:::ml
        Dataset["Crop_recommendation.csv"]:::ml
        InspectScript["inspect_model.py"]:::ml
    end

    %% Data Layer
    MongoDB[(MongoDB)]:::database

    %% Relationships
    ReactSPA -->|"HTTP GET /posts, POST /recommend"| ExpressAPI
    ExpressAPI -->|"CRUD via Mongoose"| MongoDB
    ExpressAPI -->|"HTTP POST /recommend"| MLService
    MLService -->|"Loads models"| ModelPkl
    MLService -->|"Loads scaler"| Scaler1
    MLService -->|"Loads scaler"| Scaler2
    MLService -->|"Uses dataset"| Dataset

    MainEntry --> RootComp
    RootComp --> Pages
    RootComp --> Components
    RootComp --> I18n
    RootComp --> APIWrapper

    ExpressAPI --> APIEntry
    ExpressAPI --> DBConfig
    ExpressAPI --> Models
    ExpressAPI --> Controllers
    ExpressAPI --> Routes
    ExpressAPI --> AuthMiddleware
    ExpressAPI --> JWTUtil

    %% Click Events
    click ReactSPA "https://github.com/prasooncodes/harvesthub/tree/main/client/"
    click MainEntry "https://github.com/prasooncodes/harvesthub/blob/main/client/src/main.jsx"
    click RootComp "https://github.com/prasooncodes/harvesthub/blob/main/client/src/App.jsx"
    click Pages "https://github.com/prasooncodes/harvesthub/tree/main/client/src/pages/"
    click Components "https://github.com/prasooncodes/harvesthub/tree/main/client/src/components/"
    click I18n "https://github.com/prasooncodes/harvesthub/blob/main/client/src/i18n.js"
    click APIWrapper "https://github.com/prasooncodes/harvesthub/blob/main/client/src/api.js"
    click ExpressAPI "https://github.com/prasooncodes/harvesthub/tree/main/server/"
    click APIEntry "https://github.com/prasooncodes/harvesthub/blob/main/server/index.js"
    click DBConfig "https://github.com/prasooncodes/harvesthub/blob/main/server/config/db.js"
    click Models "https://github.com/prasooncodes/harvesthub/tree/main/server/Models/"
    click Controllers "https://github.com/prasooncodes/harvesthub/tree/main/server/Controllers/"
    click Routes "https://github.com/prasooncodes/harvesthub/tree/main/server/Routes/"
    click AuthMiddleware "https://github.com/prasooncodes/harvesthub/blob/main/server/Middlewares/AuthMiddleware.js"
    click JWTUtil "https://github.com/prasooncodes/harvesthub/blob/main/server/util/SecretToken.js"
    click MLService "https://github.com/prasooncodes/harvesthub/tree/main/ML/"
    click FlaskApp "https://github.com/prasooncodes/harvesthub/blob/main/ML/app.py"
    click ModelPkl "https://github.com/prasooncodes/harvesthub/blob/main/ML/model.pkl"
    click Scaler1 "https://github.com/prasooncodes/harvesthub/blob/main/ML/minmaxscaler.pkl"
    click Scaler2 "https://github.com/prasooncodes/harvesthub/blob/main/ML/standscaler.pkl"
    click Dataset "https://github.com/prasooncodes/harvesthub/blob/main/ML/dataset/Crop_recommendation.csv"
    click InspectScript "https://github.com/prasooncodes/harvesthub/blob/main/ML/inspect_model.py"

    %% Styles
    classDef frontend fill:#D0E6FF,stroke:#0366d6,color:#0366d6;
    classDef backend fill:#E6FFEA,stroke:#28a745,color:#28a745;
    classDef ml fill:#FFF5CC,stroke:#dbab09,color:#dbab09;
    classDef database fill:#F0F0F0,stroke:#666666,color:#333333;
```
