Skip to content

nandiniraygithub/Backend_cal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Math Calculator - Architecture Guide

πŸ—οΈ Project Structure

backend-cal/
β”œβ”€β”€ backend/                    # Backend Node.js API
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ controllers/        # Route controllers
β”‚   β”‚   β”‚   β”œβ”€β”€ imageController.js
β”‚   β”‚   β”‚   └── analysisController.js
β”‚   β”‚   β”œβ”€β”€ middleware/         # Custom middleware
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.js
β”‚   β”‚   β”‚   β”œβ”€β”€ errorHandler.js
β”‚   β”‚   β”‚   └── validation.js
β”‚   β”‚   β”œβ”€β”€ models/            # Database models
β”‚   β”‚   β”‚   └── ImageModel.js
β”‚   β”‚   β”œβ”€β”€ routes/            # API routes
β”‚   β”‚   β”‚   β”œβ”€β”€ image.js
β”‚   β”‚   β”‚   └── analysis.js
β”‚   β”‚   β”œβ”€β”€ services/          # Business logic
β”‚   β”‚   β”‚   β”œβ”€β”€ ollamaService.js
β”‚   β”‚   β”‚   β”œβ”€β”€ imageProcessor.js
β”‚   β”‚   β”‚   └── cacheService.js
β”‚   β”‚   β”œβ”€β”€ utils/             # Utility functions
β”‚   β”‚   β”‚   β”œβ”€β”€ logger.js
β”‚   β”‚   β”‚   └── helpers.js
β”‚   β”‚   └── config/            # Configuration
β”‚   β”‚       β”œβ”€β”€ database.js
β”‚   β”‚       └── ollama.js
β”‚   β”œβ”€β”€ tests/                 # Test files
β”‚   β”œβ”€β”€ uploads/               # File uploads
β”‚   β”œβ”€β”€ package.json
β”‚   └── server.js              # Entry point
β”œβ”€β”€ frontend/                   # Frontend React App
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/         # Reusable components
β”‚   β”‚   β”‚   β”œβ”€β”€ ui/            # Basic UI components
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Button.tsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Input.tsx
β”‚   β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ canvas/        # Canvas-related components
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ FabricCanvas.tsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Toolbar.tsx
β”‚   β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ analysis/      # Analysis components
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ResultDisplay.tsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ LoadingState.tsx
β”‚   β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”‚   └── layout/        # Layout components
β”‚   β”‚   β”‚       β”œβ”€β”€ Navbar.tsx
β”‚   β”‚   β”‚       β”œβ”€β”€ Sidebar.tsx
β”‚   β”‚   β”‚       └── index.ts
β”‚   β”‚   β”œβ”€β”€ screens/           # Page components
β”‚   β”‚   β”‚   β”œβ”€β”€ Home/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ index.tsx
β”‚   β”‚   β”‚   β”‚   └── Home.styles.ts
β”‚   β”‚   β”‚   └── About/
β”‚   β”‚   β”œβ”€β”€ hooks/             # Custom React hooks
β”‚   β”‚   β”‚   β”œβ”€β”€ useCanvas.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ useAnalysis.ts
β”‚   β”‚   β”‚   └── useLocalStorage.ts
β”‚   β”‚   β”œβ”€β”€ services/          # API services
β”‚   β”‚   β”‚   β”œβ”€β”€ api.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ imageService.ts
β”‚   β”‚   β”‚   └── analysisService.ts
β”‚   β”‚   β”œβ”€β”€ store/             # State management
β”‚   β”‚   β”‚   β”œβ”€β”€ context/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ AppContext.tsx
β”‚   β”‚   β”‚   β”‚   └── CanvasContext.tsx
β”‚   β”‚   β”‚   └── slices/
β”‚   β”‚   β”‚       β”œβ”€β”€ canvasSlice.ts
β”‚   β”‚   β”‚       └── analysisSlice.ts
β”‚   β”‚   β”œβ”€β”€ types/             # TypeScript types
β”‚   β”‚   β”‚   β”œβ”€β”€ canvas.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ analysis.ts
β”‚   β”‚   β”‚   └── api.ts
β”‚   β”‚   β”œβ”€β”€ utils/             # Utility functions
β”‚   β”‚   β”‚   β”œβ”€β”€ constants.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ helpers.ts
β”‚   β”‚   β”‚   └── validators.ts
β”‚   β”‚   β”œβ”€β”€ styles/            # Global styles
β”‚   β”‚   β”‚   β”œβ”€β”€ globals.css
β”‚   β”‚   β”‚   └── components.css
β”‚   β”‚   └── config/            # Configuration
β”‚   β”‚       β”œβ”€β”€ api.ts
β”‚   β”‚       └── env.ts
β”‚   β”œβ”€β”€ public/
β”‚   └── package.json
β”œβ”€β”€ shared/                     # Shared types and utilities
β”‚   β”œβ”€β”€ types/
β”‚   β”‚   β”œβ”€β”€ analysis.ts
β”‚   β”‚   └── api.ts
β”‚   └── constants/
β”‚       └── errors.ts
β”œβ”€β”€ docs/                      # Documentation
β”œβ”€β”€ scripts/                   # Build and deployment scripts
└── README.md

πŸ”„ How It Works - Architecture Flow

Backend Architecture (Node.js + Express)

Client Request β†’ Express Server β†’ Middleware β†’ Controller β†’ Service β†’ Ollama AI β†’ Response
                                     ↓
                              MongoDB (Image Storage)
                                     ↓
                              In-Memory Cache

Key Components:

  1. Express Server (server.js)

    • Main entry point
    • Sets up middleware and routes
    • Handles error management
  2. Controllers (controllers/)

    • Handle HTTP requests/responses
    • Validate input data
    • Call appropriate services
  3. Services (services/)

    • OllamaService: Communicates with Ollama AI model
    • ImageProcessor: Optimizes and processes images
    • CacheService: Manages in-memory caching
  4. Models (models/)

    • ImageModel: MongoDB schema for image storage

Frontend Architecture (React + TypeScript)

User Interaction β†’ Component β†’ Hook/Context β†’ API Service β†’ Backend API β†’ Response β†’ State Update β†’ UI Re-render

Key Components:

  1. Components (src/components/)

    • Canvas Components: Drawing and canvas management
    • Analysis Components: Results and loading states
    • UI Components: Reusable UI elements
  2. Hooks (src/hooks/)

    • useCanvas: Canvas drawing logic
    • useAnalysis: Analysis state management
    • useLocalStorage: Local storage operations
  3. Services (src/services/)

    • API Service: HTTP client for backend communication
    • Image Service: Image upload and processing
    • Analysis Service: Analysis requests and responses
  4. State Management (src/store/)

    • Context API: Global state management
    • Local State: Component-specific state

πŸš€ Data Flow

1. Drawing Flow

User draws on canvas β†’ Fabric.js captures drawing β†’ Canvas state saved β†’ Ready state shown

2. Analysis Flow

User clicks calculate β†’ Loading state shown β†’ Image sent to backend β†’ 
Backend processes image β†’ Ollama AI analyzes β†’ Results returned β†’ 
Frontend displays results with explanations

3. State Management

Canvas State β†’ Drawing history β†’ Undo/Redo functionality
Analysis State β†’ Loading/Ready/Result states β†’ UI updates

πŸ”§ Key Technologies

Backend

  • Node.js + Express: Server framework
  • MongoDB: Image storage
  • Ollama: AI model (Qwen2.5-VL)
  • Sharp: Image processing
  • Axios: HTTP client

Frontend

  • React 19: UI framework
  • TypeScript: Type safety
  • Fabric.js: Canvas manipulation
  • Tailwind CSS: Styling
  • Lucide React: Icons
  • Vite: Build tool

πŸ“Š API Endpoints

POST /api/image/upload

  • Uploads canvas image
  • Returns image ID
  • Stores in MongoDB

POST /api/analysis/calculate

  • Analyzes image with AI
  • Returns mathematical results
  • Includes explanations and steps

GET /api/analysis/cache-status

  • Returns cache statistics
  • For monitoring and debugging

🎯 Best Practices

Backend

  1. Error Handling: Centralized error management
  2. Validation: Input validation for all endpoints
  3. Caching: In-memory caching for performance
  4. Logging: Comprehensive logging for debugging

Frontend

  1. Type Safety: Strict TypeScript usage
  2. Component Reusability: Modular component design
  3. State Management: Proper state separation
  4. Performance: Optimized rendering and caching

πŸ”’ Security Considerations

  1. Input Validation: All user inputs validated
  2. File Uploads: Size and type restrictions
  3. Error Messages: No sensitive information leaked
  4. Rate Limiting: Prevent API abuse

πŸ“ˆ Performance Optimizations

  1. Image Optimization: Sharp for image compression
  2. Caching: In-memory cache for repeated requests
  3. Lazy Loading: Components loaded as needed
  4. Code Splitting: Frontend bundle optimization

πŸ§ͺ Testing Strategy

  1. Unit Tests: Individual function testing
  2. Integration Tests: API endpoint testing
  3. E2E Tests: Full user flow testing
  4. Performance Tests: Load and stress testing

This architecture ensures scalability, maintainability, and performance for the AI Math Calculator application.

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors