Skip to content

Backend API in Go for a scalable blog platform with authentication, role management, CRUD, search, filtering, and AI content features.

Notifications You must be signed in to change notification settings

naolaboma/Blog-Platform-API

 
 

Repository files navigation

Blog API

A robust, scalable RESTful API for a blog platform built with Go, featuring user authentication, blog management, AI-powered content generation, and comprehensive search capabilities.

Table of Contents

Features

Core Functionality

  • User Management: Registration, authentication, profile management, and role-based access control
  • Blog Management: Create, read, update, and delete blog posts with embedded comments and reactions
  • Search & Filtering: Advanced search by title, author, tags, and date with pagination support
  • AI Integration: Powered by Groq AI for blog content generation, enhancement, and idea suggestions
  • Authentication: JWT-based authentication with refresh tokens and session management
  • OAuth Integration: Support for Google and GitHub authentication
  • Email Services: Email verification and password reset functionality
  • File Upload: Profile picture upload with validation and storage

Technical Features

  • High Performance: Redis caching, database indexing, and optimized queries
  • Scalability: Worker pools, goroutines, and connection pooling
  • Security: Password hashing, input validation, and role-based authorization
  • Monitoring: Graceful shutdown, health checks, and error handling
  • Documentation: Comprehensive Postman collection and API documentation

Technology Stack

  • Backend: Go 1.23+
  • Framework: Gin (HTTP web framework)
  • Database: MongoDB with proper indexing
  • Cache: Redis for performance optimization
  • Authentication: JWT tokens with bcrypt password hashing
  • AI Integration: Groq API
  • OAuth: Google and GitHub integration
  • Email: SMTP with HTML templates
  • File Storage: Local filesystem with validation
  • Validation: Go validator package

Prerequisites

Before running this application, ensure you have the following installed:

  • Go: Version 1.23 or higher
  • MongoDB: Version 6.0 or higher
  • Redis: Version 7.0 or higher
  • Git: For cloning the repository

Installation

  1. Clone the repository:
git clone <repository-url>
cd Blog-API
  1. Install Go dependencies:
go mod download
  1. Create environment configuration file:
cp .env.example .env

Configuration

Create a .env file in the root directory as the example env

Database Setup

Option 1: Using the Setup Script

  1. Ensure MongoDB is running:
sudo systemctl start mongod
sudo systemctl enable mongod
  1. Run the setup script:
mongosh < mongodb_setup.js

Running the Application

  1. Start Redis server:
sudo systemctl start redis-server
sudo systemctl enable redis-server
  1. Run the application:
go run cmd/server/main.go

The server will start on http://localhost:8080 by default.

API Documentation

Base URL

http://localhost:8080/api/v1

Authentication Endpoints

User Registration

POST /auth/register
Content-Type: application/json

{
  "username": "testuser",
  "email": "test@example.com",
  "password": "SecurePass123!"
}

User Login

POST /auth/login
Content-Type: application/json

{
  "email": "test@example.com",
  "password": "SecurePass123!"
}

Refresh Token

POST /auth/refresh
Content-Type: application/json

{
  "refresh_token": "your-refresh-token"
}

Logout

POST /auth/logout
Authorization: Bearer <access-token>

Email Verification

POST /auth/send-verification
Content-Type: application/json

{
  "email": "test@example.com"
}

Password Reset

POST /auth/forgot-password
Content-Type: application/json

{
  "email": "test@example.com"
}

Blog Endpoints

Get All Blogs

GET /blogs?page=1&limit=10&sort=newest

Get Blog by ID

GET /blogs/{blog-id}

Create Blog (Authenticated)

POST /blogs
Authorization: Bearer <access-token>
Content-Type: application/json

{
  "title": "My Blog Post",
  "content": "This is the content of my blog post.",
  "tags": ["technology", "programming"]
}

Update Blog (Author/Admin Only)

PUT /blogs/{blog-id}
Authorization: Bearer <access-token>
Content-Type: application/json

{
  "title": "Updated Blog Title",
  "content": "Updated content."
}

Delete Blog (Author/Admin Only)

DELETE /blogs/{blog-id}
Authorization: Bearer <access-token>

Search Blogs by Title

GET /blogs/search/title?title=search-term&page=1&limit=10

Search Blogs by Author

GET /blogs/search/author?author=username&page=1&limit=10

Filter Blogs by Tags

GET /blogs/filter/tags?tags=technology,programming&page=1&limit=10

Get Popular Blogs

GET /blogs/popular?limit=10

User Management Endpoints

Get User Profile (Authenticated)

GET /users/profile
Authorization: Bearer <access-token>

Update User Profile (Authenticated)

PUT /users/profile
Authorization: Bearer <access-token>
Content-Type: application/json

{
  "username": "newusername",
  "bio": "Updated bio information"
}

Upload Profile Picture (Authenticated)

POST /users/profile/picture
Authorization: Bearer <access-token>
Content-Type: multipart/form-data

profile_picture: <file>

AI Integration Endpoints

Generate Blog Content (Authenticated)

POST /ai/generate-blog
Authorization: Bearer <access-token>
Content-Type: application/json

{
  "topic": "Go Programming Best Practices"
}

Enhance Blog Content (Authenticated)

POST /ai/enhance-blog
Authorization: Bearer <access-token>
Content-Type: application/json

{
  "content": "Your blog content here..."
}

Suggest Blog Ideas (Authenticated)

POST /ai/suggest-ideas
Authorization: Bearer <access-token>
Content-Type: application/json

{
  "keywords": ["technology", "programming", "golang"]
}

Admin Endpoints

Promote User to Admin

PUT /admin/users/{user-id}/promote
Authorization: Bearer <admin-access-token>
Content-Type: application/json

{
  "role": "admin"
}

Demote Admin to User

PUT /admin/users/{user-id}/demote
Authorization: Bearer <admin-access-token>
Content-Type: application/json

{
  "role": "user"
}

Project Structure

Blog-API/
├── cmd/
│   └── server/
│       └── main.go                 # Application entry point
├── internal/
│   ├── delivery/                   # HTTP layer
│   │   ├── controllers/           # Request handlers
│   │   └── router/                # Route definitions
│   ├── domain/                    # Business logic interfaces
│   ├── infrastructure/            # External services
│   │   ├── ai/                   # AI service integration
│   │   ├── cache/                # Redis caching
│   │   ├── database/             # MongoDB connection
│   │   ├── email/                # Email service
│   │   ├── filesystem/           # File upload handling
│   │   ├── jwt/                  # JWT authentication
│   │   ├── middleware/           # HTTP middleware
│   │   ├── oauth/                # OAuth integration
│   │   ├── password/             # Password utilities
│   │   └── worker/               # Background job processing
│   ├── repository/               # Data access layer
│   └── usecase/                  # Business logic implementation
├── pkg/
│   └── config/                   # Configuration management
├── docs/
│   └── postman/                  # API documentation
├── mongodb_collections.json      # Database schema documentation
├── mongodb_setup.js             # Database setup script
└── README.md                     # This file

Testing

Manual Testing

Use the provided Postman collection in docs/postman/Blog-API.postman_collection.json to test all endpoints.

Automated Testing

# Run all tests
go test ./...

# Run tests with coverage
go test -cover ./...

# Run specific package tests
go test ./internal/usecase

Deployment

Production Considerations

  1. Environment Variables: Ensure all sensitive configuration is properly set
  2. Database: Use production MongoDB instance with proper authentication
  3. Redis: Configure Redis with authentication and persistence
  4. HTTPS: Enable TLS/SSL for production endpoints
  5. Monitoring: Implement logging, metrics, and health checks
  6. Scaling: Consider horizontal scaling with load balancers

Development Guidelines

  • Follow Go coding standards and conventions
  • Write comprehensive tests for new features
  • Update documentation for API changes
  • Ensure all tests pass before submitting PR
  • Use meaningful commit messages
  • Follow pure clean architecture approach

About

Backend API in Go for a scalable blog platform with authentication, role management, CRUD, search, filtering, and AI content features.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 52.4%
  • TypeScript 44.3%
  • HTML 1.6%
  • JavaScript 1.4%
  • CSS 0.3%