An API to get temperature information from a Brazilian postal code (CEP).
- Go 1.21
- Gin Framework
- Docker
- Google Cloud Run
- Docker
- Docker Compose
- Go 1.21 or higher (for local development)
- WeatherAPI account (https://www.weatherapi.com/)
- Clone the repository:
git clone https://github.com/AndreCDiniz/Weather-CEP.git
cd Weather-CEP
- Set up environment variables:
cp .env.example .env
Edit the .env
file and add your WeatherAPI key.
- With Docker:
docker-compose up --build
- Without Docker:
go mod download
go run cmd/api/main.go
Run the tests:
go test ./... -v
Returns the current temperature for the location of the provided postal code.
Parameters:
- cep: Valid 8-digit Brazilian postal code
Live API URL:
https://weather-app-374566725681.us-central1.run.app/weather/{cep}
Example Request:
https://weather-app-374566725681.us-central1.run.app/weather/01001000
Responses:
- 200: Success
{
"temp_C": 28.5,
"temp_F": 83.3,
"temp_K": 301.65
}
- 422: Invalid postal code
- 404: Postal code not found
- 500: Internal error
The application is deployed and available at:
https://weather-app-374566725681.us-central1.run.app
weather-cep/
├── cmd/ # Application entrypoints
│ └── api/
│ └── main.go # Main application file
├── internal/ # Private application code
│ ├── domain/ # Domain models and interfaces
│ │ └── models/
│ ├── handlers/ # HTTP request handlers
│ ├── services/ # Business logic
│ └── clients/ # External service clients
└── pkg/ # Public libraries
└── utils/ # Utility functions
The project follows a clean architecture approach with the following layers:
- Handlers: HTTP request handling and response formatting
- Services: Business logic and orchestration
- Clients: External API communication
- Models: Data structures and domain objects
- Create necessary models in
internal/domain/models
- Implement business logic in
internal/services
- Add HTTP handlers in
internal/handlers
- Update tests accordingly
# Run all tests
go test ./... -v
# Run tests with coverage
go test ./... -cover -coverprofile=coverage.out
# View coverage in browser
go tool cover -html=coverage.out
{
"temp_C": 25.0,
"temp_F": 77.0,
"temp_K": 298.15
}
Invalid postal code:
{
"message": "invalid zipcode"
}
Postal code not found:
{
"message": "can not find zipcode"
}
- Install Google Cloud CLI
- Configure the project:
gcloud init
gcloud auth configure-docker
- Build and push the image:
gcloud builds submit --tag gcr.io/sunlit-unison-447723-u8/weather-app
- Deploy to Cloud Run:
gcloud run deploy weather-app --image gcr.io/sunlit-unison-447723-u8/weather-app --platform managed --region us-central1 --allow-unauthenticated --project sunlit-unison-447723-u8
- 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.