Skip to content

Navigation Menu

Sign in
Sign up

Repository files navigation

my-gin-example

A comprehensive example of a production-ready RESTful API built with Gin framework, featuring modern Go practices, security best practices, JWT authentication, file upload, WebSocket support, and complete CI/CD integration.

✨ Features

Core Features

  • πŸš€ Modern Go 1.24 with latest stable dependencies
  • πŸ” JWT Authentication with role-based access control
  • πŸ“¦ CRUD Operations for users and products
  • πŸ—„οΈ GORM Integration with SQLite (easily switchable to MySQL/PostgreSQL)
  • πŸ“ File Upload Management with security validation
  • πŸ”Œ WebSocket Support for real-time communication
  • βœ… Request Validation using Gin's binding

Quality & Security

  • πŸ”’ Security Hardened - gosec verified, zero vulnerabilities
  • πŸ§ͺ Unit Tests with testify
  • πŸ“ Structured Logging with zerolog
  • βš™οΈ Configuration Management with Viper
  • 🚦 Rate Limiting middleware
  • πŸ” Pagination & Search for list endpoints

DevOps & CI/CD

  • βœ… GitHub Actions - automated testing and security scanning
  • πŸ”„ CircleCI ready configuration
  • 🐳 Docker & Docker Compose support
  • πŸ“Š CodeQL Security Analysis
  • πŸ›‘οΈ Dependabot integration

πŸ† Build Status

Go Build and Test CodeQL Security Scanning

πŸ“‹ Prerequisites

  • Go 1.24 or higher
  • Make (optional, for convenience commands)

πŸš€ Quick Start

# Clone the repository
git clone https://github.com/wmh/my-gin-example.git
cd my-gin-example
# Install dependencies
go mod download
# Run the application
go run main.go

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

βš™οΈ Configuration

Create or edit config/app.toml:

app_port = 8089
[database]
driver = "sqlite"
dsn = "./data/my_gin_example.db"
[jwt]
secret = "your-secret-key-here"
expires_hours = 24
[logs]
common_log = "./data/logs/common.log"
stdout_only = false
[upload]
max_size = 10485760 # 10MB
allowed_types = ["image/jpeg", "image/png", "image/gif", "application/pdf"]

πŸ“š API Endpoints

Authentication

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - Login and get JWT token

Users (Protected)

  • GET /api/v1/users - List users (with pagination)
  • GET /api/v1/users/:id - Get user details
  • PUT /api/v1/users/:id - Update user
  • DELETE /api/v1/users/:id - Delete user

Products (Protected)

  • GET /api/v1/products - List products (with pagination & search)
  • POST /api/v1/products - Create product
  • GET /api/v1/products/:id - Get product details
  • PUT /api/v1/products/:id - Update product
  • DELETE /api/v1/products/:id - Delete product

File Upload (Protected)

  • POST /api/v1/files/upload - Upload single file
  • POST /api/v1/files/batch-upload - Upload multiple files
  • GET /api/v1/files - List uploaded files
  • GET /api/v1/files/:id - Get file details
  • GET /api/v1/files/:id/download - Download file
  • DELETE /api/v1/files/:id - Delete file

WebSocket

  • GET /ws - WebSocket connection for real-time updates

Health Check

  • GET /ok - Simple health check

πŸ§ͺ Testing

# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific package tests
go test ./app/services/...

πŸ”’ Security

This project follows security best practices:

  • βœ… Zero vulnerabilities - verified by govulncheck
  • βœ… gosec compliant - all security warnings resolved
  • βœ… Path traversal protection - file operations validated
  • βœ… Secure file permissions - 0600 for files, 0750 for directories
  • βœ… HTTP timeout configuration - protection against slowloris attacks
  • βœ… Regular dependency updates - via Dependabot

Run security checks locally:

# Check for vulnerabilities
govulncheck ./...
# Run security scanner
gosec ./...

🐳 Docker

# Build Docker image
docker build -t my-gin-example .
# Run with Docker Compose
docker-compose up -d

πŸ› οΈ Development

Using Make commands

# Run the application
make run
# Run tests
make test
# Build binary
make build
# Run with hot reload (using air)
make dev
# Clean build artifacts
make clean

Project Structure

my-gin-example/
β”œβ”€β”€ app/
β”‚ β”œβ”€β”€ controllers/ # Request handlers
β”‚ β”œβ”€β”€ core/ # Core functionality (config, db, logger)
β”‚ β”œβ”€β”€ models/ # Database models
β”‚ β”œβ”€β”€ routes/ # Route definitions
β”‚ └── services/ # Business logic
β”œβ”€β”€ config/ # Configuration files
β”œβ”€β”€ data/ # Database and uploads
β”œβ”€β”€ scripts/ # Utility scripts
β”œβ”€β”€ tests/ # Integration tests
└── main.go # Application entry point

πŸ“– Documentation

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

πŸ“ License

This project is open source and available under the MIT License.

πŸ™ Acknowledgments

Built with:

πŸ“§ Contact

For questions or feedback, please open an issue on GitHub.


Version: 2.0.0
Go Version: 1.24+
Last Updated: December 2025

About

My example of gin

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /