Skip to content

nanotaboada/go-samples-gin-restful

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

πŸ§ͺ RESTful API with Go and Gin

Go CI Quality Gate Status Codacy Badge codecov Go Report Card CodeFactor License: MIT

A proof-of-concept RESTful API built with Go and Gin demonstrating best practices for building production-ready web services with full CRUD operations, automated testing, and containerized deployment.

Table of Contents

Features

  • πŸ”Œ RESTful API with full CRUD operations for player management
  • πŸ“š Interactive API Documentation with Swagger UI and Postman collection
  • ⚑ In-memory Caching for improved performance (1-hour TTL)
  • βœ… Comprehensive Testing with 80%+ code coverage requirements
  • 🐳 Docker Ready with multi-stage builds and health checks
  • πŸ”„ CI/CD Pipeline with automated testing and deployment
  • πŸ—οΈ Layered Architecture with clear separation of concerns
  • πŸ’Ύ Pre-seeded Database for immediate testing and exploration

Tech Stack

Category Technology Description
Language Go 1.24.1 Primary programming language
Web Framework Gin High-performance HTTP web framework
ORM GORM Developer-friendly ORM library
Database SQLite Lightweight embedded database
API Documentation Swagger/OpenAPI Interactive API documentation via swaggo
Caching gin-contrib/cache In-memory caching middleware
Testing testify Assertion and mocking framework
Containerization Docker & Docker Compose Container platform and orchestration

Architecture

Simplified, conceptual project structure and main application flow

The application follows a layered architecture pattern with clear separation of concerns:

  • Route Layer (route/) - URL routing and middleware configuration
  • Controller Layer (controller/) - HTTP request/response handling and input validation
  • Service Layer (service/) - Business logic and data operations
  • Data Layer (data/) - Database connectivity and ORM configuration
  • Model Layer (model/) - Data structures and entity definitions (shared across layers)

Data Flow: HTTP Request β†’ Route β†’ Controller β†’ Service β†’ Data/ORM β†’ Database β†’ Response

Prerequisites

Before running this application, ensure you have:

  • Go 1.24.0 or higher - Download
  • Docker & Docker Compose (optional, for containerized deployment) - Download
  • Git - For cloning the repository

Getting Started

Local Development

  1. Clone the repository
git clone https://github.com/nanotaboada/go-samples-gin-restful.git
cd go-samples-gin-restful
  1. Install dependencies
go mod download
  1. Run the application
go run .

The API will be available at http://localhost:9000

  1. Access Swagger documentation

Open your browser and navigate to:

Docker Deployment

This setup uses Docker Compose to build and run the app with a persistent SQLite database in a Docker volume.

  1. Build the Docker image
docker compose build
  1. Start the application
docker compose up

The API will be available at http://localhost:9000

Note: On first run, the container initializes a pre-seeded SQLite database in a persistent volume. On subsequent runs, the existing data is preserved.

  1. Stop the application
docker compose down
  1. Reset the database (optional)
docker compose down -v

This removes the volume and reinitializes the database on the next startup.

Environment Variables

Variable Description Default
STORAGE_PATH Path to SQLite database file ./storage/players-sqlite3.db
GIN_MODE Gin framework mode (debug, release, test) debug

API Reference

Available Endpoints

Method Endpoint Description
GET /health Health check endpoint
GET /players Retrieve all players
GET /players/:id Retrieve a player by ID
GET /players/squadnumber/:squadnumber Retrieve a player by squad number
POST /players Create a new player
PUT /players/:id Update an existing player
DELETE /players/:id Delete a player

Usage Examples

# Get all players
curl http://localhost:9000/players

# Get a specific player
curl http://localhost:9000/players/10

# Create a new player
curl -X POST http://localhost:9000/players \
  -H "Content-Type: application/json" \
  -d '{
    "id": 27,
    "firstName": "Paulo",
    "lastName": "Dybala",
    "squadNumber": 21,
    "position": "Forward",
    "team": "AS Roma"
  }'

Swagger UI & Postman

Interactive Documentation: http://localhost:9000/swagger/index.html

Postman Collection: postman-collections/go-samples-gin-restful.postman_collection.json

Swagger UI

The Swagger documentation is automatically generated from code annotations using swaggo/swag. To regenerate after making changes:

swag init

Testing

Run the test suite with coverage:

# Run all tests
go test ./...

# Run tests with coverage
go test -v ./... -coverprofile=coverage.out

# Run tests with detailed coverage for specific packages
go test -v ./... \
  -coverpkg=github.com/nanotaboada/go-samples-gin-restful/service,github.com/nanotaboada/go-samples-gin-restful/controller,github.com/nanotaboada/go-samples-gin-restful/route \
  -covermode=atomic \
  -coverprofile=coverage.out

# View coverage report
go tool cover -html=coverage.out

Coverage targets: 80% minimum for service, controller, and route packages.

Contributing

Contributions are welcome! Please read our Contributing Guidelines before submitting pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Note: This is a proof-of-concept project intended for educational and demonstration purposes.

Packages

 
 
 

Contributors 2

  •  
  •