Skip to content

JustSteveKing/go-gin-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Gin Demo

CI Go Report Card codecov Release

A simple HTTP server built with Go and the Gin framework, demonstrating graceful shutdown, middleware, and clean architecture.

Features

  • ✨ Clean, organized code structure
  • πŸš€ Powered by Gin for fast routing and middleware
  • πŸ”„ Graceful shutdown handling
  • πŸ“ Request logging middleware (Gin)
  • πŸ›‘οΈ Panic recovery middleware (Gin)
  • πŸ₯ Health check endpoint
  • πŸ“¦ Version information endpoint

Getting Started

Prerequisites

  • Go 1.21 or higher

Installation

git clone <repository-url>
cd go-demo
go mod download

Running the Server

Using Make (recommended):

make run

Or for development (no binary build):

make dev

Or manually:

go run .

The server will start on http://localhost:8080

Make Commands

This project includes a Makefile for common tasks. Run make help to see all available commands:

make help              # Show all available commands
make build             # Build the application
make test              # Run tests with coverage
make lint              # Run linter
make security          # Run security scanner
make ci                # Run all CI checks locally
make clean             # Clean build artifacts
make install-tools     # Install development tools

API Endpoints (Gin)

GET /

Returns a simple greeting message.

curl http://localhost:8080/

GET /health

Health check endpoint returning JSON status.

curl http://localhost:8080/health

Response:

{"status":"ok"}

GET /version

Returns build version information.

curl http://localhost:8080/version

Response:

{"version":"dev","commit":"","built":""}

Development

Building with Version Info

The Makefile automatically injects build information:

make build

Or build for all platforms:

make build-all

Running CI Checks Locally

Before pushing, you can run the same checks that CI runs:

make ci

This will:

  • Verify dependencies
  • Run linter
  • Run tests with race detection
  • Run security scanner

Installing Development Tools

make install-tools

This installs:

  • golangci-lint (linting)
  • gosec (security scanning)
  • goreleaser (release testing)

Project Structure

.
β”œβ”€β”€ main.go        # Gin server setup and configuration
β”œβ”€β”€ handlers.go    # Gin route handlers
β”œβ”€β”€ middleware.go  # Gin middleware functions
β”œβ”€β”€ types.go       # Build metadata
└── go.mod         # Go module definition

Configuration

Configuration is done via constants in main.go:

  • serverPort - Server listen address (default: :8080)
  • shutdownTimeout - Graceful shutdown timeout (default: 10s)
  • readTimeout - HTTP read timeout (default: 5s)
  • writeTimeout - HTTP write timeout (default: 10s)
  • idleTimeout - HTTP idle timeout (default: 60s)

Graceful Shutdown

The server handles SIGINT and SIGTERM signals for graceful shutdown, allowing in-flight requests to complete before stopping.

CI/CD

This project uses GitHub Actions for continuous integration and deployment:

CI Pipeline

  • Test: Runs tests on Go 1.21, 1.22, and 1.23
  • Lint: Code quality checks with golangci-lint
  • Security: Security scanning with Gosec
  • Coverage: Automated coverage reporting to Codecov

Release Pipeline

To create a new release:

  1. Create and push a new tag: git tag -a v1.0.0 -m "Release v1.0.0"
  2. Push the tag: git push origin v1.0.0
  3. GitHub Actions will automatically build and release binaries for:
    • Linux (amd64, arm64)
    • macOS (amd64, arm64)
    • Windows (amd64, arm64)

License

MIT

About

Example to deploy a Go Gin application on Sevalla

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published