Skip to content

utkarshsingh77/backend-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors