A full-stack social media platform built with Go backend and React frontend, featuring user authentication, posts, comments, followers, and real-time interactions.
- User Management: Registration, authentication, and profile management
- Posts & Comments: Create, read, update, and delete posts with commenting system
- Social Features: Follow/unfollow users, personalized feed
- Authentication: JWT-based authentication with secure token management
- Rate Limiting: Built-in rate limiting for API protection
- Caching: Redis integration for improved performance
- Email Integration: SendGrid integration for notifications
- API Documentation: Swagger/OpenAPI documentation
- Database Migrations: Structured database schema management
- Testing: Comprehensive test suite
- Language: Go 1.23.4
- Framework: Chi Router
- Database: PostgreSQL
- Cache: Redis
- Authentication: JWT tokens
- Email: SendGrid
- Documentation: Swagger/Swaggo
- Logging: Zap logger
- Framework: React 19.0.0
- Build Tool: Vite
- Routing: React Router DOM
- Language: JavaScript/TypeScript
- Containerization: Docker & Docker Compose
- Hot Reload: Air (Go) & Vite (React)
- CI/CD: GitHub Actions
- Database Migrations: golang-migrate
- Testing: Go testing framework
Social/
βββ cmd/ # Application entry points
β βββ api/ # REST API server
β β βββ main.go # Main application entry
β β βββ api.go # API configuration and routes
β β βββ auth.go # Authentication handlers
β β βββ users.go # User management endpoints
β β βββ posts.go # Post management endpoints
β β βββ feed.go # Feed generation endpoints
β β βββ middleware.go # HTTP middleware
β β βββ errors.go # Error handling
β β βββ json.go # JSON utilities
β β βββ health.go # Health check endpoint
β β βββ *_test.go # API tests
β βββ migrate/ # Database migration tools
β βββ migrations/ # SQL migration files
β βββ seed/ # Database seeding
βββ internal/ # Private application code
β βββ auth/ # Authentication logic
β β βββ auth.go # Auth interfaces
β β βββ jwt.go # JWT implementation
β β βββ mocks.go # Auth mocks for testing
β βββ db/ # Database connection and utilities
β β βββ db.go # Database connection setup
β β βββ seed.go # Database seeding logic
β βββ env/ # Environment configuration
β β βββ env.go # Environment variable helpers
β βββ mailer/ # Email service
β β βββ mailer.go # Mailer interface
β β βββ sendgrid.go # SendGrid implementation
β β βββ templates/ # Email templates
β βββ ratelimiter/ # Rate limiting
β β βββ ratelimiter.go # Rate limiter interface
β β βββ fixed-window.go # Fixed window implementation
β βββ store/ # Data access layer
β βββ storage.go # Storage interfaces
β βββ users.go # User data operations
β βββ posts.go # Post data operations
β βββ comments.go # Comment data operations
β βββ followers.go # Follower relationships
β βββ role.go # User roles
β βββ pagination.go # Pagination utilities
β βββ mocks.go # Store mocks for testing
β βββ cache/ # Caching layer
βββ web/ # Frontend React application
β βββ src/ # React source code
β β βββ App.jsx # Main App component
β β βββ main.jsx # React entry point
β β βββ ConfirmationPage.tsx # Email confirmation page
β β βββ App.css # App styles
β β βββ index.css # Global styles
β β βββ assets/ # Static assets
β βββ public/ # Public assets
β βββ package.json # Node.js dependencies
β βββ vite.config.js # Vite configuration
β βββ eslint.config.js # ESLint configuration
β βββ index.html # HTML template
βββ docs/ # API documentation
β βββ docs.go # Generated Swagger docs
β βββ swagger.json # Swagger JSON spec
β βββ swagger.yaml # Swagger YAML spec
βββ scripts/ # Utility scripts
β βββ db_init.sql # Database initialization
β βββ test_concurrency.go # Concurrency testing
βββ bin/ # Compiled binaries
βββ .github/ # GitHub workflows
β βββ workflows/ # CI/CD pipelines
βββ go.mod # Go module definition
βββ go.sum # Go module checksums
βββ makefile # Build automation
βββ docker-compose.yml # Docker services
βββ .air.toml # Air hot reload config
βββ .envrc # Environment variables
βββ .gitignore # Git ignore rules
βββ README.md # This file
- Go: 1.23.4 or later
- Node.js: 18.0 or later
- PostgreSQL: 12.0 or later
- Redis: 6.0 or later (optional, for caching)
- Docker & Docker Compose: For containerized setup
git clone https://github.com/karthikbhandary2/Social.git
cd SocialCreate a .envrc file in the root directory:
# Database Configuration
export DB_ADDR="postgres://username:password@localhost/social?sslmode=disable"
export DB_MAX_OPEN_CONNS=30
export DB_MAX_IDLE_CONNS=30
export DB_MAX_IDLE_TIME="15m"
# Server Configuration
export ADDR=":8082"
export EXTERNAL_URL="localhost:8082"
export FRONTEND_URL="http://localhost:5173"
export ENV="development"
# Redis Configuration (optional)
export REDIS_ADDR="localhost:6379"
export REDIS_PW=""
export REDIS_DB=0
export REDIS_ENABLED=true
# Authentication
export AUTH_BASIC_USER="admin"
export AUTH_BASIC_PASS="admin"
export AUTH_TOKEN_SECRET="your-secret-key"
# Email Configuration (SendGrid)
export FROM_EMAIL="[email protected]"
export SENDGRID_API_KEY="your-sendgrid-api-key"
# Rate Limiting
export RATELIMITER_REQUESTS_COUNT=20
export RATE_LIMITER_ENABLED=true# Start PostgreSQL with Docker Compose
docker-compose up -d db
# Wait for database to be ready, then run migrations
make migrate-up
# Seed the database with sample data
make seed# Create database
createdb social
# Run migrations
make migrate-up
# Seed database
make seedgo mod tidycd web
npm install
cd ..Terminal 1 - Backend:
# Install Air for hot reload (if not already installed)
go install github.com/cosmtrek/air@latest
# Start backend with hot reload
airTerminal 2 - Frontend:
cd web
npm run devBackend:
go build -o bin/main cmd/api/*.go
./bin/mainFrontend:
cd web
npm run build
npm run preview- Frontend: http://localhost:5173
- Backend API: http://localhost:8082
- API Documentation: http://localhost:8082/swagger/index.html
- Health Check: http://localhost:8082/v1/health
# Database Operations
make migrate-up # Run all pending migrations
make migrate-down # Rollback last migration
make migrate-force ARG=1 # Force migration to specific version
make seed # Seed database with sample data
# Documentation
make gen-docs # Generate Swagger documentation
# Testing
make test # Run all tests
# Create new migration
make migration create_users_tablePOST /v1/authentication/user- User loginPOST /v1/users- User registrationPUT /v1/users/activate/:token- Account activation
GET /v1/users/:id- Get user profilePUT /v1/users/:id/follow- Follow userPUT /v1/users/:id/unfollow- Unfollow user
GET /v1/posts- List posts with paginationPOST /v1/posts- Create new postGET /v1/posts/:id- Get specific postPATCH /v1/posts/:id- Update postDELETE /v1/posts/:id- Delete post
GET /v1/posts/:id/comments- Get post commentsPOST /v1/posts/:id/comments- Add comment
GET /v1/users/:id/feed- Get user's personalized feed
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific test
go test ./cmd/api -v
# Run tests with race detection
go test -race ./...Create a new migration:
make migration create_new_tableThis creates two files in cmd/migrate/migrations/:
000X_create_new_table.up.sql- Forward migration000X_create_new_table.down.sql- Rollback migration
# Build and start all services
docker-compose up --build
# Start in background
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down# Build backend image
docker build -t social-api .
# Build frontend image
cd web && docker build -t social-web .The project includes comprehensive tests:
- Unit Tests: Individual component testing
- Integration Tests: API endpoint testing
- Concurrency Tests: Race condition testing
cmd/api/
βββ api_test.go # API integration tests
βββ user_test.go # User endpoint tests
βββ test_utils.go # Testing utilities
The application exposes metrics at /debug/vars:
- Version: Application version
- Database: Connection pool stats
- Goroutines: Active goroutine count
- Memory: Runtime memory statistics
- JWT Authentication: Secure token-based authentication
- Rate Limiting: Configurable request rate limiting
- CORS: Cross-origin resource sharing configuration
- Input Validation: Request payload validation
- SQL Injection Protection: Parameterized queries
- Password Hashing: Bcrypt password hashing
export ENV="production"
export DB_ADDR="postgres://user:pass@prod-db:5432/social?sslmode=require"
export REDIS_ADDR="prod-redis:6379"
export EXTERNAL_URL="https://your-domain.com"
export FRONTEND_URL="https://your-frontend-domain.com"The application provides health check endpoints:
GET /v1/health- Basic health statusGET /debug/vars- Detailed metrics
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
-
Database Connection Failed
# Check if PostgreSQL is running pg_isready -h localhost -p 5432 # Verify connection string in .envrc
-
Redis Connection Failed
# Check if Redis is running redis-cli ping # Disable Redis if not needed export REDIS_ENABLED=false
-
Migration Errors
# Check migration status migrate -path ./cmd/migrate/migrations/ -database $DB_ADDR version # Force to specific version if needed make migrate-force ARG=1
-
Port Already in Use
# Change port in .envrc export ADDR=":8083"
For support and questions:
- Create an issue on GitHub
- Check existing documentation
- Review the API documentation at
/swagger/index.html
Happy Coding! π