help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
go-rest-api
/
internal
/
handlers
RSK World
go-rest-api
Go REST API - Enterprise-grade REST API with JWT Authentication + PostgreSQL + Redis Caching + Docker + Comprehensive Testing + Educational Design
handlers
  • api_handler.go1.4 KB
  • auth_handler.go4.4 KB
  • auth_handler_test.go4.6 KB
  • category_handler.go5.2 KB
  • product_handler.go8.8 KB
  • upload_handler.go5 KB
README.mdLICENSE
README.md
Raw Download

README.md

# Go REST API
**ID**: 23
**Category**: Go Projects
**Difficulty**: Advanced

## Description
A comprehensive RESTful API service built with Go programming language. Features advanced authentication, role-based access control, product management with categories, file uploads, search and filtering, Redis caching, comprehensive testing, and API versioning. Perfect for learning enterprise-grade Go development, microservices architecture, and modern API design patterns.

## 📋 Demo & Documentation
🎯 **[View Interactive Demo](./index.html)** - Complete project overview with installation guide, API documentation, and live examples.

> **Quick Start**: Open `demo.html` in your browser for a comprehensive guide to get started immediately!

## Features
- **Authentication & Authorization**
- JWT-based authentication
- Role-based access control (Admin/User)
- Password hashing with bcrypt
- Account activation/deactivation

- **Product Management**
- CRUD operations for products
- Product categories with relationships
- Image upload functionality
- Search and advanced filtering
- Pagination support

- **API Features**
- RESTful API design
- API versioning (v1)
- Comprehensive middleware stack
- Request/response logging
- CORS support
- Rate limiting
- Input validation

- **Performance & Caching**
- Redis caching for frequently accessed data
- Database query optimization
- Efficient pagination

- **Development & Testing**
- Comprehensive unit tests
- Integration tests
- Test coverage reporting
- Docker containerization
- Makefile for common tasks

- **Documentation**
- Swagger/OpenAPI documentation
- Comprehensive API documentation
- Code documentation

## Technologies
- **Backend**: Go 1.21, Gin Framework
- **Database**: PostgreSQL with GORM ORM
- **Cache**: Redis
- **Authentication**: JWT
- **Validation**: Go Playground Validator
- **File Storage**: Local filesystem with Docker volumes
- **Containerization**: Docker & Docker Compose
- **Documentation**: Swagger/OpenAPI
- **Testing**: Go testing framework

## Prerequisites
- Docker and Docker Compose
- Go 1.21+ (for local development)
- Make (optional, for using Makefile commands)

## Quick Start

### Using Docker (Recommended)
```bash
# Clone the repository
git clone <repository-url>
cd go-rest-api

# Start all services
docker-compose up --build

# The API will be available at http://localhost:8080
# Swagger documentation at http://localhost:8080/swagger/index.html
```

### Local Development
```bash
# Install dependencies
go mod download

# Set up environment variables (copy and modify .env)
cp .env.example .env

# Run database migrations
make seed

# Run the application
make run

# Run tests
make test

# Generate Swagger docs
make swagger-gen
```

## Environment Variables
```bash
# Database
DB_HOST=localhost
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=gorestapi
DB_PORT=5432

# Authentication
JWT_SECRET=your-super-secret-jwt-key

# Server
PORT=8080

# File Upload
UPLOAD_PATH=./uploads
MAX_FILE_SIZE=5242880 # 5MB

# Redis Cache
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
CACHE_ENABLED=true
```

## API Endpoints

### Authentication
- `POST /api/v1/register` - Register a new user
- `POST /api/v1/login` - Login and receive JWT token

### Public Endpoints
- `GET /api/v1/health` - Health check
- `GET /api/v1/categories` - Get all categories
- `GET /api/v1/categories/{id}` - Get category by ID
- `GET /api/v1/products` - Get products with filtering and pagination
- `GET /api/v1/products/{id}` - Get product by ID

### Protected Endpoints (Requires Authentication)
- `GET /api/v1/profile` - Get user profile

### Admin Only Endpoints (Requires Admin Role)
- `POST /api/v1/categories` - Create category
- `PUT /api/v1/categories/{id}` - Update category
- `DELETE /api/v1/categories/{id}` - Delete category
- `POST /api/v1/products` - Create product
- `PUT /api/v1/products/{id}` - Update product
- `DELETE /api/v1/products/{id}` - Delete product
- `POST /api/v1/products/{id}/upload` - Upload product image
- `DELETE /api/v1/products/{id}/image` - Delete product image

## API Usage Examples

### Register User
```bash
curl -X POST http://localhost:8080/api/v1/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}'
```

### Login
```bash
curl -X POST http://localhost:8080/api/v1/login \
-H "Content-Type: application/json" \
-d '{
"email": "john@example.com",
"password": "password123"
}'
```

### Get Products with Filtering
```bash
# Get products with search and pagination
curl "http://localhost:8080/api/v1/products?search=laptop&page=1&limit=10"

# Filter by category and price range
curl "http://localhost:8080/api/v1/products?category_id=1&min_price=100&max_price=1000"

# Get only in-stock products
curl "http://localhost:8080/api/v1/products?in_stock=true"
```

### Create Product (Admin Only)
```bash
curl -X POST http://localhost:8080/api/v1/products \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "New Product",
"description": "Product description",
"price": 99.99,
"stock": 50,
"category_id": 1
}'
```

### Upload Product Image (Admin Only)
```bash
curl -X POST http://localhost:8080/api/v1/products/1/upload \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-F "image=@/path/to/image.jpg"
```

## Database Schema

### Users
- id (Primary Key)
- email (Unique)
- password (Hashed)
- name
- role (admin/user)
- is_active
- last_login
- timestamps

### Categories
- id (Primary Key)
- name (Unique)
- description
- timestamps

### Products
- id (Primary Key)
- name
- description
- price
- stock
- category_id (Foreign Key)
- image (File path)
- timestamps

## Testing

```bash
# Run all tests
make test

# Run tests with verbose output
make test-verbose

# Run tests with coverage
make test-coverage

# View coverage report
open coverage.html
```

## Development Commands

```bash
# Run the application
make run

# Build the application
make build

# Run database seeder
make seed

# Generate Swagger documentation
make swagger-gen

# Clean build artifacts
make clean

# Docker commands
make docker-up # Start services
make docker-down # Stop services
```

## Project Structure
```
├── cmd/
│ ├── api/ # Main application entry point
│ └── seeder/ # Database seeder
├── internal/
│ ├── cache/ # Redis caching layer
│ ├── config/ # Configuration management
│ ├── database/ # Database connection and setup
│ ├── handlers/ # HTTP request handlers
│ ├── middleware/ # Custom middleware
│ ├── models/ # Database models
│ ├── response/ # Response utilities
│ ├── routes/ # Route definitions
│ └── validation/ # Input validation
├── tests/ # Integration tests
├── docs/ # Swagger documentation
├── docker-compose.yml
├── Dockerfile
├── Makefile
├── go.mod
└── README.md
```

## API Documentation
Complete API documentation is available via Swagger UI at:
`http://localhost:8080/swagger/index.html`

## Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Ensure all tests pass
6. Submit a pull request

## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Support
- **Author**: RSK World
- **Website**: [RSK World](https://rskworld.in/contact.php)
- **Email**: help@rskworld.in / support@rskworld.in
- **Year**: 2026

## Source
[Download Source](./go-rest-api/go-rest-api.zip)
LICENSE
Raw Download
Find: Go to:
MIT License

Copyright (c) 2026 RSK World

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Contact: help@rskworld.in / support@rskworld.in
Website: https://rskworld.in/contact.php
25 lines•1.2 KB
text
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer