A simple REST API for fetching and storing weather data using Go, Gorilla Mux, and MongoDB. This project demonstrates how to interact with an external API (WeatherStack), store data in MongoDB, and serve weather-related endpoints.
- Get Weather by City: Fetch current weather (temperature and humidity) for a specific city from the WeatherStack API.
- Save Weather Data: Store weather data for later retrieval using MongoDB.
- Automatic Cleanup: Old weather data is automatically deleted using MongoDB TTL indexing.
- Rate Limiting: Prevent excessive requests with built-in rate limiting.
- Swagger Integration: View detailed, interactive API documentation.
- Go (v1.23.3)
- Gorilla Mux - A robust HTTP router and URL matcher for building RESTful APIs.
- MongoDB - A NoSQL database for efficient data storage.
- WeatherStack API - An external weather data provider.
- Swagger - For API documentation generation.
- Install Go (version 1.23.3 or later).
- Install MongoDB and ensure it's running locally on port
27017. - Obtain a WeatherStack API key from WeatherStack.
-
Clone the repository:
git clone https://github.com/yourusername/weather-api.git cd weather-api -
Install dependencies:
go mod tidy
-
Set up environment variables: Create a
.envfile in the project root with the following:MONGODB_URI=mongodb://localhost:27017/weatherdb WEATHERSTACK_API_KEY=your_weatherstack_api_key -
Run the application:
go run main.go
The server will be running on
http://localhost:8080.
- Description: Retrieves current weather data for a specified city.
- Path Parameter:
city: Name of the city (required).
- Response:
{ "city": "London", "temperature": 15.5, "humidity": 80, "date": "2024-01-01", "createdAt": "2024-01-01T12:00:00Z" }
- Description: Stores weather data in MongoDB.
- Request Body:
{ "city": "London", "temperature": 15.5, "humidity": 80, "date": "2024-01-01", "createdAt": "2024-01-01T12:00:00Z" } - Response:
{ "message": "Weather data successfully created" }
- Access the interactive Swagger documentation at:
http://localhost:8080/swagger/
Contributions are welcome! Fork this repository, make your changes, and open a pull request. Whether it's fixing bugs, improving documentation, or adding new features, every contribution counts.
This project is open-source and available under the MIT License.