Skip to content

Latest commit

 

History

History
239 lines (177 loc) · 6.49 KB

File metadata and controls

239 lines (177 loc) · 6.49 KB

Streaming Backend in Go

Go Version Build Status Go Report Card License

A high-performance, secure TMDB API proxy backend written in Go. This project replicates the functionality of a Node.js streaming backend, providing a robust foundation for TV streaming applications.

About

This backend acts as a secure proxy to The Movie Database (TMDB) API, hiding API keys from the frontend while providing intelligent caching, monitoring, and production-grade security features.

Features

  • 🔐 Secure API Proxy: Hides TMDB API keys from frontend requests
  • Intelligent Caching: In-memory cache with configurable TTL (default 1 hour)
  • 🛡️ Production Security: CORS, security headers, rate limiting, gzip compression
  • 📊 Monitoring: Health checks and performance metrics endpoints
  • 🌐 Static File Serving: Serves frontend assets with SPA fallback
  • 🚀 Docker Ready: Multi-stage Docker build for efficient deployment
  • 🧪 Well Tested: Unit tests for core functionality
  • 📈 Performance Optimized: Efficient request routing and memory management

Installation

Prerequisites

  • Go 1.22 or later
  • TMDB API key (get one at TMDB)

Quick Start

  1. Clone the repository

    git clone https://github.com/code-xon/streaming-backend.git
    cd streaming-backend
  2. Install dependencies

    go mod tidy
  3. Set up environment variables

    cp .env.example .env
    # Edit .env with your TMDB_KEY
  4. Run the server

    go run cmd/server/main.go

The server will start on http://localhost:3000

Usage

Environment Variables

Variable Default Description
TMDB_KEY TMDB API key (required)
PORT 3000 Server port
NODE_ENV development Environment (development/production)
CACHE_TTL 3600 Cache TTL in seconds
CACHE_CHECK_PERIOD 120 Cache cleanup interval in seconds
RATE_LIMIT_WINDOW_MS 900000 Rate limit window in milliseconds
RATE_LIMIT_MAX_REQUESTS 100 Max requests per window
FRONTEND_URL "" CORS allowed origin (leave empty for all)

API Endpoints

Core Endpoints

  • GET /health - System health check
  • GET /metrics - Performance metrics
  • GET /api/* - TMDB API proxy

TMDB Proxy Examples

# Get trending movies
curl "http://localhost:3000/api/trending/movie/week"

# Search for movies
curl "http://localhost:3000/api/search/movie?query=batman"

# Get movie details
curl "http://localhost:3000/api/movie/550"

# Get popular TV shows
curl "http://localhost:3000/api/tv/popular"

Health Check Response

{
  "status": "healthy",
  "timestamp": "2024-01-01T12:00:00Z",
  "environment": "production",
  "version": "1.0.0",
  "uptime": 3600,
  "memory": {
    "used": "45.2 MB",
    "total": "128.0 MB"
  },
  "cache": {
    "stats": {
      "hits": 150,
      "misses": 20
    },
    "keys": 25
  },
  "api": {
    "tmdb_configured": true,
    "base_url": "https://api.themoviedb.org/3"
  },
  "server": {
    "port": 3000,
    "go_version": "go1.22.0"
  }
}

Testing

Unit Tests

go test ./...

Load Testing

Use Apache Bench for load testing:

# Test health endpoint
ab -n 1000 -c 10 http://localhost:3000/health

# Test TMDB proxy (replace with actual endpoint)
ab -n 100 -c 5 "http://localhost:3000/api/trending/movie/week"

Integration Testing

# Test with curl
curl -i http://localhost:3000/health
curl -i "http://localhost:3000/api/search/movie?query=test"

Deployment

Docker

# Build image
docker build -t streaming-backend .

# Run container
docker run -p 3000:3000 \
  -e TMDB_KEY=your_api_key \
  -e NODE_ENV=production \
  streaming-backend

Render.com

  1. Connect your GitHub repository to Render
  2. Create a new Web Service
  3. Set the following:
    • Build Command: go build ./cmd/server
    • Start Command: ./server
    • Health Check Path: /health
  4. Add environment variables in Render dashboard
  5. Deploy!

Other Platforms

The application can be deployed to any platform supporting Go applications:

  • Heroku: Use Go buildpack
  • Railway: Connect repo and set commands
  • Fly.io: Use provided Dockerfile
  • AWS/GCP/Azure: Container or binary deployment

Architecture

streaming-backend/
├── cmd/server/          # Application entry point
├── internal/
│   ├── cache/           # In-memory caching logic
│   ├── handlers/        # HTTP request handlers
│   ├── metrics/         # Metrics collection
│   └── middleware/      # HTTP middleware
├── public/              # Static frontend files
├── Dockerfile           # Multi-stage Docker build
├── go.mod               # Go module definition
└── README.md

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Quick Start for Contributors

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Make your changes and add tests
  4. Run tests: go test ./...
  5. Submit a pull request

Security

Please see our Security Policy for reporting security vulnerabilities.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits

Lead Developer: Ramkrishna - Project architect and core implementation

Acknowledgments


Made with ❤️ in Go by code-xon