rameshsunkara/go-rest-api-example
Production-ready Go REST APIs without the enterprise bloat
๐ฆ Go 1.25 Flight Recorder Integration
- โ Automatic Slow Request Detection - Captures execution traces for requests taking > 500ms
- โ Zero Overhead When Disabled - Production-safe with opt-in configuration
- โ Visual Trace Analysis - Use `go tool trace` to debug performance bottlenecks
- โ Smart Buffer Management - Rolling 1 MiB buffer with 1-second retention window
๐ฆ Go 1.25 Upgrade
- Upgraded from Go 1.24 โ Go 1.25
- Updated all dependencies to latest stable versions
- Enhanced CI/CD pipeline for Go 1.25 support
๐ฆ Enhanced Developer Experience
- ```bash
- make trace TRACE_FILE=./traces/slow-request-GET-orders-1234567890.trace
- ```
- ๐จ Enhanced README with emojis and visual improvements
- ๐ Collapsible sections for better readability
- ๐ Added `.env.example` template
- New `enableTracing` environment variable
โจ New Components
- | Component | Description |
- |-----------|-------------|
- | `pkg/flightrecorder` | Flight recorder package with clean API |
- | `.env.example` | Environment configuration template |
- | `make trace` | Trace analysis command |
โจ New Environment Variables
- ```bash
- enableTracing=true # Default: false
- ```
๐ฆ Trace Configuration
- Threshold: 500ms (requests slower than this are traced)
- Buffer Size: 1 MiB (rolling buffer)
- Retention: 1 second (minimum trace age)
- Output Directory: `./traces/`
โก ๐ Performance Impact
- | Mode | Memory Overhead | CPU Overhead | Notes |
- |------|----------------|--------------|-------|
- | Disabled | 0 bytes | 0% | Default, production-safe |
- | Enabled | ~1 MiB | ~1-2% | Rolling buffer, minimal impact |
- | Capturing | +file size | Negligible | Non-blocking write |
๐ฆ Quick Start
- ```bash
- git pull origin main
- go mod tidy
- cp .env.example .env
- echo "enableTracing=true" >> .env
- make start
- ```
๐ฆ Analyzing Traces
- ```bash
- ls -lhtr ./traces/
- make trace TRACE_FILE=./traces/slow-request-GET-orders-1762824976.trace
- ```
๐ฆ Upgrading from v1.x
- This release is backward compatible with no breaking changes.
- ```bash
- git pull origin main
- go mod tidy
- make start # Works immediately!
- ```
- ```bash
- enableTracing=true
- + 1 more
๐ Docker Users
- ```bash
- docker-compose down
- docker-compose build
- docker-compose up -d
- ```
๐ฆ CI/CD
- โ Updated to Go 1.25
- โ golangci-lint v2.6.2 (latest stable)
๐ ๐ Security
- No security vulnerabilities introduced
- Flight recorder only active when explicitly enabled
- Trace files contain execution data (review before sharing)
- Added `.env` to `.gitignore` (prevents credential leaks)
๐ฆ ๐ Learning Resources
- [Go 1.25 Flight Recorder Blog](https://go.dev/blog/flight-recorder)
- [Go Tool Trace Documentation](https://pkg.go.dev/cmd/trace)
- [Project README](./README.md)
๐ฆ ๐ฌ Feedback
- I love to hear your feedback!
- ๐ Found a bug? [Open an issue](https://github.com/rameshsunkara/go-rest-api-example/issues)
- ๐ก Have an idea? [Start a discussion](https://github.com/rameshsunkara/go-rest-api-example/discussions)
- โญ Like this release? Give us a star!
- ---
๐ฆ ๐ง **MongoDB Connection Architecture Redesign**
- Complete refactor of MongoDB connection management from `internal/db` to dedicated `pkg/mongodb` package
- New modular connection system with support for:
- MongoDB and MongoDB+SRV connection schemes
- Configurable options using functional pattern (replica sets, read preferences, write concerns)
- Credential management via sidecar files
- Connection URL masking for security
- Comprehensive connection validation and error handling
๐ฆ ๐ฆ **Package Restructuring & Clean Architecture**
- Migrated logger from `internal/logger` to `pkg/logger` for better reusability
- Extracted configuration management to dedicated `internal/config` package
- Improved separation of concerns with cleaner package boundaries
๐ฆ ๐๏ธ **Server & Application Improvements**
- Enhanced graceful shutdown with proper context cancellation handling
- Improved server lifecycle management with better error handling
- Streamlined main application logic with cleaner separation of setup phases
- Better configuration loading and validation
๐ฆ ๐ **Dependency Updates**
- MongoDB Driver 1.17.4 (latest stable release)
- Gin Framework 1.11.0 (latest version with security fixes)
- Updated all dependencies to latest stable versions for security and performance
โจ **New Packages & Components**
- ```
- pkg/mongodb/ # MongoDB connection management
- โโโ connection.go # Connection manager implementation
- โโโ options.go # Connection options and functional patterns
- โโโ types.go # Type definitions and credentials
- โโโ url.go # Connection URL building and validation
- pkg/logger/ # Structured logging with zerolog
- โโโ logger.go # Logger interface and implementation
- + 5 more
๐๏ธ **Removed/Refactored Components**
- โ `internal/db/connection.go` โ โ `pkg/mongodb/connection.go`
- โ `internal/db/connectionURL.go` โ โ `pkg/mongodb/url.go`
- โ `internal/logger/` โ โ `pkg/logger/`
- โ `internal/util/` โ โ `internal/utilities/`
๐ ๐ **Security Enhancements**
- Input validation for all MongoDB connection parameters
- Updated dependencies with latest security patches
๐ฆ ๐ **DevOps & CI/CD Improvements**
- Enhanced Makefile with comprehensive test coverage reporting
- Improved Docker configuration for local development
- VS Code workspace settings for consistent development experience
๐ ๐ **Bug Fixes & Stability**
- Improved error handling with proper error wrapping and context
- Enhanced graceful shutdown preventing resource leaks
๐ฅ ๐ **Breaking Changes**
- โ ๏ธ API Changes (internal packages only - no public API changes):
- MongoDB connection interfaces moved from `internal/db` to `pkg/mongodb`
- Logger interface relocated to `pkg/logger`
- Configuration structure updated in `internal/config`
๐ฆ ๐ฏ **Migration Guide**
- For developers working with the codebase:
- 1. Import path updates for MongoDB connections:
- ```go
- // Old
- import "github.com/rameshsunkara/go-rest-api-example/internal/db"
- // New
- import "github.com/rameshsunkara/go-rest-api-example/pkg/mongodb"
- ```
- + 7 more
โจ In addition to test changes
- Injects logger to all appropriate functions
- main.go is now modular and readable
- Removed deferrun dep which was dealing with signals to cleanup
๐ Changes in this release:
- 1. Upgraded Go version to 1.24: Updated the project to use the latest stable version of Go, ensuring better performance, security, and access to new language features.
- 2. Upgraded golangci-lint to v2: Integrated the latest version of golangci-lint (v2), improving static code analysis and linting capabilities to catch potential issues early.
- 3. Optimized Dockerfile with BuildKit: The Dockerfile now leverages Docker BuildKit for more efficient builds, including enhanced caching and faster layer processing.
- 4. Parallelized Build Workflow: The CI build pipeline has been updated to run tests and Docker builds in parallel, reducing build time and improving overall workflow efficiency.
- This release focuses on performance improvements, build optimizations, and code quality enhancements.
- Full Changelog: https://github.com/rameshsunkara/go-rest-api-example/compare/v1.0.1...v1.1.0
๐ Changes
- Updated Makefile to improve readability and maintainability
- Got rid of in memory mongodb usage for db test and used mtest from offical mongodb.
- Tests are now executed in parallel
- Updated redme to improve readbility
- Increased code coverage
โจ API Features:
- 1. OWASP Compliant [Open API 3 Spec](./OpenApi-v1.yaml)
- 2. Middleware for
- Logging : Helps in debugging and monitoring
- Authentication : Placeholder for different authentication mechanisms
- Tracing by Request ID : Helps in debugging
- Panic Recovery : Helps in keeping the service up
- Common Security Headers : Keeps the service secure
- Query Params Validation : Helps in keeping the service secure
- + 6 more
โจ Go Application Features:
- 1. Configuration Management through Environment Variables
- 2. A Makefile to do all common tasks
- 3. A Git Action to build, run tests, generate code coverage
- 4. Integrated GO Formatter and Linter
- 5. Mechanism to load secrets from Sidecar
- 6. Enables connecting to multiple databases
- 7. Follows the best practices for connecting to MongoDB
- 8. Good mocking practises for Unit test patterns
- + 1 more
