Skip to content

Back-Buddy/Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

BackBuddy Backend

.NET C# Docker MongoDB Redis Firebase Azure Azure Service Bus Azure Container Apps

πŸ“‹ About the Project

BackBuddy Backend is the core of an intelligent posture and sitting-time monitoring platform. The system was developed to help users develop healthier sitting habits and improve their posture.

🎯 Core Features

  • πŸ” Real-time Sensor Data Processing: Continuous monitoring and analysis of ESP32 sensor data
  • πŸ‘€ User Management: Complete authentication and authorization via Firebase
  • πŸ“± Push Notifications: Smart reminders based on sitting behavior
  • πŸ“Š Data Analytics: Detailed statistics and reports on sitting habits
  • πŸ”— WebSocket Communication: Bidirectional real-time communication with clients
  • πŸ—οΈ Microservice Architecture: Modular design for maximum scalability

πŸ—οΈ Architecture & Tech Stack

Microservices Overview

graph TB
    subgraph "Client Layer"
        APP[Mobile App]
        WEB[Web Dashboard]
        ESP[ESP32 Devices]
    end

    subgraph "API Gateway"
        API[API Service]
        WS[WebSocket Handler]
    end

    subgraph "Core Services"
        DS[Device Service]
        US[User Service]
        NS[Notification Service]
        DO[Device Orchestrator]
    end

    subgraph "Infrastructure"
        MONGO[(MongoDB)]
        REDIS[(Redis Cache)]
        MSG_DEV[RabbitMQ - Dev]
        MSG_PROD[Azure Service Bus - Prod]
        FIREBASE[Firebase]
        KEYVAULT_PROD[Azure KeyVault - Prod]
        ACR[Azure Container Registry]
        ACA[Azure Container Apps]
    end

    APP --> API
    WEB --> API
    ESP --> API
    API --> WS
    API --> DS
    API --> US
    API --> NS
    DO --> DS
    DS --> MONGO
    US --> FIREBASE
    NS --> MSG_DEV
    NS --> MSG_PROD
    DS --> REDIS
    US --> KEYVAULT_PROD

    %% Deployment
    ACR --> ACA
    API -.-> ACA
    DS -.-> ACA
    US -.-> ACA
    NS -.-> ACA
    DO -.-> ACA
Loading

πŸ“š Tech Stack Details

Backend Framework

  • .NET 9.0 - Latest LTS version with improved performance
  • ASP.NET Core - Web API framework with integrated DI
  • C# 13 - Latest language features and performance optimizations

Databases & Caching

  • MongoDB 7.x - NoSQL database for flexible document storage
  • Redis - In-memory cache for session management and real-time data
  • Azure KeyVault - Production: Secure management of secrets and certificates

Message Broker & Communication

  • RabbitMQ - Development: Local message broker for async communication
  • Azure Service Bus - Production: Enterprise messaging service for reliable communication
  • MassTransit 8.x - .NET Service Bus Framework for Enterprise Messaging
  • WebSockets - Bidirectional real-time communication

Authentication & Authorization

  • Firebase Authentication - Secure user authentication
  • JWT Bearer Tokens - Stateless authentication
  • Firebase Admin SDK - Server-side Firebase integration

DevOps & Infrastructure

  • Azure Container Apps - Serverless container hosting platform
  • Azure Container Registry - Private Docker registry
  • Docker & Docker Compose - Containerization of all services
  • GitHub Actions - CI/CD Pipeline
  • Multi-stage Dockerfile - Optimized container builds

Testing & Quality Assurance

  • MSTest Framework - Unit Testing
  • NSubstitute - Mocking Framework
  • Integration Tests - End-to-End Testing

Monitoring & Observability

  • Swagger/OpenAPI - API Documentation
  • Scalar UI - Modern API Explorer Alternative (for Emulator)
  • Structured Logging - Comprehensive Application Logging
  • Health Checks - Service health monitoring with readiness and liveness probes
  • Azure Application Insights - Production monitoring, telemetry, and performance analytics
  • OpenTelemetry - Distributed tracing and metrics collection

🏒 Service Details

API Service (Port: 8080)

  • Purpose: Main entry point for all client requests
  • Technologies: ASP.NET Core, JWT Authentication, Swagger
  • Functions: REST API, WebSocket Gateway, Request Validation
  • Monitoring: Health check endpoints (/api/health/isalive, /api/health/isready)

Device Service

  • Purpose: Management of all devices and sensor data
  • Technologies: MongoDB, Redis, MassTransit
  • Functions: Device Registration, Data Processing, Threshold Management

User Service

  • Purpose: User management and profile operations
  • Technologies:
    • Development: Firebase Admin SDK, local configuration
    • Production: Firebase Admin SDK, Azure KeyVault
  • Functions: User Authentication, Profile Management, Relationships

Notification Service

  • Purpose: Push notifications and alerting
  • Technologies:
    • Development: Firebase Cloud Messaging, RabbitMQ, Notification Emulator
    • Production: Firebase Cloud Messaging, Azure Service Bus
  • Functions: Smart Notifications, Reminder System

Device Orchestrator

  • Purpose: Coordination between devices and services
  • Technologies: Background Services, MassTransit
  • Functions: Data Orchestration, Event Processing

πŸš€ Quick Start

Prerequisites

Installation & Setup

  1. Clone repository:

    git clone https://github.com/Back-Buddy/Backend.git
    cd Backend/src
  2. Configure environment variables:

    cp Development.env.example Development.env
    # Edit Development.env with your values
  3. Start services:

    docker-compose up --build -d
  4. Verify services:

    # Check service status
    docker-compose ps
    
    # Verify API health
    curl http://localhost:8080/api/health/isalive
    
    # Access Swagger UI
    start http://localhost:8080/swagger
    
    # View logs
    docker-compose logs -f

🌐 Service Endpoints

Service URL Description
API Gateway http://localhost:8080 Main API Endpoint
Swagger UI http://localhost:8080/swagger API Documentation
Scalar API Explorer http://localhost:8080/scalar/v1 Modern API UI (for Emulator)
Health Checks http://localhost:8080/api/health/isalive Service Health Status
Health Checks http://localhost:8080/api/health/isready Service Readiness Status
MongoDB Express http://localhost:8081 Database Admin UI
Redis Insight http://localhost:5540 Redis Management
RabbitMQ Management http://localhost:15672 Message Broker UI (Dev)
Firebase Emulator http://localhost:4000 Firebase Local Development
Notification Emulator http://localhost:8083 Push Notification Testing (Dev)

πŸ” Default Credentials

  • RabbitMQ: guest / guest (Development only)
  • Redis: Password: my-password
  • MongoDB: No authentication (Development)

πŸ—„οΈ Data Model & Relationships

Entity Relationship Diagram

erDiagram
    USER {
        string userId_Firebase_PK
        string username
        string email
        string avatar_url
        datetime created_at
    }

    DEVICE {
        Guid id_PK
        string name
        string userId_FK
        TimeSpan threshold
        datetime secretGeneratedAt
        boolean active
    }

    DEVICE_LOGS {
        Guid id_PK
        Guid deviceId_FK
        datetime startTime
        datetime endTime
        enum logType "Sit|Error"
    }

    DEVICE_STATUS_REDIS {
        datetime startTime
    }

    REPORT {
        Guid id_PK
        string name
        enum visibilityType "All|Followers|Private"
        string userId_FK
        Guid deviceId_FK
        datetime startTime
        datetime endTime
        report_metadata metadata
        List_Guid usedLogs
        datetime createdAt
    }

    REPORT_METADATA {
        TimeSpan totalTime
        TimeSpan sitTime
        TimeSpan standTime
        double sitPercentage
        double standPercentage
        int postureChanges
        TimeSpan averageSitPeriod
        TimeSpan shortestSitPeriod
        TimeSpan longestSitPeriod
    }

    REPORT_LIKE {
        Guid id_PK
        Guid reportId_FK
        string userId_FK
    }

    USER_FOLLOW {
        Guid id_PK
        string userId_FK
        string targetId_FK
        datetime createdAt
    }

    %% Relationships
    USER ||--o{ DEVICE : "owns"
    DEVICE ||--o{ DEVICE_LOGS : "generates"
    DEVICE ||--o| DEVICE_STATUS_REDIS : "has_current_state"
    DEVICE ||--o{ REPORT : "generates_reports"
    USER ||--o{ REPORT : "creates_reports"
    USER ||--o{ USER_FOLLOW : "follows_users"
    USER ||--o{ REPORT_LIKE : "likes_reports"
    REPORT ||--o{ REPORT_LIKE : "receives_likes"
    REPORT ||--|| REPORT_METADATA : "contains"
Loading

πŸ“Š Database Collections

Users Collection (Firebase)

  • Primary Key: Firebase UID (string)
  • Purpose: User profile and authentication
  • Storage: Firebase Firestore

Devices Collection (MongoDB)

  • Primary Key: GUID
  • Purpose: Device registration and configuration
  • Indexes: userId

DeviceLogs Collection (MongoDB)

  • Primary Key: GUID
  • Purpose: Historical session data (sitting/standing periods)
  • Indexes: deviceId

Reports Collection (MongoDB)

  • Primary Key: GUID
  • Purpose: User-generated reports with analytics metadata
  • Indexes: reportId

UserFollow Collection (MongoDB)

  • Primary Key: GUID
  • Purpose: User relationship management (following system)
  • Indexes: id, userId, targetId

ReportLike Collection (MongoDB)

  • Primary Key: GUID
  • Purpose: User likes on reports
  • Indexes: reportId, userId

Redis Cache Structure

DeviceStatus:{deviceId} -> Current Device Status JSON
device_status_keys -> List of all DeviceIds where the status exists
DeviceConnected:{deviceId}:presence -> Device is Connected
DeviceConnected:{deviceId}:meta -> Device Connection Meta Data (e.g ConnectedAt)

πŸ“Š Monitoring & Observability

The BackBuddy Backend implements enterprise-grade monitoring and observability features for production-ready operation.

Health Checks

Standard Health Check Endpoints

  • Liveness Probe: /api/health/isalive - Basic service availability
  • Readiness Probe: /api/health/isready - Service readiness for traffic
  • JSON Response Format: Detailed health status information
  • Kubernetes Compatible: Ready for container orchestration

Health Check Features

GET /api/health/isalive
Content-Type: application/json

{
  "status": "Healthy",
  "totalDuration": "00:00:00.0123456",
  "entries": {
    "self": {
      "status": "Healthy",
      "duration": "00:00:00.0045678"
    }
  }
}

Application Insights & Telemetry

Azure Application Insights Integration

  • Custom Metrics: Business-specific performance indicators
  • Exception Tracking: Automatic error monitoring and alerting
  • Performance Monitoring: Response times, throughput, and resource usage
  • Dependency Tracking: Database, Redis, and external service calls

πŸ§ͺ Testing & Quality Assurance

The BackBuddy Backend features a comprehensive test suite with 212 Integration Tests and 30 Unit Tests, ensuring robust quality assurance.

Test Overview

Test Type Count Coverage
Integration Tests 212 End-to-End API Testing
Unit Tests 30 Core Business Logic
Total 242 Comprehensive Coverage

Integration Test Scaling

The integration tests are executed under realistic conditions where the API is automatically scaled to 2 containers. This enables:

  • Realistic Test Conditions: Simulation of production-like environments
  • Container-to-Container Communication: Validation of microservice communication
  • Load Balancing Testing: Verification of load distribution between containers

Running Tests

Unit Tests

# All Unit Tests (30 Tests)
dotnet test --collect:"XPlat Code Coverage"

# Specific Test Suite
dotnet test src/Core-Test/Core-Test.csproj -v detailed

# Device Service Tests
dotnet test src/DeviceService-Test/DeviceService-Test.csproj -v detailed

# With Coverage Report
dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResults

Integration Tests

# Start infrastructure with API scaling (213 Integration Tests)
docker-compose -f docker-compose.test.yml up -d

# Run integration tests with 2 API containers
dotnet test src/Integration-Test/Integration-Test.csproj -v detailed

# Test cleanup
docker-compose -f docker-compose.test.yml down -v

All Tests

# Run complete test suite (243 Tests total)
dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResults

# Run with detailed output and coverage
dotnet test --logger:"console;verbosity=detailed" --collect:"XPlat Code Coverage"

πŸš€ Deployment & DevOps

CI/CD Pipeline (GitHub Actions)

graph LR
    A[Push to main] --> B[Build & Test]
    B --> C[Build Docker Images]
    C --> D[Integration Tests]
    D --> E[Push to ACR]
    E --> F[Deploy to Container Apps]
Loading

Deployment Environments

Development

# Local development setup
docker-compose -f docker-compose.yml -f docker-compose.override.yml up -d

Production (Azure)

  • Container Hosting: Azure Container Apps
  • Container Registry: Azure Container Registry
  • Key Management: Azure KeyVault for secure secret storage
  • Scaling: Automatic scaling based on CPU/Memory/HTTP requests
  • Monitoring: Azure Application Insights
  • Logging: Azure Log Analytics

πŸ”§ Configuration

Environment Variables

# Database Configuration
MongoDB__Connection=mongodb://mongodb:27017/
MongoDB__DatabaseName=BackBuddy
Redis__Connection=redis
Redis__DatabaseName=Device

# Message Broker Configuration
# Development (RabbitMQ)
MESSAGE_QUEUE_CONNECTION=rabbitmq
# Production (Azure Service Bus)
MESSAGE_QUEUE_CONNECTION=<Connection String>

# Firebase Configuration
Firebase__ProjectId=change-me
Firebase__Secret=<Secret-File as Base64 (only Prod)>
Firebase__FireStoreEmulatorHost=firebase:8080 (only Dev)
Firebase__FireAuthEmulatorHost=firebase:9099 (only Dev)
Firebase__NotificationEmulatorHost=http://notificationemulator:8080/send (only Dev)

# Azure Services (Production Only)
KEY_VAULT_URI=https://kv-backbuddy.vault.azure.net/
AZURE_CLIENT_ID=
AZURE_CLIENT_SECRET=
AZURE_TENANT_ID=

# Application Settings
ASPNETCORE_ENVIRONMENT=Development
ASPNETCORE_HTTP_PORTS=8080
Auth__Authority=
Auth__ValidIssuer=
Auth__ValidAudience=

# Config
ConnectedDeviceConfig__PRESENCETIMEOUT=00:00:30
ConnectedDeviceConfig__METATIMEOUT=00:01:00:00
ConnectedDeviceConfig__HEARTBEATINTERVAL=00:00:05

# Development: Secrets stored directly in environment variables or appsettings
# Production: Secrets retrieved from Azure KeyVault

πŸ“± API Versioning & Documentation

API Versioning Strategy

  • URL Path Versioning: /api/v1/, /api/v2/
  • Backward Compatibility: Minimum 2 versions simultaneously
  • Deprecation Policy: 6 months notice before breaking changes

OpenAPI Specification

  • Swagger UI: Interactive API documentation
  • Scalar: Modern alternative to Swagger UI
  • Code Generation: Client SDKs for various languages
  • API Testing: Integrated testing tools

πŸ”’ Security & Compliance

Security Measures

Authentication & Authorization

  • JWT Token Rotation: Short lifespan with refresh tokens
  • Roating Secret: Short lifespan for sensor secret

Data Protection

  • Encryption in Transit: TLS 1.3 for all connections
  • Key Management:
    • Development: Local configuration and environment variables
    • Production: Azure KeyVault integration

Security Scanning

# Dependency Vulnerability Scan
dotnet list package --vulnerable --include-transitive

# Container Security Scan
docker scout cves backbuddy/api:latest

# Static Code Analysis
dotnet format --severity error --verify-no-changes

🀝 Development & Contribution

Development Setup

Local Development Environment

# Clone repository
git clone https://github.com/Back-Buddy/Backend.git
cd Backend/src

# Install dependencies
dotnet restore

# Start development services
docker-compose up -d mongodb redis rabbitmq

# Start API locally
dotnet run --project Api/Api.csproj

Code Guidelines

  • Code Style: EditorConfig + .NET Coding Conventions
  • Commit Messages: Conventional Commits Format
  • Branch Strategy: GitFlow (main, develop, feature_x)
  • Code Reviews: Minimum 2 approvals for production (Static Code Review Sonar Cloud)

Debugging

# Start with debugger
dotnet run --project Api/Api.csproj --configuration Debug

# View logs
docker-compose logs -f api

πŸ” Monitoring & Observability

Application Metrics

  • Azure Application Insights: Application performance monitoring
  • Custom Metrics: Business-specific KPIs
  • Performance Counters: Response times, throughput
  • Error Tracking: Exception rates, error types
  • Resource Usage: CPU, memory, disk I/O

Logging Strategy

// Structured Logging Example
logger.LogInformation("Device data processed: {DeviceId} - {DataPoints} points in {Duration}ms",
    deviceId, dataPoints, processingTime);

πŸ› Troubleshooting

Common Issues

Services won't start

# Check Docker status
docker-compose ps

# Analyze logs
docker-compose logs [service-name]

# Restart containers
docker-compose restart [service-name]

# Complete restart
docker-compose down -v && docker-compose up -d

Database connection failed

# Test MongoDB connection
docker exec -it backbuddy-mongodb-1 mongosh

# Test Redis connection
docker exec -it backbuddy-redis-1 redis-cli ping

Performance issues

# Check resource usage
docker stats

# Database performance
# Enable MongoDB Slow Query Log
# Analyze Redis Memory Usage

πŸ† Acknowledgments & Credits

Open Source Libraries

  • ASP.NET Core Team - Microsoft
  • MongoDB .NET Driver - MongoDB Inc.
  • MassTransit - Chris Patterson & Contributors
  • FirebaseAdmin - Google
  • Docker - Docker Inc.
  • RabbitMQ - VMware (Development)
  • Azure Service Bus - Microsoft (Production)
  • Azure KeyVault - Microsoft (Production)

Contributors

  • PlaySkyHD (Nic Markfort) - Backend, Architecture & App Development
  • JPoepsel02 (Jannik PΓΆpsel) - Feature Development (Backend & Sensor)
  • Ben-Bongo (Ben Steverding) - App Development
  • LeonSCG (Leon Schmedding) - Sensor Development / App Development

BackBuddy Backend

License: MIT

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •