--- title: Sense Backend emoji: ๐ก๏ธ colorFrom: blue colorTo: indigo sdk: docker pinned: false --- # Backend Module Structure ## ๐ Directory Organization ``` backend/ โโโ api.py # FastAPI routes and endpoints โโโ app.py # Application entry point โโโ backend.py # RegexClassifier and PII detection โโโ email_service.py # Email sending service (NEW โจ) โโโ email_service_examples.py # Usage examples (NEW ๐) โโโ requirements.txt # Python dependencies โโโ packages.txt # Additional packages ``` --- ## ๐ง Core Modules ### api.py **Purpose**: FastAPI application with REST API endpoints **Key Features**: - File upload endpoints (CSV, JSON, PDF, images, etc.) - Database connectors (PostgreSQL, MySQL, MongoDB) - Cloud storage integrations (S3, Azure, GCS) - Enterprise connectors (Gmail, Slack, Confluence) - PII analysis and masking endpoints - **Email endpoints** (`/api/send-welcome`) **Imports**: ```python from backend import RegexClassifier # PII detection from email_service import send_welcome_email # Email sending ``` --- ### backend.py **Purpose**: Core PII detection and classification logic **Key Features**: - `RegexClassifier` - Pattern-based PII detection - Multiple PII types (SSN, credit cards, emails, etc.) - Database and file format support - Data masking and anonymization --- ### email_service.py โจ **NEW** **Purpose**: Standalone email service module **Key Features**: - `EmailService` class for sending emails via SMTP - Singleton pattern via `get_email_service()` - Welcome email template with HTML styling - Generic `send_email()` method for custom emails - Configuration management from environment variables **Usage**: ```python # Quick usage from email_service import send_welcome_email send_welcome_email("User Name", "user@example.com") # Advanced usage from email_service import get_email_service email_service = get_email_service() email_service.send_email(to_email, subject, html_body, text_body) ``` **Configuration** (Environment Variables): - `SMTP_EMAIL` - Sender email address - `SMTP_PASSWORD` - SMTP password/app password - `SMTP_HOST` - SMTP server (default: smtp.gmail.com) - `SMTP_PORT` - SMTP port (default: 587) --- ### email_service_examples.py ๐ **NEW** **Purpose**: Comprehensive usage examples **Contains 6 examples**: 1. Quick welcome email 2. Using service instance 3. Custom email 4. Configuration checking 5. FastAPI integration 6. Batch email sending **Run examples**: ```bash cd backend python email_service_examples.py ``` --- ### app.py **Purpose**: Application entry point for running the API **Usage**: ```bash python app.py # or uvicorn app:app --host 0.0.0.0 --port 7860 ``` --- ## ๐ Module Dependencies ``` app.py โโโ api.py โโโ backend.py (RegexClassifier) โโโ email_service.py (send_welcome_email) ``` **Key Benefits**: - โ Clean separation of concerns - โ Each module has a single responsibility - โ Easy to import and reuse - โ Testable independently - โ Follows Python best practices --- ## ๐ฆ Email Service API ### EmailService Class ```python class EmailService: def __init__(self) """Initialize with SMTP config from environment""" def is_configured(self) -> bool """Check if SMTP credentials are set""" def send_email(to_email, subject, html_body, text_body) -> bool """Send generic email with HTML and text versions""" def send_welcome_email(name, email) -> bool """Send pre-configured welcome email""" ``` ### Convenience Functions ```python def get_email_service() -> EmailService """Get singleton instance of EmailService""" def send_welcome_email(name, email) -> bool """Quick access to send welcome email""" ``` --- ## ๐ Quick Start ### Send Welcome Email ```python from email_service import send_welcome_email # That's it! success = send_welcome_email("John Doe", "john@example.com") ``` ### Send Custom Email ```python from email_service import get_email_service service = get_email_service() service.send_email( to_email="user@example.com", subject="Custom Subject", html_body="