A high-performance currency exchange rate service built in Go that provides real-time and historical currency conversion with in-memory caching.
- Real-time currency conversion for USD, INR, EUR, JPY, GBP
- Historical exchange rates (up to 90 days)
- In-memory caching for optimal performance
- Hourly automatic rate refresh
- Thread-safe concurrent request handling
- Comprehensive input validation
- RESTful API design
- Docker support
┌─────────────┐
│ Client │
└──────┬──────┘
│
▼
┌─────────────────┐
│ HTTP Handler │ (Gin Framework)
└────────┬────────┘
│
▼
┌──────────────────┐
│ Rate Fetcher │ (Business Logic)
│ Service │
└────┬───────┬─────┘
│ │
▼ ▼
┌────────┐ ┌──────────┐
│ Cache │ │ API │
│ (RAM) │ │ Client │
└────────┘ └─────┬────┘
│
▼
┌───────────────┐
│ External API │
│(exchangerate- │
│ api.com) │
└───────────────┘
- API Client (
service/api_client.go) - Handles external API calls - Cache (
service/cache.go) - Thread-safe in-memory storage - Converter (
service/converter.go) - Pure currency conversion logic - Rate Fetcher (
service/rate_fetcher.go) - Main orchestrator with validation - Handler (
handler/convert_handler.go) - HTTP request/response handling
- Go 1.21 or higher
- Docker (optional, for containerized deployment)
- API Key from https://exchangerate.host
# 1. Clone the repository
git clone <your-repo-url>
cd exchange-rate-service
# 2. Set your API key
export API_KEY=your_api_key_here
# 3. Run with Docker Compose
docker-compose up
# Service will be available at http://localhost:8080# 1. Clone the repository
git clone <your-repo-url>
cd exchange-rate-service
# 2. Install dependencies
go mod download
# 3. Create .env file
echo "API_KEY=your_api_key_here" > .env
echo "PORT=8080" >> .env
# 4. Run the service
go run main.go
# Service will be available at http://localhost:8080Convert an amount from one currency to another.
Endpoint: GET /convert
Query Parameters:
from(required) - Source currency code (USD, INR, EUR, JPY, GBP)to(required) - Target currency code (USD, INR, EUR, JPY, GBP)amount(required) - Amount to convert (positive number)date(optional) - Historical date in YYYY-MM-DD format (max 90 days ago)
Examples:
# Current exchange rate
curl "http://localhost:8080/convert?from=USD&to=INR&amount=100"
# Response:
{
"from": "USD",
"to": "INR",
"amount": 100,
"result": 8312.50
}
# Historical exchange rate (30 days ago)
curl "http://localhost:8080/convert?from=EUR&to=GBP&amount=50&date=2025-10-28"
# Response:
{
"from": "EUR",
"to": "GBP",
"amount": 50,
"date": "2025-10-28",
"result": 43.95
}
# Same currency conversion
curl "http://localhost:8080/convert?from=USD&to=USD&amount=100"
# Response:
{
"from": "USD",
"to": "USD",
"amount": 100,
"result": 100
}Error Responses:
# Unsupported currency
curl "http://localhost:8080/convert?from=BTC&to=USD&amount=1"
# {"error":"unsupported currency: BTC"}
# Invalid amount
curl "http://localhost:8080/convert?from=USD&to=INR&amount=-100"
# {"error":"amount must be positive"}
# Date too old (>90 days)
curl "http://localhost:8080/convert?from=USD&to=INR&amount=100&date=2024-01-01"
# {"error":"date is too old, maximum lookback is 90 days"}
# Future date
curl "http://localhost:8080/convert?from=USD&to=INR&amount=100&date=2026-01-01"
# {"error":"date cannot be in the future"}
# Missing parameters
curl "http://localhost:8080/convert?from=USD&to=INR"
# {"error":"missing required parameter: amount"}# Set API key
export API_KEY=your_api_key_here
# Run tests
go test -v
# Run with coverage
go test -cover✓ Basic conversion
✓ All 20 currency pairs
✓ Historical conversion
✓ Unsupported currencies rejected
✓ Invalid amounts rejected
✓ Date validation
✓ Caching performance
Environment variables:
API_KEY=your_api_key_here # Required: exchangerate-api.com API key
PORT=8080 # Optional: Server port (default: 8080)- USD - United States Dollar
- INR - Indian Rupee
- EUR - Euro
- JPY - Japanese Yen
- GBP - British Pound Sterling
- Base Currency: All exchange rates are relative to USD from the API
- Rate Refresh: Latest rates are refreshed every hour automatically
- Historical Data: Limited to last 90 days as per requirements
- Caching Strategy:
- Latest rates cached until next hourly refresh
- Historical rates cached permanently (within 90-day window)
- Date Format: Historical dates must be in YYYY-MM-DD format
- Timezone: All dates are processed in UTC
- API Rate Limits: Free tier API limits are respected
- Error Handling: External API failures return graceful error messages
- Concurrency: Service handles multiple concurrent requests safely using mutex locks
- Decimal Precision: Results are returned as floating-point numbers
exchange-rate-service/
├── main.go # Application entry point
├── service/
│ ├── api_client.go # External API integration
│ ├── cache.go # In-memory caching
│ ├── converter.go # Conversion logic
│ └── rate_fetcher.go # Main service orchestrator
├── handler/
│ └── convert_handler.go # HTTP handlers
├── service_test.go # Integration tests
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose configuration
├── .env # Environment variables
├── go.mod # Go module definition
└── README.md # This file