A production-ready backend system for managing coupons in a medicine ordering platform, built with Go.
- Admin Coupon Creation & Management
- Coupon Validation
- Concurrent Request Handling
- Caching with Redis
- PostgreSQL Database
- OpenAPI Documentation
- Docker Support
- Go 1.21+
- Gin Web Framework
- PostgreSQL
- Redis
- GORM
- Docker
- Swagger/OpenAPI
- Go 1.21 or higher
- Docker and Docker Compose
- PostgreSQL
- Redis
-
Clone the repository:
git clone https://github.com/raman20/crazy-coupon.git cd crazy-coupon -
Copy the environment file and update variables:
cp .env.example .env
-
Install dependencies:
go mod download
-
Run with Docker:
docker compose up --build
Or run locally:
go run cmd/main.go
| Variable | Description | Default |
|---|---|---|
| DB_HOST | Database host | localhost |
| DB_PORT | Database port | 5432 |
| DB_USER | Database user | postgres |
| DB_PASSWORD | Database password | postgres |
| DB_NAME | Database name | crazy_coupon |
| DB_SSL_MODE | Database SSL mode | disable |
| REDIS_HOST | Redis host | redis |
| REDIS_PORT | Redis port | 6379 |
| SERVER_PORT | Application port | 8080 |
Once the server is running, access the Swagger documentation at:
http://localhost:8080/swagger/index.html
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/coupons/applicable | Get applicable coupons for a cart |
| POST | /api/v1/coupons/validate | Validate and apply a coupon |
| POST | /api/v1/coupons | Create a new coupon (admin) |
- Models: Defines the data structures for coupons and related entities
- Repository: Handles database operations
- Service: Implements business logic
- Handler: Manages HTTP requests and responses
- Middleware: Handles cross-cutting concerns
- Uses Redis for caching frequently accessed coupons
- Implements mutex locks for concurrent coupon validations
- Database-level safety with PostgreSQL transactions
go test ./...swag init -g cmd/main.goContributions are welcome! Please open issues or submit pull requests for improvements or bug fixes.
For support, open an issue on GitHub or contact the maintainer at [email protected].
MIT License