eddycjy
go-gin-example
Go

An example of gin

Last updated Jul 7, 2026
7.2k
Stars
1.6k
Forks
52
Issues
+2
Stars/day
Attention Score
91
Language breakdown
No language data available.
โ–ธ Files click to expand
README

Go Gin Example - Blog APIrcard GoDoc License

A production-ready RESTful blog API example built with Go and Gin framework, demonstrating real-world patterns and best practices.

็ฎ€ไฝ“ไธญๆ–‡

Project Overview

This project is a comprehensive blog backend API system that provides complete article and tag management functionalities, along with features like JWT authentication, image upload, QR code generation, and Excel import/export.

Tech Stack

| Category | Technology | |----------|------------| | Language | Go | | Web Framework | Gin | | ORM | GORM | | Database | MySQL | | Cache | Redis (via Redigo) | | Authentication | JWT (jwt-go) | | Configuration | go-ini | | API Documentation | Swagger | | Excel Processing | excelize, xlsx | | Image Processing | freetype, barcode | | Validation | beego/validation |

Project Structure

go-gin-example/
โ”œโ”€โ”€ conf/                       # Configuration files
โ”‚   โ””โ”€โ”€ app.ini                 # Application configuration
โ”œโ”€โ”€ docs/                       # Documentation
โ”‚   โ”œโ”€โ”€ sql/                    # Database scripts
โ”‚   โ”‚   โ””โ”€โ”€ blog.sql            # Database schema
โ”‚   โ””โ”€โ”€ swagger/                # Swagger documentation
โ”œโ”€โ”€ middleware/                 # Middleware
โ”‚   โ””โ”€โ”€ jwt/                    # JWT authentication middleware
โ”‚       โ””โ”€โ”€ jwt.go
โ”œโ”€โ”€ models/                     # Data models (ORM)
โ”‚   โ”œโ”€โ”€ article.go              # Article model
โ”‚   โ”œโ”€โ”€ auth.go                 # Auth model
โ”‚   โ”œโ”€โ”€ models.go               # Database initialization
โ”‚   โ””โ”€โ”€ tag.go                  # Tag model
โ”œโ”€โ”€ pkg/                        # Shared packages
โ”‚   โ”œโ”€โ”€ app/                    # Application utilities
โ”‚   โ”‚   โ”œโ”€โ”€ form.go             # Form binding
โ”‚   โ”‚   โ”œโ”€โ”€ request.go          # Request handling
โ”‚   โ”‚   โ””โ”€โ”€ response.go         # Response formatting
โ”‚   โ”œโ”€โ”€ e/                      # Error codes
โ”‚   โ”‚   โ”œโ”€โ”€ cache.go            # Cache key constants
โ”‚   โ”‚   โ”œโ”€โ”€ code.go             # Error code definitions
โ”‚   โ”‚   โ””โ”€โ”€ msg.go              # Error messages
โ”‚   โ”œโ”€โ”€ export/                 # Excel export utilities
โ”‚   โ”‚   โ””โ”€โ”€ excel.go
โ”‚   โ”œโ”€โ”€ file/                   # File utilities
โ”‚   โ”‚   โ””โ”€โ”€ file.go
โ”‚   โ”œโ”€โ”€ gredis/                 # Redis client
โ”‚   โ”‚   โ””โ”€โ”€ redis.go
โ”‚   โ”œโ”€โ”€ logging/                # Logging utilities
โ”‚   โ”‚   โ”œโ”€โ”€ file.go
โ”‚   โ”‚   โ””โ”€โ”€ log.go
โ”‚   โ”œโ”€โ”€ qrcode/                 # QR code generation
โ”‚   โ”‚   โ””โ”€โ”€ qrcode.go
โ”‚   โ”œโ”€โ”€ setting/                # Configuration management
โ”‚   โ”‚   โ””โ”€โ”€ setting.go
โ”‚   โ”œโ”€โ”€ upload/                 # Image upload utilities
โ”‚   โ”‚   โ””โ”€โ”€ image.go
โ”‚   โ””โ”€โ”€ util/                   # Common utilities
โ”‚       โ”œโ”€โ”€ jwt.go              # JWT utilities
โ”‚       โ”œโ”€โ”€ md5.go              # MD5 hashing
โ”‚       โ”œโ”€โ”€ pagination.go       # Pagination helper
โ”‚       โ””โ”€โ”€ util.go
โ”œโ”€โ”€ routers/                    # Route definitions
โ”‚   โ”œโ”€โ”€ api/                    # API handlers
โ”‚   โ”‚   โ”œโ”€โ”€ v1/                 # API v1 handlers
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ article.go      # Article endpoints
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ tag.go          # Tag endpoints
โ”‚   โ”‚   โ”œโ”€โ”€ auth.go             # Authentication endpoint
โ”‚   โ”‚   โ””โ”€โ”€ upload.go           # Image upload endpoint
โ”‚   โ””โ”€โ”€ router.go               # Route initialization
โ”œโ”€โ”€ runtime/                    # Runtime resources
โ”‚   โ”œโ”€โ”€ fonts/                  # Font files
โ”‚   โ””โ”€โ”€ qrcode/                 # QR code resources
โ”œโ”€โ”€ service/                    # Business logic layer
โ”‚   โ”œโ”€โ”€ article_service/        # Article services
โ”‚   โ”‚   โ”œโ”€โ”€ article.go          # Article CRUD operations
โ”‚   โ”‚   โ””โ”€โ”€ article_poster.go   # Poster generation
โ”‚   โ”œโ”€โ”€ auth_service/           # Auth services
โ”‚   โ”‚   โ””โ”€โ”€ auth.go
โ”‚   โ”œโ”€โ”€ cache_service/          # Cache key generation
โ”‚   โ”‚   โ”œโ”€โ”€ article.go
โ”‚   โ”‚   โ””โ”€โ”€ tag.go
โ”‚   โ””โ”€โ”€ tag_service/            # Tag services
โ”‚       โ””โ”€โ”€ tag.go
โ”œโ”€โ”€ Dockerfile                  # Docker build file
โ”œโ”€โ”€ Makefile                    # Build automation
โ”œโ”€โ”€ go.mod                      # Go module definition
โ”œโ”€โ”€ go.sum                      # Dependency checksums
โ””โ”€โ”€ main.go                     # Application entry point

Architecture

The project follows a layered architecture pattern:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                      HTTP Requests                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Middleware Layer                         โ”‚
โ”‚                   (JWT Authentication)                      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     Router Layer                            โ”‚
โ”‚                (routers/api/v1/*.go)                        โ”‚
โ”‚        - Request validation                                 โ”‚
โ”‚        - Parameter binding                                  โ”‚
โ”‚        - Response formatting                                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Service Layer                            โ”‚
โ”‚                   (service//.go)                          โ”‚
โ”‚        - Business logic                                     โ”‚
โ”‚        - Cache management                                   โ”‚
โ”‚        - Cross-model operations                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     Model Layer                             โ”‚
โ”‚                    (models/*.go)                            โ”‚
โ”‚        - Database operations                                โ”‚
โ”‚        - CRUD methods                                       โ”‚
โ”‚        - Data structures                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Storage Layer                            โ”‚
โ”‚              MySQL (Primary) + Redis (Cache)                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Feature Tree

Go Gin Blog API
โ”œโ”€โ”€ Authentication
โ”‚   โ””โ”€โ”€ JWT Login Validation
โ”‚       โ”œโ”€โ”€ Token Generation (3-hour expiry)
โ”‚       โ”œโ”€โ”€ Token Validation
โ”‚       โ””โ”€โ”€ Token Refresh
โ”œโ”€โ”€ Article Management
โ”‚   โ”œโ”€โ”€ Create Article
โ”‚   โ”œโ”€โ”€ Read Article (with Redis caching)
โ”‚   โ”œโ”€โ”€ Update Article
โ”‚   โ”œโ”€โ”€ Delete Article (soft delete)
โ”‚   โ”œโ”€โ”€ List Articles (paginated)
โ”‚   โ”œโ”€โ”€ Count Articles
โ”‚   โ””โ”€โ”€ Generate Article Poster
โ”‚       โ”œโ”€โ”€ Embed QR Code
โ”‚       โ”œโ”€โ”€ Apply Background Image
โ”‚       โ”œโ”€โ”€ Render Text Overlay
โ”‚       โ””โ”€โ”€ Save Merged Image
โ”œโ”€โ”€ Tag Management
โ”‚   โ”œโ”€โ”€ CRUD Operations
โ”‚   โ”‚   โ”œโ”€โ”€ Create Tag
โ”‚   โ”‚   โ”œโ”€โ”€ Read Tags (paginated, cached)
โ”‚   โ”‚   โ”œโ”€โ”€ Update Tag
โ”‚   โ”‚   โ””โ”€โ”€ Delete Tag (soft delete)
โ”‚   โ”œโ”€โ”€ Export Tags to Excel
โ”‚   โ””โ”€โ”€ Import Tags from Excel
โ”œโ”€โ”€ File Upload
โ”‚   โ””โ”€โ”€ Image Upload
โ”‚       โ”œโ”€โ”€ Format validation (.jpg, .jpeg, .png)
โ”‚       โ”œโ”€โ”€ Size validation (max 5MB)
โ”‚       โ””โ”€โ”€ MD5-based naming
โ”œโ”€โ”€ API Documentation
โ”‚   โ””โ”€โ”€ Swagger UI (/swagger/*any)
โ””โ”€โ”€ Static File Serving
    โ”œโ”€โ”€ Exported Excel files (/export)
    โ”œโ”€โ”€ Uploaded images (/upload/images)
    โ””โ”€โ”€ Generated QR codes (/qrcode)

API Endpoints

Public Endpoints

| Method | Endpoint | Description | |--------|----------|-------------| | POST | /auth | User authentication, returns JWT token | | GET | /swagger/*any | Swagger API documentation | | POST | /upload | Image upload | | POST | /tags/export | Export tags to Excel | | POST | /tags/import | Import tags from Excel |

Protected Endpoints (Require JWT Token)

Tags

| Method | Endpoint | Description | |--------|----------|-------------| | GET | /api/v1/tags | Get tag list (paginated) | | POST | /api/v1/tags | Create new tag | | PUT | /api/v1/tags/:id | Update tag by ID | | DELETE | /api/v1/tags/:id | Delete tag by ID |

Articles

| Method | Endpoint | Description | |--------|----------|-------------| | GET | /api/v1/articles | Get article list (paginated) | | GET | /api/v1/articles/:id | Get article by ID | | POST | /api/v1/articles | Create new article | | PUT | /api/v1/articles/:id | Update article by ID | | DELETE | /api/v1/articles/:id | Delete article by ID | | POST | /api/v1/articles/poster/generate | Generate article poster with QR code |

Database Schema

Tables

blog_auth - User authentication

- id: INT (PK, AUTO_INCREMENT) 
  • username: VARCHAR(50)
  • password: VARCHAR(50)
blog_tag - Article tags
- id: INT (PK, AUTO_INCREMENT) 
  • name: VARCHAR(100) - Tag name
  • created_on: INT - Creation timestamp
  • created_by: VARCHAR(100) - Creator
  • modified_on: INT - Modification timestamp
  • modified_by: VARCHAR(100) - Modifier
  • deleted_on: INT - Deletion timestamp (soft delete)
  • state: TINYINT - Status (0: disabled, 1: enabled)
blog_article - Articles
- id: INT (PK, AUTO_INCREMENT) 
  • tag_id: INT (FK) - Associated tag ID
  • title: VARCHAR(100) - Article title
  • desc: VARCHAR(255) - Description
  • content: TEXT - Article content
  • coverimageurl: VARCHAR(255) - Cover image URL
  • created_on: INT - Creation timestamp
  • created_by: VARCHAR(100) - Creator
  • modified_on: INT - Modification timestamp
  • modified_by: VARCHAR(255) - Modifier
  • deleted_on: INT - Deletion timestamp (soft delete)
  • state: TINYINT - Status

Configuration

Configuration is managed through conf/app.ini:

[app]
PageSize = 10                    # Pagination page size
JwtSecret = 233                  # JWT signing secret
PrefixUrl = http://127.0.0.1:8000
RuntimeRootPath = runtime/
ImageSavePath = upload/images/
ImageMaxSize = 5                 # Max image size in MB
ImageAllowExts = .jpg,.jpeg,.png
ExportSavePath = export/
QrCodeSavePath = qrcode/
FontSavePath = fonts/
LogSavePath = logs/

[server] RunMode = debug # debug or release HttpPort = 8000 ReadTimeout = 60 # seconds WriteTimeout = 60 # seconds

[database] Type = mysql User = root Password = rootroot Host = 127.0.0.1:3306 Name = blog TablePrefix = blog_

[redis] Host = 127.0.0.1:6379 Password = MaxIdle = 30 MaxActive = 30 IdleTimeout = 200

Getting Started

Prerequisites

  • Go 1.13+
  • MySQL 5.6+
  • Redis

Database Setup

  • Create a MySQL database named blog
  • Execute the SQL script:
mysql -u root -p blog < docs/sql/blog.sql

Configuration

  • Edit conf/app.ini to match your environment
  • Update database credentials
  • Update Redis connection settings

Running the Application

# Build
make build

Run

./go-gin-example

Or run directly

go run main.go

The server will start at http://localhost:8000

Using Docker

# Build image
docker build -t go-gin-example .

Run container

docker run -p 8000:8000 go-gin-example

API Usage Examples

1. Get Authentication Token

curl -X POST http://localhost:8000/auth \
  -d "username=test&password=test123"

Response:

{   "code": 200,   "msg": "ok",   "data": {     "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."   } }

2. Create a Tag

curl -X POST "http://localhost:8000/api/v1/tags?token=YOUR_TOKEN" \
  -d "name=Go&created_by=admin&state=1"

3. Get Tags List

curl "http://localhost:8000/api/v1/tags?token=YOUR_TOKEN"

4. Create an Article

curl -X POST "http://localhost:8000/api/v1/articles?token=YOUR_TOKEN" \
  -d "tagid=1&title=Hello Gin&desc=Introduction to Gin&content=Article content...&createdby=admin&coverimageurl=http://example.com/image.jpg&state=1"

5. Upload an Image

curl -X POST http://localhost:8000/upload \
  -F "image=@/path/to/image.jpg"

6. Export Tags to Excel

curl -X POST http://localhost:8000/tags/export

Key Design Patterns

1. Soft Delete

All models use soft delete by setting deleted_on timestamp instead of actual deletion.

2. Redis Caching

Articles and tags are cached in Redis with 1-hour TTL to reduce database load.

3. Service Layer Pattern

Business logic is separated into service layer, keeping handlers thin and focused on request/response handling.

4. Unified Response Format

All API responses follow consistent format:
{
  "code": 200,
  "msg": "ok",
  "data": {}
}

5. Custom GORM Callbacks

Custom callbacks for automatic timestamp management:
  • CreatedOn set on create
  • ModifiedOn updated on modifications
  • DeletedOn set on soft delete

Error Codes

| Code | Description | |------|-------------| | 200 | Success | | 400 | Invalid parameters | | 500 | Internal server error | | 10001 | Tag already exists | | 10003 | Tag not found | | 10011 | Article not found | | 20001 | Token validation failed | | 20002 | Token expired | | 20003 | Token generation error | | 20004 | Authentication failed | | 30001 | Image save failed | | 30002 | Image check failed | | 30003 | Invalid image format |

Development Commands

# Build
make build

Run code analysis

make tool

Run linter

make lint

Clean build artifacts

make clean

License

MIT License - See LICENSE for details.

Credits

Project by EDDYCJY

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท eddycjy/go-gin-example ยท Updated daily from GitHub