Skip to content

Cortex API is a Reddit-like social media platform backend API built with Go and PostgreSQL. It provides a comprehensive platform for community discussions with posts, comments, voting systems, and private/public communities (subs).

Notifications You must be signed in to change notification settings

CodeAndCraft-Online/cortex-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

61 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cortex API

Go Version Docker License CI Test Coverage

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.

πŸš€ Features

Core Functionality

  • 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

Technical Highlights

  • 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

πŸ›  Technology Stack

  • 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

πŸ“ Project Structure

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)

πŸ— Architecture

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
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Design Patterns

  • Repository Pattern: Abstract database operations
  • Service Layer: Business logic orchestration
  • DTO Pattern: Separate internal/external representations
  • Dependency Injection: Clean component coupling

πŸš€ Getting Started

Prerequisites

  • Go 1.21+
  • PostgreSQL 13+
  • Docker (optional, for containerized deployment)

Local Development Setup

  1. Clone the repository

    git clone https://github.com/CodeAndCraft-Online/cortex-api.git
    cd cortex-api
  2. Environment Configuration

    cp .vscode/.env .env
    # Edit .env with your database credentials
  3. Database Setup

    # Ensure PostgreSQL is running locally
    createdb cortex_db
    psql -d cortex_db -f create_database.sql
  4. Install Dependencies

    go mod download
  5. Run the Application

    go run main.go
    # API will be available at http://localhost:8080

Docker Deployment

# 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

πŸ“‘ API Endpoints

Authentication

  • POST /auth/register - User registration
  • POST /auth/login - User login
  • POST /auth/refresh - Refresh access token
  • POST /auth/reset-password - Request password reset

Users

  • GET /users/:id - Get user profile
  • PUT /users/:id - Update user profile

Communities (Subs)

  • 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 community
  • PATCH /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)

Posts

  • GET /posts - List posts (with pagination)
  • GET /posts/:id - Get specific post
  • POST /posts - Create new post
  • PUT /posts/:id - Update post
  • DELETE /posts/:id - Delete post

Comments

  • GET /posts/:id/comments - Get post comments
  • POST /posts/:id/comments - Create comment
  • PUT /comments/:id - Update comment
  • DELETE /comments/:id - Delete comment

Voting

  • POST /posts/:id/vote - Vote on post
  • POST /comments/:id/vote - Vote on comment
  • DELETE /votes/:id - Remove vote

πŸ”§ Development Status

βœ… Completed Features

  • 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

πŸ”„ In Progress

  • Complete comment CRUD operations
  • Full voting system implementation
  • Password reset flow completion

πŸ“‹ Planned Features

  • Image upload handling
  • Rate limiting expansion
  • API documentation (OpenAPI/Swagger)
  • Comprehensive testing suite
  • Email notifications
  • Caching layer implementation

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow Clean Architecture principles
  • Write comprehensive tests
  • Maintain consistent error handling
  • Use proper Go naming conventions
  • Document complex business logic

πŸ§ͺ Testing

Test Coverage

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

Dependencies

  • testify - Assertions and test utilities
  • dockertest - Integration test database setup
  • sqlmock - SQL mocking for unit tests

Running Tests

Local Development (requires PostgreSQL)

# 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

With Docker (for integration tests)

# Build and run tests in container
docker build -t cortex-api-test .
docker run --rm cortex-api-test go test ./...

CI/CD Coverage

  • 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

Test Structure

tests/
β”œβ”€β”€ unit/               # Unit tests (mocked dependencies)
β”œβ”€β”€ integration/        # Integration tests (real DB)
└── coverage/           # Test coverage reports

πŸ“ License

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

πŸ“ž Support

For support, create an issue in the GitHub repository.


Built with ❀️ using Go, Gin, and PostgreSQL

About

Cortex API is a Reddit-like social media platform backend API built with Go and PostgreSQL. It provides a comprehensive platform for community discussions with posts, comments, voting systems, and private/public communities (subs).

Resources

Stars

Watchers

Forks

Packages