# sarthaklad1034/secqr architecture

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

- Interactive diagram: https://gitdiagram.com/sarthaklad1034/secqr
- Repository: https://github.com/sarthaklad1034/secqr

## Overview

##### Project Type and Purpose
- SecQR is a full-stack web/mobile-responsive application for secure QR code scanning.
- Frontend: React.js SPA handling camera input, UI components, API calls.
- Backend: Python Flask server exposing REST API endpoints, loading a pre-trained Random Forest model, querying MongoDB for URL overrides and history.
- Machine Learning: scikit-learn Random Forest model (.pkl) with custom feature extraction utilities.

##### File Structure
- Top Level:
- backend/
- frontend/
- Backend:
- app.py – Flask entry point with route definitions.
- models/random_forest_model.pkl – serialized ML model.
- utils/feature_extraction.py – transforms URL strings into numeric features.
- requirements.txt – Python dependencies (Flask, pymongo, scikit-learn, etc.).
- Frontend:
- public/ – static HTML, manifest.
- src/
– components/ – React view components (HomeScreen, QRScanner, SafeSection, MaliciousSection) and nested UI primitives (Card, Alert).
– services/api.js – Axios wrapper for backend endpoints.
– App.js, routing setup, global CSS and tailwind config.
- package.json – JS dependencies (react, axios, react-router, tailwindcss).

##### README for Additional Insights
- Describes endpoints: /api/scan, /api/checksafe-url, /api/checkmalicious-url.
- Tech stack summary confirms React front, Flask back, MongoDB, scikit-learn.
- Machine learning model uses URL length, domain reputation, character frequency, TLD and path analysis.

##### Main Components
a. User Device (Browser/Mobile App)
- Camera module (browser media API)
- React SPA
b. Frontend Application
- QRScanner component
- UI sections (SafeSection, MaliciousSection)
- API Service (axios calls to backend)
c. Backend API
- Flask app.py
- Route handlers (/scan, /checksafe-url, /checkmalicious-url)
d. Business Logic & ML Layer
- feature_extraction.py
- random_forest_model.pkl loader
- prediction orchestrator (apply ML → get score)
e. Database Layer
- MongoDB cluster
- URLs collection (safe vs malicious overrides)
f. External Services (optional)
- Third-party threat intelligence / future enhancements
- Hosting environment / container orchestration

##### Relationships and Data Flow
1. User scans QR → camera API → QRScanner component captures image/data.
2. Frontend calls POST /api/scan with image or URL payload via `services/api.js`.
3. Flask route receives request → passes URL to feature_extraction → transforms to vector.
4. Backend checks MongoDB overrides collection (lookup safe/malicious).
- If override exists, return that result.
- Else, load RandomForest model → predict safety → optionally store result in DB.
5. Backend sends JSON response (safe/malicious + detail report).
6. Frontend receives response → renders SafeSection or MaliciousSection UI → user sees alert.

##### Architectural Patterns and Design Principles
- Layered Architecture: Presentation (React), API (Flask), Business/ML Logic, Data.
- Separation of Concerns:
- `services/api.js` isolates HTTP logic.
- `utils/feature_extraction.py` isolates feature engineering.
- `models` folder isolates ML assets.
- Extensibility: clear override mechanism in DB; future auth/history layers can be slotted in.
- Mobile-Responsive & SPA pattern: React Router, Tailwind for CSS.

##### Detailed Capture of Essential Elements
- API Endpoints
- Feature Extraction
- Model Loader
- DB Override Service

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Device Layer
    subgraph "User Device"
        direction TB
        CameraAPI["Camera API"]:::external
    end

    %% Frontend Layer
    subgraph "Frontend (React.js SPA)"
        direction TB
        ReactSPA["React SPA"]:::frontend
        AppJS["App.js"]:::frontend
        QRScanner["QRScanner"]:::frontend
        HomeScreen["HomeScreen"]:::frontend
        SafeSection["SafeSection"]:::frontend
        MaliciousSection["MaliciousSection"]:::frontend
        AlertUI["Alert"]:::frontend
        CardUI["Card"]:::frontend
        APIService["API Service"]:::frontend
        GlobalCSS["Global CSS"]:::frontend
        TailwindConfig["Tailwind Config"]:::frontend

        ReactSPA --> AppJS
        AppJS --> QRScanner
        AppJS --> HomeScreen
        QRScanner --> APIService
        AppJS --> GlobalCSS
        AppJS --> TailwindConfig
        HomeScreen --> AlertUI
        HomeScreen --> CardUI
        QRScanner --> SafeSection
        QRScanner --> MaliciousSection
    end

    %% Backend Layer
    subgraph "Backend (Flask API)"
        direction TB
        FlaskApp["Flask app.py"]:::backend
        FeatureExtraction["feature_extraction.py"]:::ml
        ModelArtifact["random_forest_model.pkl"]:::ml
        Requirements["requirements.txt"]:::backend

        FlaskApp --> FeatureExtraction
        FeatureExtraction --> ModelArtifact
        FlaskApp --> Requirements
    end

    %% Database Layer
    MongoDB["MongoDB Cluster"]:::db

    %% External Services
    ThreatIntel["Threat Intel API (Future)"]:::external

    %% Data Flows
    CameraAPI --> ReactSPA
    APIService --> FlaskApp
    FlaskApp -->|“MongoDB query”| MongoDB
    FlaskApp -->|“Load model”| ModelArtifact
    FlaskApp -->|“feature vector”| FeatureExtraction
    MongoDB -->|“override result”| FlaskApp
    FlaskApp -->|“JSON response”| APIService
    APIService --> SafeSection
    APIService --> MaliciousSection
    ThreatIntel -.->|“future enrichment”| FlaskApp

    %% Click Events
    click ReactSPA "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/index.js"
    click AppJS "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/App.js"
    click QRScanner "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/components/QRScanner.jsx"
    click HomeScreen "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/components/HomeScreen.jsx"
    click SafeSection "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/components/SafeSection.jsx"
    click MaliciousSection "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/components/MaliciousSection.jsx"
    click AlertUI "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/components/ui/Alert.jsx"
    click CardUI "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/components/ui/Card.jsx"
    click APIService "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/services/api.js"
    click GlobalCSS "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/index.css"
    click GlobalCSS "https://github.com/sarthaklad1034/secqr/blob/main/frontend/src/App.css"
    click TailwindConfig "https://github.com/sarthaklad1034/secqr/blob/main/frontend/tailwind.config.js"
    click FlaskApp "https://github.com/sarthaklad1034/secqr/blob/main/backend/app.py"
    click FeatureExtraction "https://github.com/sarthaklad1034/secqr/blob/main/backend/utils/feature_extraction.py"
    click ModelArtifact "https://github.com/sarthaklad1034/secqr/blob/main/backend/models/random_forest_model.pkl"
    click Requirements "https://github.com/sarthaklad1034/secqr/blob/main/backend/requirements.txt"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef backend fill:#90EE90,stroke:#333,stroke-width:1px
    classDef ml fill:#FFA500,stroke:#333,stroke-width:1px
    classDef db fill:#DDA0DD,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px
```
