# s0sp/ruralroots architecture

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

- Interactive diagram: https://gitdiagram.com/s0sp/ruralroots
- Repository: https://github.com/s0sp/ruralroots

## Overview

##### Project Type and Purpose
- From the file tree and README, this is a full-stack, serverless web application (“Rural Roots”) built with React/TypeScript on the frontend, hosted on Netlify, with serverless backend logic (Netlify Functions) and integration with external services (OpenWeatherMap API, SMS gateway, optional AI APIs). Its purpose is to assist farmers by providing weather forecasts, disease diagnosis tools, and SMS alerts.

##### High-Level Architecture Components
a. Frontend SPA
- Hosted on Netlify as static assets (index.html + JS/CSS bundles in `public/` and `src/`).
- Built with React, TypeScript, React Router, styled-components.
- Core modules:
– UI components (`src/components/*`)
– Page views (`src/pages/*`)
– Context & auth (`src/contexts/AuthContext.tsx`, `src/config/firebase.ts`)
– Services for direct API calls from client (`src/services/*`, e.g. `weatherService.ts`, `placesService.ts`, `smsService.ts`).
b. Serverless Backend (Netlify Functions)
- `netlify/functions/api.js` and `netlify/functions/sms.js` wrap Node handlers.
- Underlying business logic resides in `src/server/` (controllers, routes, services):
– `weatherAlertController.js`, `alertRoutes.js` → schedule weather alerts
– `smsRoutes.js`, `sms.js` → interface with SMS gateway
- Uses cron/scheduler for alerts (`alertScheduler.js`) and third-party HTTP clients (`weatherApiService.js`).
c. External Services
- OpenWeatherMap (weather data)
- SMS gateway (Twilio or similar)
- Optional AI API for disease detection (mocked now via `geminiService.ts`)
- (Future) Firebase Auth + Firestore for user management

##### Relationships and Data Flows
- User’s Browser ↔ React SPA ↔ Netlify Functions
→ Weather endpoints → OpenWeatherMap
→ SMS endpoints → SMS gateway
→ (Future) Auth calls → Firebase
- Scheduler Function (triggered on Netlify) → checks weather conditions → sends SMS via gateway
- Disease Detection: Image uploaded in SPA → processed client-side or forwarded to AI API

##### Architectural Patterns and Design Principles
- SPA on a CDN behind a serverless BFF (Backend-for-Frontend)
- Separation of concerns:
- UI layer (components/pages)
- Service layer (API wrappers in `src/services`)
- Backend logic (controllers/services under `src/server`)
- Use of contexts for cross-cutting concerns (AuthContext)
- Modular routing in serverless functions

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend SPA" 
        ReactSPA["React SPA\n(TypeScript, React Router,\nstyled-components)"]:::frontend
        subgraph "UI Components"
            ComponentsDir["src/components/"]:::frontend
            Disease["DiseaseDetection.tsx"]:::frontend
            Header["Header.tsx"]:::frontend
            ProtectedRoute["ProtectedRoute.tsx"]:::frontend
            ServicesCards["ServiceCards (Weather,SMS,...)"]:::frontend
            WeatherCards["WeatherCards.tsx"]:::frontend
        end
        subgraph "Pages"
            PagesDir["src/pages/"]:::frontend
            Home["Home.tsx"]:::frontend
            Community["Community.tsx"]:::frontend
            Farm["FarmDashboard.tsx"]:::frontend
            Schemes["SchemesPage.tsx"]:::frontend
            ServicesPage["Services.tsx"]:::frontend
        end
        subgraph "Client Services"
            ServicesDir["src/services/"]:::frontend
            WeatherService["weatherService.ts"]:::frontend
            PlacesService["placesService.ts"]:::frontend
            SMSService["smsService.ts"]:::frontend
            GeminiService["geminiService.ts"]:::frontend
        end
        AuthContext["AuthContext.tsx"]:::frontend
        FirebaseConfig["firebase.ts"]:::frontend
        App["App.tsx"]:::frontend
        MainEntry["main.tsx"]:::frontend
        Index["index.tsx"]:::frontend
    end

    %% CDN / Hosting
    Browser["User Browser"]:::external
    CDN["Netlify CDN (Static Hosting)"]:::frontend

    %% Serverless Backend
    subgraph "Netlify Functions" 
        APIFunc["API Function\nnetlify/functions/api.js"]:::backend
        SMSFunc["SMS Function\nnetlify/functions/sms.js"]:::backend
    end
    subgraph "Backend Logic (src/server)"
        ServerIndex["index.js"]:::backend
        Controllers["weatherAlertController.js"]:::backend
        RoutesAlert["alertRoutes.js"]:::backend
        RoutesSMS["smsRoutes.js"]:::backend
        AlertScheduler["alertScheduler.js"]:::backend
        WeatherApiService["weatherApiService.js"]:::backend
    end

    %% External Services
    OpenWeather["OpenWeatherMap API"]:::external
    SMSGateway["SMS Gateway"]:::external
    AIAPI["AI Disease Detection API"]:::external
    FirebaseAuth["Firebase Auth & Firestore"]:::external

    %% Connections
    Browser -->|"loads SPA"| CDN
    CDN --> ReactSPA
    ReactSPA -->|"GET /forecast"| APIFunc
    ReactSPA -->|"POST /sms"| SMSFunc
    ReactSPA -->|"GET /weather"| OpenWeather
    ReactSPA -->|"detect disease"| GeminiService
    APIFunc -->|uses| WeatherApiService
    APIFunc -->|"GET weather data"| OpenWeather
    SMSFunc -->|"send SMS"| SMSGateway
    AlertScheduler -->|trigger scheduler| SMSFunc
    AlertScheduler -->|check weather| OpenWeather
    Controllers -->|schedule alerts| AlertScheduler
    RoutesAlert --> Controllers
    RoutesSMS --> SMSFunc
    ServerIndex --> RoutesAlert
    ServerIndex --> RoutesSMS

    %% Click Events
    click ComponentsDir "https://github.com/s0sp/ruralroots/tree/main/src/components/"
    click Disease "https://github.com/s0sp/ruralroots/blob/main/src/components/DiseaseDetection/DiseaseDetection.tsx"
    click Header "https://github.com/s0sp/ruralroots/blob/main/src/components/Header/Header.tsx"
    click ProtectedRoute "https://github.com/s0sp/ruralroots/blob/main/src/components/ProtectedRoute.tsx"
    click ServicesCards "https://github.com/s0sp/ruralroots/tree/main/src/components/Services/"
    click WeatherCards "https://github.com/s0sp/ruralroots/blob/main/src/components/Weather/WeatherCards.tsx"
    click PagesDir "https://github.com/s0sp/ruralroots/tree/main/src/pages/"
    click Home "https://github.com/s0sp/ruralroots/blob/main/src/pages/Home.tsx"
    click Community "https://github.com/s0sp/ruralroots/blob/main/src/pages/Community.tsx"
    click Farm "https://github.com/s0sp/ruralroots/blob/main/src/pages/FarmDashboard.tsx"
    click Schemes "https://github.com/s0sp/ruralroots/blob/main/src/pages/SchemesPage.tsx"
    click ServicesPage "https://github.com/s0sp/ruralroots/blob/main/src/pages/Services.tsx"
    click ServicesDir "https://github.com/s0sp/ruralroots/tree/main/src/services/"
    click WeatherService "https://github.com/s0sp/ruralroots/blob/main/src/services/weatherService.ts"
    click PlacesService "https://github.com/s0sp/ruralroots/blob/main/src/services/placesService.ts"
    click SMSService "https://github.com/s0sp/ruralroots/blob/main/src/services/smsService.ts"
    click GeminiService "https://github.com/s0sp/ruralroots/blob/main/src/services/geminiService.ts"
    click AuthContext "https://github.com/s0sp/ruralroots/blob/main/src/contexts/AuthContext.tsx"
    click FirebaseConfig "https://github.com/s0sp/ruralroots/blob/main/src/config/firebase.ts"
    click App "https://github.com/s0sp/ruralroots/blob/main/src/App.tsx"
    click MainEntry "https://github.com/s0sp/ruralroots/blob/main/src/main.tsx"
    click Index "https://github.com/s0sp/ruralroots/blob/main/src/index.tsx"
    click APIFunc "https://github.com/s0sp/ruralroots/blob/main/netlify/functions/api.js"
    click SMSFunc "https://github.com/s0sp/ruralroots/blob/main/netlify/functions/sms.js"
    click ServerIndex "https://github.com/s0sp/ruralroots/blob/main/src/server/index.js"
    click Controllers "https://github.com/s0sp/ruralroots/blob/main/src/server/controllers/weatherAlertController.js"
    click RoutesAlert "https://github.com/s0sp/ruralroots/blob/main/src/server/routes/alertRoutes.js"
    click RoutesSMS "https://github.com/s0sp/ruralroots/blob/main/src/server/routes/smsRoutes.js"
    click AlertScheduler "https://github.com/s0sp/ruralroots/blob/main/src/server/services/alertScheduler.js"
    click WeatherApiService "https://github.com/s0sp/ruralroots/blob/main/src/server/services/weatherApiService.js"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#3399ff,color:#000
    classDef backend fill:#d5f5d5,stroke:#27ae60,color:#000
    classDef external fill:#fdebd0,stroke:#e67e22,color:#000
```
