Skip to content

Latest commit

 

History

History
247 lines (189 loc) · 6.99 KB

File metadata and controls

247 lines (189 loc) · 6.99 KB

Social Aggregator Backend

A production-ready Go backend for a social content aggregation platform built with Gin, Supabase, and JWT authentication.

🚀 Features

  • Fast REST API with Gin framework (~40× faster than Martini)
  • Supabase Integration for PostgreSQL database and authentication
  • JWT Middleware for secure route protection
  • Clean Architecture with organized project structure
  • Docker Ready with multi-stage builds
  • Production Optimized for horizontal scaling

📋 Tech Stack

Layer Technology Why
Web Framework Gin Fast and ergonomic routing
Database & Auth Supabase (PostgreSQL + GoTrue) Serverless Postgres + managed identity
Supabase SDKs postgrest-go, auth-go Strongly-typed Go clients
JWT golang-jwt/jwt/v5 Standardized token flow
Containerization Multi-stage Dockerfile Small final image (~15MB)

🏗️ Project Structure

backend/
├── cmd/
│   └── server/
│       └── main.go              # Application entry point
├── internal/
│   ├── api/
│   │   ├── handlers/
│   │   │   ├── auth.go          # Authentication handlers
│   │   │   ├── content.go       # Content management
│   │   │   └── users.go         # User profiles & following
│   │   ├── middleware/
│   │   │   └── jwt.go           # JWT authentication middleware
│   │   └── routes.go            # Route registration
│   ├── supabase/
│   │   ├── client.go            # Supabase client wrapper
│   │   └── models.go            # Database models
│   └── config/
│       └── config.go            # Configuration management
├── Dockerfile                   # Multi-stage container build
├── docker-compose.yml          # Local development setup
├── go.mod                      # Go module dependencies
└── README.md                   # This file

⚙️ Environment Variables

Create a .env file in the root directory:

PORT=8080
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_ANON_KEY=your_public_key
SUPABASE_SERVICE_KEY=your_service_role_key
JWT_SECRET=your_jwt_secret_change_me

🗄️ Database Schema

Run this SQL in your Supabase SQL editor:

-- Enable UUID extension
create extension if not exists "uuid-ossp";

-- User profiles table
create table profiles (
  id uuid primary key references auth.users(id),
  username text unique,
  bio text,
  created_at timestamp with time zone default now()
);

-- Content table
create table content (
  id uuid primary key default uuid_generate_v4(),
  platform text check (platform in ('YouTube','Twitter','Reddit','IG')),
  url text not null,
  title text,
  thumbnail text,
  created_at timestamp with time zone default now()
);

-- Saved content relationship
create table saved_content (
  id uuid primary key default uuid_generate_v4(),
  user_id uuid references profiles(id),
  content_id uuid references content(id),
  created_at timestamp with time zone default now(),
  unique(user_id, content_id)
);

-- User following relationship
create table follows (
  follower_id uuid references profiles(id),
  following_id uuid references profiles(id),
  created_at timestamp with time zone default now(),
  primary key (follower_id, following_id)
);

🚀 Quick Start

Local Development

  1. Clone and setup:

    git clone <repository>
    cd backend
  2. Install dependencies:

    go mod tidy
  3. Set up environment:

    cp .env.example .env
    # Edit .env with your Supabase credentials
  4. Run locally:

    go run cmd/server/main.go

Docker Development

# Build and run with Docker Compose
docker compose up --build

# The API will be available at http://localhost:8080

📚 API Endpoints

Method Path Auth Required Description
GET /health Health check
POST /auth/signup User registration
POST /auth/login User login (returns JWT)
GET /profile/:id Get public profile
GET /feed Get personalized feed
POST /content Save content link
POST /follow/:id Follow user
DELETE /follow/:id Unfollow user

Example Requests

Sign Up:

curl -X POST http://localhost:8080/auth/signup \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","password":"password123"}'

Login:

curl -X POST http://localhost:8080/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"user@example.com","password":"password123"}'

Add Content (requires auth):

curl -X POST http://localhost:8080/content \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{"platform":"YouTube","url":"https://youtube.com/watch?v=example","title":"Example Video"}'

🐳 Production Deployment

Build Production Image

docker build -t social-agg-backend .

AWS ECS/Fargate Deployment

  1. Push to ECR:

    aws ecr create-repository --repository-name social-agg-backend
    docker tag social-agg-backend:latest YOUR_ECR_URI
    docker push YOUR_ECR_URI
  2. Create ECS Task Definition with environment variables

  3. Deploy with ALB for load balancing

  4. Enable auto-scaling based on CPU/memory metrics

Environment Variables for Production

PORT=8080
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_ANON_KEY=your_public_key
SUPABASE_SERVICE_KEY=your_service_role_key
JWT_SECRET=your_strong_production_secret

🔧 Development

Running Tests

go test ./...

Building for Production

CGO_ENABLED=0 GOOS=linux go build -o server ./cmd/server

📈 Scaling & Performance

  • Horizontal Scaling: Deploy multiple instances behind a load balancer
  • Database: Supabase handles PostgreSQL scaling automatically
  • Caching: Add Redis for session/feed caching when needed
  • Monitoring: Integrate with CloudWatch, Grafana, or similar

🔮 Future Enhancements

  • Framework Swap: Consider Fiber for even better performance
  • Real-time: Add WebSocket support for live feeds
  • ML Ranking: Integrate ML-based feed ranking algorithms
  • API Wrappers: Use Supabase FDW for direct social platform integration

📄 License

MIT License - see LICENSE file for details