Skip to content

thomassems/NeuralBets-backend

Repository files navigation

NeuralBets Backend

A scalable, microservices-based backend platform for sports betting analytics and odds management, built with modern Python technologies and containerized deployment.

Python Flask Docker MongoDB Gunicorn

πŸ“‹ Table of Contents

🎯 Overview

NeuralBets Backend is a production-ready microservices architecture designed to handle sports betting data, odds aggregation, and user management. The system integrates with external sports betting APIs to fetch real-time odds, stores and processes betting data, and provides RESTful APIs for frontend consumption.

Key Highlights

  • Microservices Architecture: Modular design with independent, scalable services
  • Containerized Deployment: Docker and Docker Compose for easy deployment and scaling
  • Data Modeling: Type-safe schemas with validation and transformation utilities
  • Repository Pattern: Clean separation of data access logic
  • Production-Ready: Gunicorn WSGI server, health checks, error handling

πŸ—οΈ Architecture

The backend follows a microservices architecture with the following services:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    NeuralBets Backend                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ User Service β”‚  β”‚ Bet Service  β”‚  β”‚ Auth Service β”‚ β”‚
β”‚  β”‚   (Port 8081)β”‚  β”‚  (Port 8082) β”‚  β”‚  (Port TBD)  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚         β”‚                  β”‚                  β”‚         β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β”‚                            β”‚                            β”‚
β”‚                    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”                   β”‚
β”‚                    β”‚  Shared Utils  β”‚                   β”‚
β”‚                    β”‚  (Constants,   β”‚                   β”‚
β”‚                    β”‚   Validators)  β”‚                   β”‚
β”‚                    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜                   β”‚
β”‚                            β”‚                            β”‚
β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
β”‚         β”‚                  β”‚                  β”‚         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  MongoDB    β”‚  β”‚ External API  β”‚  β”‚   Docker     β”‚  β”‚
β”‚  β”‚  Database   β”‚  β”‚  Integration  β”‚  β”‚  Containers  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Service Responsibilities

  • User Service: User management and authentication
  • Bet Service: Odds aggregation, betting data management, external API integration
  • Auth Service: Authentication and authorization (planned)
  • Account Service: User account management (planned)
  • Wallet Service: Transaction and balance management (planned)
  • Prediction Service: ML-based predictions (planned)

πŸ› οΈ Technologies

Core Stack

  • Python 3.11 - Modern Python with latest features
  • Flask 3.0.3 - Lightweight web framework for REST APIs
  • Gunicorn 22.0.0 - Production WSGI HTTP Server
  • MongoDB - NoSQL database for flexible data storage
  • PyMongo - MongoDB driver for Python

DevOps & Infrastructure

  • Docker - Containerization for consistent deployments
  • Docker Compose - Multi-container orchestration
  • Python-dotenv - Environment variable management

Architecture Patterns

  • Microservices - Service-oriented architecture
  • Repository Pattern - Data access abstraction
  • Schema/Model Pattern - Type-safe data structures
  • Blueprint Pattern - Modular route organization

External Integrations

  • The Odds API - Real-time sports betting odds aggregation

✨ Features

Bet Service

  • βœ… Real-time odds fetching from multiple bookmakers
  • βœ… Sports data aggregation and caching
  • βœ… Event data management
  • βœ… Data transformation and normalization
  • βœ… Schema-based validation
  • βœ… MongoDB persistence with optimized queries
  • βœ… Health check endpoints

Data Management

  • βœ… Dual Storage Strategy: Full and simplified data formats
  • βœ… Type-Safe Schemas: Dataclass-based models with validation
  • βœ… Automatic Transformation: API data β†’ Simplified format
  • βœ… JSON Serialization: Handles ObjectId, datetime, nested structures
  • βœ… Data Validation: Pre-storage validation for data integrity

Development Features

  • βœ… Hot-reload support for development
  • βœ… Comprehensive error handling
  • βœ… Logging and debugging utilities
  • βœ… Environment-based configuration
  • βœ… Shared utilities package for code reuse

πŸ“ Project Structure

NeuralBets-backend/
β”œβ”€β”€ bet-service/              # Betting odds and data service
β”‚   β”œβ”€β”€ routes/              # API route definitions
β”‚   β”‚   └── api_routes.py
β”‚   β”œβ”€β”€ schemas.py           # Data models and transformations
β”‚   β”œβ”€β”€ respository.py       # Data access layer
β”‚   β”œβ”€β”€ external_api_client.py  # External API integration
β”‚   β”œβ”€β”€ config.py            # Service configuration
β”‚   β”œβ”€β”€ app.py               # Flask application entry point
β”‚   β”œβ”€β”€ Dockerfile           # Service containerization
β”‚   └── requirements.txt     # Python dependencies
β”‚
β”œβ”€β”€ user-service/            # User management service
β”‚   β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ app.py
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── requirements.txt
β”‚
β”œβ”€β”€ shared_utils/            # Shared code package
β”‚   β”œβ”€β”€ constants.py         # Shared constants and validators
β”‚   └── setup.py             # Package configuration
β”‚
β”œβ”€β”€ docker-compose.yml       # Multi-service orchestration
β”œβ”€β”€ .env                     # Environment variables (not in repo)
└── README.md               # This file

πŸš€ Getting Started

Prerequisites

  • Docker and Docker Compose installed
  • Python 3.11+ (for local development)
  • MongoDB instance (or use Docker)
  • API key for The Odds API

Installation

  1. Clone the repository

    git clone <repository-url>
    cd NeuralBets-backend
  2. Set up environment variables

    cp .env.example .env
    # Edit .env and add your API keys
  3. Build and start services

    docker compose up --build
  4. Verify services are running

    # Check service health
    curl http://localhost:8081/health  # User Service
    curl http://localhost:8082/health   # Bet Service

Development Setup

For local development without Docker:

# Install shared utilities
cd shared_utils
pip install -e .

# Install service dependencies
cd ../bet-service
pip install -r requirements.txt

# Run service
python app.py

πŸ“‘ API Endpoints

Bet Service (Port 8082)

Endpoint Method Description
/ GET Service health check
/health GET Health status
/bets/status GET API status
/bets/getdefaultodds GET Get cached/default odds
/bets/getodds GET Get odds for specific sport
/bets/getevents GET Get events for sport
/bets/getdefaultevents GET Get default events

User Service (Port 8081)

Endpoint Method Description
/ GET Service health check
/health GET Health status
/users/status GET API status

πŸ’» Development

Code Organization

  • Schemas (schemas.py): Define data structures, transformations, and validation
  • Repository (respository.py): Handle all database operations
  • Routes (routes/api_routes.py): Thin HTTP layer, delegates to repository
  • External Client (external_api_client.py): Third-party API integration

Adding a New Service

  1. Create service directory with app.py, Dockerfile, requirements.txt
  2. Add service to docker-compose.yml
  3. Use shared utilities from shared_utils/ package
  4. Follow existing patterns for consistency

Best Practices

  • βœ… Use schemas for all data structures
  • βœ… Validate data before storage
  • βœ… Transform data on storage (not retrieval)
  • βœ… Handle errors gracefully
  • βœ… Use type hints where possible
  • βœ… Document functions and classes

πŸ”§ Configuration

Services are configured via environment variables:

  • ODDS_API_KEY: API key for The Odds API
  • MONGODB_URI: MongoDB connection string
  • FLASK_ENV: Environment (development/production

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors