A production-ready web service template using Gin web framework and Uber's Dig for dependency injection, featuring automated module registration and scalable architecture.
- Modular Architecture: Domain-driven design with isolated modules (users, products, etc.)
- Dependency Injection: Powered by Uber's Dig for clean dependency management
- Code Generation: Auto-registration of modules via build-time code generation
- Gin Framework: High-performance HTTP routing and middleware support
- Configuration Management: Environment-aware configuration loading
- Route Groups: Versioned API endpoints with middleware support
- Go 1.21+
- Go modules enabled
digandginpackages:go get go.uber.org/dig go get github.com/gin-gonic/gin
-
Clone the repository:
git clone https://github.com/fdddf/digin.git myapp cd myapp -
Install dependencies:
go mod download
-
Generate module registration code:
go generate ./...
-
Build and run:
go build -o myapp ./cmd/server/ ./myapp
myapp/
├── cmd/
│ └── server/ # Main application entrypoint
├── internal/
│ ├── config/ # Configuration management
│ ├── core/ # DI container setup
│ ├── modules/ # Domain modules (users, products, etc.)
│ └── gen/ # Auto-generated code (do not edit manually)
├── scripts/ # Code generation utilities
├── go.mod
└── go.sum
The project uses automatic module registration via code generation:
-
Module Convention:
- Each domain package contains a
module.gofile - Implements
Register(*dig.Container)function - Located in
internal/modules/<domain>/
- Each domain package contains a
-
Generate Code:
# Manual generation go run scripts/generate_modules.go # Using go generate go generate ./...
-
Example Module (
internal/modules/user/module.go):package user import "go.uber.org/dig" func Register(container *dig.Container) { container.Provide(NewService) container.Provide(NewHandler) container.Provide(NewUserRoutes, dig.Group("routes")) }
# Get user by ID
curl http://localhost:8080/api/v1/users/123
# Create product
curl -X POST http://localhost:8080/api/v1/products \
-H "Content-Type: application/json" \
-d '{"name":"New Product", "price":99.99}'Run the test suite:
go test -v ./...- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
MIT License - see LICENSE for details
Acknowledgments