A modern, high-performance microservice for intelligent route planning with real-time air quality and weather data integration.
- ๐ฃ๏ธ Multi-modal Route Planning - Support for car, scooter, and other transportation modes
- ๐ฌ๏ธ Real-time Air Quality - Live AQI data from WAQI API with PM2.5 predictions
- ๐ค๏ธ Weather Intelligence - Current and forecasted weather conditions
- โก Energy Optimization - Route energy calculation based on vehicle type and conditions
- ๐ฏ Smart Routing - Multiple preferences: fastest, shortest, balanced, low-emission, low-exposure
- ๐ Backward Compatible - Maintains existing API endpoints while adding new features
- ๐ณ Container Ready - Docker support for easy deployment
- ๐ Health Monitoring - Built-in health checks and monitoring endpoints
- PM2.5 Prediction - Machine learning-based air quality forecasting using custom deployed ML models
graph TB
subgraph "๐ Client Layer"
C[Web Client]
M[Mobile App]
API[API Client]
end
subgraph "๐ Clean Route Backend"
subgraph "๐ก API Layer"
H1[Route Handler]
H2[Weather Handler]
H3[AQI Handler]
H4[Prediction Handler]
end
subgraph "โ๏ธ Business Logic"
S1[Route Service]
S2[Weather Service]
S3[AQI Service]
end
subgraph "๐ Data Layer"
M1[Request Models]
M2[Response Models]
M3[External Models]
end
subgraph "๐ง Infrastructure"
CFG[Config Manager]
MW[Middleware]
UTILS[Utilities]
end
end
subgraph "๐ External Services"
MAPBOX[Mapbox API]
GRAPH[GraphHopper API]
WAQI[WAQI API]
OW[OpenWeather API]
ML[Custom ML Models]
end
%% Client connections
C --> H1
C --> H2
C --> H3
C --> H4
M --> H1
M --> H2
M --> H3
M --> H4
API --> H1
API --> H2
API --> H3
API --> H4
%% Internal connections
H1 --> S1
H2 --> S2
H3 --> S3
H4 --> S2
S1 --> M1
S1 --> M2
S2 --> M1
S2 --> M2
S3 --> M1
S3 --> M2
%% External connections
S1 --> MAPBOX
S1 --> GRAPH
S2 --> OW
S3 --> WAQI
S2 --> ML
%% Infrastructure connections
CFG --> S1
CFG --> S2
CFG --> S3
MW --> H1
MW --> H2
MW --> H3
MW --> H4
UTILS --> S1
UTILS --> S2
UTILS --> S3
- Go 1.20+ - Download here
- Docker (optional) - Download here
- direnv (for local development) - Install here
-
Clone and Setup
git clone <repository-url> cd go-backend
-
Environment Configuration
cp .envrc.example .envrc # Edit .envrc with your API keys direnv allow
-
Run the Service
go run main.go
-
Verify it's Working
curl http://localhost:8080/health
# Build the image
docker build -t clean-route-backend .
# Run the container
docker run -p 8080:8080 --env-file .envrc clean-route-backend
http://localhost:8080
Currently, the API uses API keys for external service authentication. Configure these in your environment variables.
POST /route
POST /api/v1/route
Request Body:
{
"source": [12.9716, 77.5946],
"destination": [13.0827, 77.5877],
"delayCode": 0,
"mode": "driving-traffic",
"route_preference": "balanced",
"vehicle_mass": 1500,
"condition": "average",
"engine_type": "petrol"
}
Response:
{
"success": true,
"data": {
"distance": 12500,
"duration": 1800000,
"totalExposure": 45.2,
"totalEnergy": 2.8
}
}
POST /all-routes
POST /api/v1/routes
Returns all route types (fastest, shortest, balanced, low-emission, low-exposure) for the given request.
GET /api/v1/weather?lat=12.9716&lon=77.5946
Response:
{
"success": true,
"data": {
"current": {
"temp": 25.5,
"humidity": 65,
"wind_speed": 5.5,
"wind_direction": 180
},
"hourly": [...]
}
}
GET /api/v1/aqi?lat=12.9716&lon=77.5946
Response:
{
"success": true,
"data": {
"aqi": 45.2
}
}
POST /api/v1/predict/pm25
Request Body:
{
"features": [
{
"ITEMP": 25.5,
"IRH": 65.2,
"IWD": 180.0,
"IWS": 5.5,
"IPM": 45.0,
"FTEMP": 26.0,
"FRH": 63.0,
"FWD": 175.0,
"FWS": 6.0,
"delayCode": 0
}
]
}
Response:
{
"success": true,
"data": {
"predictions": [42.3, 38.7, 41.2]
}
}
GET /health
Response:
{
"status": "healthy",
"service": "clean-route-backend"
}
Variable | Description | Required | Default |
---|---|---|---|
MAPBOX_API_KEY |
Mapbox API key for route planning | โ | - |
GRAPHHOPPER_API_KEY |
GraphHopper API key for alternative routes | โ | - |
WAQI_API_KEY |
WAQI API key for air quality data | โ | - |
OPEN_WEATHER_API_KEY |
OpenWeather API key for weather data | โ | - |
ML_MODEL_ENDPOINT |
Custom ML models endpoint for PM2.5 predictions | โ | - |
RAILWAY |
Set to "true" for Railway deployment | โ | false |
PORT |
Server port | โ | 8080 |
MAPBOX_API_KEY=your_mapbox_key_here
GRAPHHOPPER_API_KEY=your_graphhopper_key_here
WAQI_API_KEY=your_waqi_key_here
OPEN_WEATHER_API_KEY=your_openweather_key_here
ML_MODEL_ENDPOINT=https://your-ml-models-endpoint.com
PORT=8080
go-backend/
โโโ internal/
โ โโโ config/ # Configuration management
โ โโโ handlers/ # HTTP request handlers
โ โโโ middleware/ # HTTP middleware
โ โโโ models/ # Data models and structures
โ โโโ services/ # Business logic services
โ โโโ utils/ # Utility functions
โโโ main.go # Application entry point
โโโ go.mod # Go module file
โโโ go.sum # Go module checksums
โโโ Dockerfile # Docker configuration
โโโ README.md # This file
- Models - Add data structures in
internal/models/
- Services - Implement business logic in
internal/services/
- Handlers - Create HTTP handlers in
internal/handlers/
- Routes - Update routes in
main.go
- Tests - Add tests for new functionality
- Follow Go conventions and best practices
- Use meaningful variable and function names
- Add comments for complex logic
- Handle errors appropriately
- Use consistent formatting (run
go fmt
)
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific test
go test ./internal/services
- Endpoint:
GET /health
- Purpose: Service health monitoring
- Response: Service status and metadata
- Structured logging for debugging
- Error tracking and reporting
- Performance metrics through HTTP status codes
- Request/response times
- Error rates
- API usage statistics
- CORS Configuration - Configured for web client access
- Input Validation - Comprehensive validation on all endpoints
- API Key Management - Secure environment variable handling
- Security Headers - Referrer policy and other security headers
- Rate Limiting - Built-in protection against abuse
# Deploy to Railway
railway up
version: '3.8'
services:
clean-route-backend:
build: .
ports:
- "8080:8080"
env_file:
- .envrc
apiVersion: apps/v1
kind: Deployment
metadata:
name: clean-route-backend
spec:
replicas: 3
selector:
matchLabels:
app: clean-route-backend
template:
metadata:
labels:
app: clean-route-backend
spec:
containers:
- name: clean-route-backend
image: clean-route-backend:latest
ports:
- containerPort: 8080
env:
- name: MAPBOX_API_KEY
valueFrom:
secretKeyRef:
name: api-keys
key: mapbox-key
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Mapbox - Route planning and directions
- GraphHopper - Alternative routing and energy calculations
- WAQI - Air quality data
- OpenWeather - Weather data and forecasts
- Custom ML Models - Machine learning predictions
- Issues: GitHub Issues
- Documentation: API Docs
- Mapbox Directions API - Primary route planning for cars
- GraphHopper API - Alternative routes and energy calculations
- WAQI API - Air quality data
- OpenWeather API - Weather data
- Custom ML Models - PM2.5 prediction model