# rahul-suthar/flockguard architecture

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

- Interactive diagram: https://gitdiagram.com/rahul-suthar/flockguard
- Repository: https://github.com/rahul-suthar/flockguard

## Overview

#### Purpose and Deliverables
FlockGuard is a livestock-health platform for pig and poultry farms. It combines a React Native farmer-facing app, an Express/MongoDB API, a Flask disease-classification service, and a lightweight React/Vite public site. Core outcomes are farm records, disease-image prediction, health reports, role-based access, and veterinary/pharmacy workflows.

#### Client Entry Points
The public web client is a Vite React application rooted at `client/src/main.jsx` and `client/src/App.jsx`. It primarily serves informational, onboarding, contact, legal, and download-facing pages, structured with shared navigation/layout components in `client/src/components/NavBar.jsx` and `client/src/components/DocLayout.jsx`. Deployment configuration targets Vercel via `client/vercel.json`.

#### Mobile Application and Navigation
The primary operational UI is the React Native app, bootstrapped through `mobile/index.js` and `mobile/App.jsx`. Navigation composes stack and tab flows in `mobile/src/navigations/StackNavigator.jsx`, `mobile/src/navigations/TabNavigator.jsx`, and `mobile/src/navigations/HomeStackNav.jsx`. Major workflows are represented by login/registration, farm/home, diagnosis details, vet, and profile screens under `mobile/src/screens/`.

#### Mobile State and API Boundary
Cross-screen concerns use React contexts: authentication, camera capture, health/report state, loader, popup, and theme state in `mobile/src/context/Auth.context.js`, `mobile/src/context/Camera.context.js`, and `mobile/src/context/Health.context.js`. The app reaches the backend through API-domain modules—`mobile/src/apis/api.js`, `mobile/src/apis/auth.js`, `mobile/src/apis/farm.js`, and `mobile/src/apis/report.js`—which form the client/server boundary. Camera-driven image collection is encapsulated in `mobile/src/components/CameraView.jsx`.

#### Backend API and Authorization
The Node.js/Express service starts from `server/src/index.js`, with application assembly in `server/src/app.js`. Route groups separate authentication/admin, farms, and reports in `server/src/routes/auth.routes.js`, `server/src/routes/farm.routes.js`, and `server/src/routes/report.routes.js`. Controllers implement corresponding business operations in `server/src/controllers/user.controller.js`, `server/src/controllers/farm.controller.js`, and `server/src/controllers/report.controller.js`. JWT/role enforcement and multipart image intake are boundary middleware in `server/src/middlewares/auth.middleware.js`, `server/src/middlewares/role.middleware.js`, and `server/src/middlewares/multer.middleware.js`.

#### Persistent Domain and External Services
MongoDB persistence is initialized in `server/src/db/index.js`; Mongoose models capture users, farms, reports, alerts, vets, pharmacies, and related roles, including `server/src/models/user.models.js`, `server/src/models/farm.models.js`, and `server/src/models/report.models.js`. Uploaded diagnostic images pass through temporary server storage (`server/public/temp/.gitkeep`) and Cloudinary integration (`server/src/utils/cloudinary.js`). The backend converts ML output into application-facing report data through `server/src/utils/mlRespose.js`.

#### ML Prediction Service and Report Flow
The Flask service entry point is `ml/app.py`; health and prediction endpoints are split into `ml/routes/health.py` and `ml/routes/predict.py`. It loads the serialized disease model at `ml/models/disease_model.pkl`. The principal flow is: authenticated mobile user creates/selects a farm, captures/uploads an animal image, Express stores/forwards image data and invokes ML prediction, then persists prediction-linked health report data for later vet notes, prescriptions, and alert-related follow-up.

Main entry points: [`server/src/controllers/report.controller.js`](https://github.com/rahul-suthar/flockguard/blob/main/server/src/controllers/report.controller.js) (Domain controllers), [`ml/app.py`](https://github.com/rahul-suthar/flockguard/blob/main/ml/app.py) (Flask prediction API), [`mobile/src/apis/api.js`](https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/apis/api.js) (API domain modules), [`mobile/src/navigations/StackNavigator.jsx`](https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/navigations/StackNavigator.jsx) (App navigation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public Web"]
  node_web_entry["Vite React site<br/>web client<br/>[main.jsx]"]
  node_web_layout["Shared web layout<br/>React components<br/>[NavBar.jsx]"]
end

subgraph group_mobile["Farmer Mobile App"]
  node_mobile_entry["React Native app<br/>mobile client<br/>[index.js]"]
  node_mobile_navigation["App navigation<br/>stack and tabs<br/>[StackNavigator.jsx]"]
  node_mobile_workflows["Farm, diagnosis, vet and profile<br/>mobile screens"]
  node_mobile_state["Cross-screen state<br/>React contexts<br/>[Auth.context.js]"]
  node_camera["Camera capture<br/>image collection<br/>[CameraView.jsx]"]
  node_mobile_api{{"API domain modules<br/>client API boundary<br/>[api.js]"}}
end

subgraph group_api["Platform API"]
  node_express_entry["Express service<br/>Node.js API<br/>[index.js]"]
  node_routes["Auth, farm and report routes<br/>route groups<br/>[auth.routes.js]"]
  node_access_middleware{{"JWT, roles and uploads<br/>boundary middleware<br/>[auth.middleware.js]"}}
  node_controllers["Domain controllers<br/>business operations"]
  node_mongo[("MongoDB domain data<br/>Mongoose persistence<br/>[index.js]")]
  node_image_storage["Diagnostic image storage<br/>temporary and Cloudinary<br/>[cloudinary.js]"]
  node_ml_adapter["ML result adapter<br/>report transformation<br/>[mlRespose.js]"]
end

subgraph group_ml["Disease ML Service"]
  node_flask["Flask prediction API<br/>ML service<br/>[app.py]"]
  node_predict_route["Prediction endpoint<br/>Flask route<br/>[predict.py]"]
  node_disease_model[("Disease classifier<br/>serialized model<br/>[disease_model.pkl]")]
end

node_web_entry -->|"renders"| node_web_layout
node_mobile_entry -->|"boots"| node_mobile_navigation
node_mobile_navigation -->|"routes to"| node_mobile_workflows
node_mobile_state -->|"supplies state"| node_mobile_workflows
node_mobile_workflows -->|"captures images"| node_camera
node_camera -->|"uploads image"| node_mobile_api
node_mobile_workflows -->|"farm and report requests"| node_mobile_api
node_mobile_api -->|"HTTPS API"| node_express_entry
node_express_entry -->|"mounts"| node_routes
node_routes -->|"guards requests"| node_access_middleware
node_access_middleware -->|"authorizes and passes uploads"| node_controllers
node_controllers -->|"reads and writes"| node_mongo
node_controllers -->|"stores diagnostic images"| node_image_storage
node_controllers -->|"requests prediction"| node_flask
node_flask -->|"serves"| node_predict_route
node_predict_route -->|"loads and infers"| node_disease_model
node_flask -->|"returns ML output"| node_ml_adapter
node_ml_adapter -->|"persists prediction-linked report"| node_mongo

click node_web_entry "https://github.com/rahul-suthar/flockguard/blob/main/client/src/main.jsx"
click node_web_layout "https://github.com/rahul-suthar/flockguard/blob/main/client/src/components/NavBar.jsx"
click node_mobile_entry "https://github.com/rahul-suthar/flockguard/blob/main/mobile/index.js"
click node_mobile_navigation "https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/navigations/StackNavigator.jsx"
click node_mobile_state "https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/context/Auth.context.js"
click node_camera "https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/components/CameraView.jsx"
click node_mobile_api "https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/apis/api.js"
click node_express_entry "https://github.com/rahul-suthar/flockguard/blob/main/server/src/index.js"
click node_routes "https://github.com/rahul-suthar/flockguard/blob/main/server/src/routes/auth.routes.js"
click node_access_middleware "https://github.com/rahul-suthar/flockguard/blob/main/server/src/middlewares/auth.middleware.js"
click node_controllers "https://github.com/rahul-suthar/flockguard/blob/main/server/src/controllers/report.controller.js"
click node_mongo "https://github.com/rahul-suthar/flockguard/blob/main/server/src/db/index.js"
click node_image_storage "https://github.com/rahul-suthar/flockguard/blob/main/server/src/utils/cloudinary.js"
click node_ml_adapter "https://github.com/rahul-suthar/flockguard/blob/main/server/src/utils/mlRespose.js"
click node_flask "https://github.com/rahul-suthar/flockguard/blob/main/ml/app.py"
click node_predict_route "https://github.com/rahul-suthar/flockguard/blob/main/ml/routes/predict.py"
click node_disease_model "https://github.com/rahul-suthar/flockguard/blob/main/ml/models/disease_model.pkl"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web_entry,node_web_layout toneBlue
class node_mobile_entry,node_mobile_navigation,node_mobile_workflows,node_mobile_state,node_camera,node_mobile_api toneAmber
class node_express_entry,node_routes,node_access_middleware,node_controllers,node_mongo,node_image_storage,node_ml_adapter toneMint
class node_flask,node_predict_route,node_disease_model toneRose
```

## Components

### Public Web

- [Vite React site](https://github.com/rahul-suthar/flockguard/blob/main/client/src/main.jsx): web client · Informational, onboarding, legal, and download pages. · `client/src/main.jsx`
- [Shared web layout](https://github.com/rahul-suthar/flockguard/blob/main/client/src/components/NavBar.jsx): React components · `client/src/components/NavBar.jsx`

### Farmer Mobile App

- [React Native app](https://github.com/rahul-suthar/flockguard/blob/main/mobile/index.js): mobile client · `mobile/index.js`
- [App navigation](https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/navigations/StackNavigator.jsx): stack and tabs · `mobile/src/navigations/StackNavigator.jsx`
- **Farm, diagnosis, vet and profile**: mobile screens
- [Cross-screen state](https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/context/Auth.context.js): React contexts · Authentication, camera, health reports, loader, popup, and theme state. · `mobile/src/context/Auth.context.js`
- [Camera capture](https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/components/CameraView.jsx): image collection · `mobile/src/components/CameraView.jsx`
- [API domain modules](https://github.com/rahul-suthar/flockguard/blob/main/mobile/src/apis/api.js): client API boundary · `mobile/src/apis/api.js`

### Platform API

- [Express service](https://github.com/rahul-suthar/flockguard/blob/main/server/src/index.js): Node.js API · `server/src/index.js`
- [Auth, farm and report routes](https://github.com/rahul-suthar/flockguard/blob/main/server/src/routes/auth.routes.js): route groups · `server/src/routes/auth.routes.js`
- [JWT, roles and uploads](https://github.com/rahul-suthar/flockguard/blob/main/server/src/middlewares/auth.middleware.js): boundary middleware · Enforces access and accepts multipart diagnostic images. · `server/src/middlewares/auth.middleware.js`
- [Domain controllers](https://github.com/rahul-suthar/flockguard/blob/main/server/src/controllers/report.controller.js): business operations · `server/src/controllers/report.controller.js`
- [MongoDB domain data](https://github.com/rahul-suthar/flockguard/blob/main/server/src/db/index.js): Mongoose persistence · Users, farms, reports, alerts, vets, pharmacies, and roles. · `server/src/db/index.js`
- [Diagnostic image storage](https://github.com/rahul-suthar/flockguard/blob/main/server/src/utils/cloudinary.js): temporary and Cloudinary · `server/src/utils/cloudinary.js`
- [ML result adapter](https://github.com/rahul-suthar/flockguard/blob/main/server/src/utils/mlRespose.js): report transformation · Converts prediction output into application report data. · `server/src/utils/mlRespose.js`

### Disease ML Service

- [Flask prediction API](https://github.com/rahul-suthar/flockguard/blob/main/ml/app.py): ML service · `ml/app.py`
- [Prediction endpoint](https://github.com/rahul-suthar/flockguard/blob/main/ml/routes/predict.py): Flask route · `ml/routes/predict.py`
- [Disease classifier](https://github.com/rahul-suthar/flockguard/blob/main/ml/models/disease_model.pkl): serialized model · `ml/models/disease_model.pkl`

## Connections

- Vite React site → Shared web layout: renders
- React Native app → App navigation: boots
- App navigation → Farm, diagnosis, vet and profile: routes to
- Cross-screen state → Farm, diagnosis, vet and profile: supplies state
- Farm, diagnosis, vet and profile → Camera capture: captures images
- Camera capture → API domain modules: uploads image
- Farm, diagnosis, vet and profile → API domain modules: farm and report requests
- API domain modules → Express service: HTTPS API
- Express service → Auth, farm and report routes: mounts
- Auth, farm and report routes → JWT, roles and uploads: guards requests
- JWT, roles and uploads → Domain controllers: authorizes and passes uploads
- Domain controllers → MongoDB domain data: reads and writes
- Domain controllers → Diagnostic image storage: stores diagnostic images
- Domain controllers → Flask prediction API: requests prediction
- Flask prediction API → Prediction endpoint: serves
- Prediction endpoint → Disease classifier: loads and infers
- Flask prediction API → ML result adapter: returns ML output
- ML result adapter → MongoDB domain data: persists prediction-linked report
