A simple weather API written in Go that fetches weather information from Visual Crossing and caches results in Redis.
Project reference: https://roadmap.sh/projects/weather-api-wrapper-service
- HTTP endpoint for weather lookup by city
- Weather provider integration with Visual Crossing
- Redis-backed caching layer
- Configurable server, Redis, and cache settings via environment variables
- Basic request validation and timeout handling
- Go 1.26 or later
- Redis server for caching
The application uses the following environment variables:
SERVER_HOST- server host address, default0.0.0.0SERVER_PORT- server port, default8080REDIS_ADDR- Redis address, defaultlocalhost:6379REDIS_PASSWORD- Redis password, default emptyREDIS_DB- Redis database index, default0REDIS_PROTOCOL- Redis protocol version, default3WEATHER_API_KEY- Visual Crossing weather API key, requiredCACHE_TTL- cache time to live, default30m
- Install Go and start Redis.
- Set environment variables in a
.envfile or in your shell. - Run the application:
cd /home/hazubeep/Documents/golang/weather-api
go run ./cmdThe service listens on http://SERVER_HOST:SERVER_PORT.
Request weather data using the /weather endpoint:
GET /weather?city={city}Example:
curl "http://localhost:8080/weather?city=Bandung"or:
curl "http://localhost:8080/weather?city=Bandung,ID"Run the Go test suite:
go test ./...cmd/main.go- application entry point and server setupinternal/config- configuration loading and validationinternal/client- Visual Crossing API clientinternal/repository- Redis cache implementationinternal/service- weather business logicinternal/handler- HTTP request handling