Go Learning Project
REST API implementation with rate limiting, caching, and external API integration.
Project Structure
.
├── cache/ # Cache abstractions
├── constants/ # Application constants
├── db/ # Database related code
│ ├── migrations/ # SQL migration files
│ ├── postgres/ # PostgreSQL client and configurations
│ ├── redis/ # Redis client and configurations
│ └── scripts/ # Database management scripts
├── handlers/
│ ├── fakestore/ # Fake store API handlers
│ └── financial/ # Financial calculation handlers
├── httpclient/ # HTTP client wrapper
├── middlewares/
│ └── ratelimiter/
├── repos/ # Repository layer
│ └── fakestore/ # Fake store API integration
├── services/ # Business logic
│ ├── fakestore/
│ └── financial/
├── types/ # Shared types
└── utils/ # Utility functions
Features
- PostgreSQL Integration: Robust database layer with connection pooling and migrations
- Redis Caching: High-performance caching layer for data access and rate limiter
- Financial calculations with rate limiting
- Fake Store API integration with Redis caching
- Clean architecture with DI
- Concurrent API requests
- Unit tests and coverage reporting
- Docker Support: Containerized development environment
Technical Stack
- Golang 1.23.4
- Gin web framework
- Redis 7
- PostgreSQL 15
- Environment variable configuration
Architecture
- Handler → Service → Repository pattern
- Interface-based dependency injection
- Redis for caching and rate limiting
- Concurrent operations with goroutines
- Error handling abstractions
Setup
Can either choose Mac or Docker
With Mac:
-
Install dependencies:
$ go mod download
-
Set up environment variables in .env:
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=""
REDIS_DB=0
DB_HOST=postgres
DB_MAX_POOL_CONS=200
DB_PORT=5432
DB_USER=your_username
DB_NAME=your_db_name
DB_PASSWORD=your_db_password
- Run the application:
$ go run main.go or $ air for hot reload
With Docker:
- Go to
/development-container folder
- Run the whole application:
docker compose up --build
(This include air command, so hot-reload supported)
Testing
-
Run all tests only:
$ go test ./...
-
Run all tests with coverage result:
$ go test -coverprofile=coverage.out ./... (get the coverage file)
or
$ go test -v -cover ./...
-
Check the test coverage on browser after run $ go test -coverprofile=coverage.out ./...:
$ go tool cover -html=coverage.out
Test rate limiting:
$ chmod +x shellscripts/ratelimiterchecker.sh
or
$ . ./shellscripts/ratelimiterchecker.sh
Database Migration
go to /db folder and $. ./scripts/migrate.sh up
API Endpoints
Financial
POST /calculate: Financial calculations
- Rate limit: 100 req/5min
- Payload:
{"revenue": 100, "expenses": 50, "taxRate": 0.3}
Fake Store
Learning Goals
- Golang syntax and patterns
- Clean code architecture
- Middleware implementation
- Redis integration
- Postgres integration
- Rate limiting concepts
- Unit testing in Go
- Shell scripting
License
See LICENSE file.