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.
- Features
- Technology Stack
- Prerequisites
- Installation
- Configuration
- Database Setup
- Running the Application
- API Documentation
- Project Structure
- Testing
- Deployment
- Contributing
- License
- 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
- 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
- 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
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
- Clone the repository:
git clone <repository-url>
cd Blog-API- Install Go dependencies:
go mod download- Create environment configuration file:
cp .env.example .envCreate a .env file in the root directory as the example env
- Ensure MongoDB is running:
sudo systemctl start mongod
sudo systemctl enable mongod- Run the setup script:
mongosh < mongodb_setup.js- Start Redis server:
sudo systemctl start redis-server
sudo systemctl enable redis-server- Run the application:
go run cmd/server/main.goThe server will start on http://localhost:8080 by default.
http://localhost:8080/api/v1
POST /auth/register
Content-Type: application/json
{
"username": "testuser",
"email": "test@example.com",
"password": "SecurePass123!"
}POST /auth/login
Content-Type: application/json
{
"email": "test@example.com",
"password": "SecurePass123!"
}POST /auth/refresh
Content-Type: application/json
{
"refresh_token": "your-refresh-token"
}POST /auth/logout
Authorization: Bearer <access-token>POST /auth/send-verification
Content-Type: application/json
{
"email": "test@example.com"
}POST /auth/forgot-password
Content-Type: application/json
{
"email": "test@example.com"
}GET /blogs?page=1&limit=10&sort=newestGET /blogs/{blog-id}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"]
}PUT /blogs/{blog-id}
Authorization: Bearer <access-token>
Content-Type: application/json
{
"title": "Updated Blog Title",
"content": "Updated content."
}DELETE /blogs/{blog-id}
Authorization: Bearer <access-token>GET /blogs/search/title?title=search-term&page=1&limit=10GET /blogs/search/author?author=username&page=1&limit=10GET /blogs/filter/tags?tags=technology,programming&page=1&limit=10GET /blogs/popular?limit=10GET /users/profile
Authorization: Bearer <access-token>PUT /users/profile
Authorization: Bearer <access-token>
Content-Type: application/json
{
"username": "newusername",
"bio": "Updated bio information"
}POST /users/profile/picture
Authorization: Bearer <access-token>
Content-Type: multipart/form-data
profile_picture: <file>POST /ai/generate-blog
Authorization: Bearer <access-token>
Content-Type: application/json
{
"topic": "Go Programming Best Practices"
}POST /ai/enhance-blog
Authorization: Bearer <access-token>
Content-Type: application/json
{
"content": "Your blog content here..."
}POST /ai/suggest-ideas
Authorization: Bearer <access-token>
Content-Type: application/json
{
"keywords": ["technology", "programming", "golang"]
}PUT /admin/users/{user-id}/promote
Authorization: Bearer <admin-access-token>
Content-Type: application/json
{
"role": "admin"
}PUT /admin/users/{user-id}/demote
Authorization: Bearer <admin-access-token>
Content-Type: application/json
{
"role": "user"
}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
Use the provided Postman collection in docs/postman/Blog-API.postman_collection.json to test all endpoints.
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific package tests
go test ./internal/usecase- Environment Variables: Ensure all sensitive configuration is properly set
- Database: Use production MongoDB instance with proper authentication
- Redis: Configure Redis with authentication and persistence
- HTTPS: Enable TLS/SSL for production endpoints
- Monitoring: Implement logging, metrics, and health checks
- Scaling: Consider horizontal scaling with load balancers
- 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