A Reddit-like social media platform backend API built with Go and PostgreSQL, providing a comprehensive platform for community-driven discussions with posts, comments, voting systems, and private/public community features.
- User Authentication - JWT-based authentication with refresh token rotation
- Community Management - Complete CRUD operations for public and private communities (subs) with invitation system
- Community Administration - Owner-controlled sub updates, deletions, member management, and invitation oversight
- Content Creation - Posts with image support and threaded comments
- Voting System - Upvote/downvote functionality for posts and comments
- Password Reset - Secure token-based password recovery system
- Clean Architecture - Layered approach with clear separation of concerns
- RESTful API - Consistent HTTP endpoints with proper status codes
- Database Design - PostgreSQL with GORM ORM and strategic indexing
- Security First - bcrypt password hashing, JWT tokens, CORS, rate limiting
- Docker Support - Containerized deployment with multi-stage builds
- Language: Go 1.x
- Framework: Gin Web Framework
- Database: PostgreSQL 13+ with GORM ORM
- Authentication: JWT with refresh tokens
- Containerization: Docker
- Development: Hot reload, environment configuration
cortex-api/
βββ main.go # Application entry point
βββ create_database.sql # PostgreSQL schema DDL
βββ Dockerfile # Multi-stage container build
βββ .dockerignore
βββ .gitignore
βββ go.mod # Go module dependencies
βββ go.sum # Dependency lock file
β
βββ internal/
β βββ database/
β β βββ database.go # Database initialization
β β
β βββ models/ # Data structures and ORM models
β β βββ user.go # User entity
β β βββ post.go # Post entity
β β βββ comment.go # Comment entity
β β βββ sub.go # Community (sub) entity
β β βββ vote.go # Vote entity
β β βββ reset_token.go # Password reset tokens
β β
β βββ repositories/ # Data access layer
β β βββ auth_repository.go
β β βββ post_repository.go
β β βββ comment_repository.go
β β βββ sub_repository.go
β β βββ user_repository.go # Repository implementations
β β
β βββ services/ # Business logic layer
β β βββ auth_service.go
β β βββ post_service.go
β β βββ comments_service.go
β β βββ sub_service.go
β β βββ user_service.go # Service implementations
β β
β βββ handlers/ # HTTP request/response handlers
β β βββ auth_handlers.go
β β βββ post_handlers.go
β β βββ votes_handlers.go
β β βββ sub_handlers.go
β β βββ user-auth_handlers.go
β β βββ user-login.go # Handler implementations
β β
β βββ routes/ # API routing and middleware
β βββ routes.go # Main routing setup
β βββ auth/, comments/, posts/, subs/, users/, votes/
β βββ *.go # Route group definitions
β
βββ pkg/ # Shared utilities and middleware
β βββ auth.go # Authentication utilities
β βββ rate_limit.go # Rate limiting middleware
β
βββ memory-bank/ # Project documentation (see .clinerules/)
β βββ projectbrief.md # Core requirements and goals
β βββ productContext.md # Why product exists, UX goals
β βββ systemPatterns.md # Architecture and design patterns
β βββ techContext.md # Technology stack and setup
β βββ activeContext.md # Current development focus
β βββ progress.md # Development progress tracking
β
βββ .vscode/
βββ .env # Environment variables (development)
The API follows Clean Architecture principles with clear separation of concerns:
βββββββββββββββββββ
β Routes Layer β β Gin Router Groups
βββββββββββββββββββ€
β Handlers Layer β β HTTP Request/Response
βββββββββββββββββββ€
β Services Layer β β Business Logic & Validation
βββββββββββββββββββ€
βRepository Layer β β Database Operations
βββββββββββββββββββ€
β Models Layer β β Data Transfer Objects
βββββββββββββββββββ
- Repository Pattern: Abstract database operations
- Service Layer: Business logic orchestration
- DTO Pattern: Separate internal/external representations
- Dependency Injection: Clean component coupling
- Go 1.21+
- PostgreSQL 13+
- Docker (optional, for containerized deployment)
-
Clone the repository
git clone https://github.com/CodeAndCraft-Online/cortex-api.git cd cortex-api
-
Environment Configuration
cp .vscode/.env .env # Edit .env with your database credentials
-
Database Setup
# Ensure PostgreSQL is running locally createdb cortex_db psql -d cortex_db -f create_database.sql
-
Install Dependencies
go mod download
-
Run the Application
go run main.go # API will be available at http://localhost:8080
# Pull from GitHub Container Registry
docker pull ghcr.io/codeandcraft-online/cortex-api:latest
# Run the container
docker run -p 8080:8080 --env-file .env ghcr.io/codeandcraft-online/cortex-api:latest
# Or build locally
docker build -t cortex-api .
docker run -p 8080:8080 --env-file .env cortex-api
POST /auth/register
- User registrationPOST /auth/login
- User loginPOST /auth/refresh
- Refresh access tokenPOST /auth/reset-password
- Request password reset
GET /users/:id
- Get user profilePUT /users/:id
- Update user profile
GET /subs
- List available communities (public + authorized private)GET /subs/:id/members
- List community members (access-controlled)GET /subs/:id/pending-invites
- View pending invitations (owner-only)POST /subs
- Create new communityPATCH /subs/:id
- Update community settings (owner-only)DELETE /subs/:id
- Delete community (owner-only)POST /subs/:id/join
- Join community (public or with invitation)POST /subs/:id/invite
- Invite user to private community (owner-only)
GET /posts
- List posts (with pagination)GET /posts/:id
- Get specific postPOST /posts
- Create new postPUT /posts/:id
- Update postDELETE /posts/:id
- Delete post
GET /posts/:id/comments
- Get post commentsPOST /posts/:id/comments
- Create commentPUT /comments/:id
- Update commentDELETE /comments/:id
- Delete comment
POST /posts/:id/vote
- Vote on postPOST /comments/:id/vote
- Vote on commentDELETE /votes/:id
- Remove vote
- Complete PostgreSQL database schema
- JWT authentication system
- User registration and login
- Full community (sub) management with complete CRUD operations
- Private/public community support with invitation system
- Community administration features (member management, invitation oversight)
- Post creation and retrieval
- Comment system foundation
- Voting system foundation
- Complete comment CRUD operations
- Full voting system implementation
- Password reset flow completion
- Image upload handling
- Rate limiting expansion
- API documentation (OpenAPI/Swagger)
- Comprehensive testing suite
- Email notifications
- Caching layer implementation
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow Clean Architecture principles
- Write comprehensive tests
- Maintain consistent error handling
- Use proper Go naming conventions
- Document complex business logic
The project includes comprehensive unit and integration tests with the following coverage areas:
- Repository Layer: Database operations and data access
- Service Layer: Business logic and validation
- Handler Layer: HTTP request/response handling
- Integration Tests: Full API endpoint testing
- testify - Assertions and test utilities
- dockertest - Integration test database setup
- sqlmock - SQL mocking for unit tests
# Using the test script
./scripts/run-tests.sh
# Or manually
go test -v ./... -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
# Build and run tests in container
docker build -t cortex-api-test .
docker run --rm cortex-api-test go test ./...
- GitHub Actions workflow runs on every push/PR
- PostgreSQL service for integration testing
- Codecov integration for coverage tracking
- Coverage reports are generated and stored as artifacts
tests/
βββ unit/ # Unit tests (mocked dependencies)
βββ integration/ # Integration tests (real DB)
βββ coverage/ # Test coverage reports
This project is licensed under the MIT License - see the LICENSE file for details.
For support, create an issue in the GitHub repository.
Built with β€οΈ using Go, Gin, and PostgreSQL