A simple HTTP server built with Go and the Gin framework, demonstrating graceful shutdown, middleware, and clean architecture.
- β¨ 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
- Go 1.21 or higher
git clone <repository-url>
cd go-demo
go mod downloadUsing Make (recommended):
make runOr for development (no binary build):
make devOr manually:
go run .The server will start on http://localhost:8080
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 toolsReturns a simple greeting message.
curl http://localhost:8080/Health check endpoint returning JSON status.
curl http://localhost:8080/healthResponse:
{"status":"ok"}Returns build version information.
curl http://localhost:8080/versionResponse:
{"version":"dev","commit":"","built":""}The Makefile automatically injects build information:
make buildOr build for all platforms:
make build-allBefore pushing, you can run the same checks that CI runs:
make ciThis will:
- Verify dependencies
- Run linter
- Run tests with race detection
- Run security scanner
make install-toolsThis installs:
- golangci-lint (linting)
- gosec (security scanning)
- goreleaser (release testing)
.
βββ 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 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)
The server handles SIGINT and SIGTERM signals for graceful shutdown, allowing in-flight requests to complete before stopping.
This project uses GitHub Actions for continuous integration and deployment:
- 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
To create a new release:
- Create and push a new tag:
git tag -a v1.0.0 -m "Release v1.0.0" - Push the tag:
git push origin v1.0.0 - GitHub Actions will automatically build and release binaries for:
- Linux (amd64, arm64)
- macOS (amd64, arm64)
- Windows (amd64, arm64)
MIT