Skip to content

amainooti/challenge-no-3

Repository files navigation

Weather App - Backend Challenge

This is a weather application built as part of the 20 Backend Challenge. The app provides real-time weather data for various cities using a third-party weather API. The app implements caching using Redis to minimize external API requests and improve response times for frequently queried cities. The backend is developed using NestJS, with caching and HTTP request functionalities integrated.


Table of Contents


Features

  • Fetches current weather data for any city.
  • Caches weather data using Redis to avoid redundant API calls and improve performance.
  • Handles common HTTP errors gracefully.
  • Custom timeout for API requests to handle latency issues.
  • Organized and scalable NestJS structure for easy maintainability.
  • Fully Dockerized environment for Redis and the NestJS app.

Technologies Used

  • NestJS: A progressive Node.js framework for building efficient, scalable server-side applications.
  • Redis: An in-memory key-value store used for caching.
  • Axios: For making HTTP requests to the weather API.
  • RxJS: Used for handling observables from HTTP requests.
  • Docker: For containerizing the application and Redis.
  • RedisInsight/Redis Commander: For visualizing Redis cache (optional).

Installation

Prerequisites

Steps

  1. Clone the repository: ```bash git clone https://github.com/yourusername/weather-app.git cd weather-app ```

  2. Install dependencies: ```bash npm install ```

  3. Start Redis using Docker: ```bash docker run --name redis-weather-app -p 6379:6379 -d redis ```

  4. Set up environment variables (see Environment Variables).

  5. Run the development server: ```bash npm run start:dev ```


Environment Variables

You need to set the following environment variables in a `.env` file:

```bash API_KEY=your_weather_api_key # Your API key for the weather service BASE_URL=https://weather.visualcrossing.com # The base URL for the weather API REDIS_HOST=localhost REDIS_PORT=6379 CACHE_TTL=300 # Time to live for cache in seconds (default is 5 minutes) ```


Usage

Once the app is up and running, you can fetch the weather for any city by hitting the `/weather` endpoint:

Example:

```bash curl http://localhost:3000/weather?city=London ```

The response will either return cached data (if available) or fetch fresh data from the weather API and cache it.


API Endpoints

1. Get Weather Data

  • Endpoint: `/weather?city=<city_name>`

  • Method: `GET`

  • Description: Fetches the weather data for a specified city.

  • Response: ```json { "temperature": 20, "humidity": 78, "condition": "Cloudy", "city": "London" } ```

  • Query Parameters:

    • `city` (required): The name of the city you want to get weather data for.

Caching Mechanism

  • Redis is used to cache the weather data for a specified TTL (default: 300 seconds or 5 minutes).
  • When a city is requested, the app first checks the Redis cache for existing data. If found, it serves the cached data. If not, it fetches the data from the weather API and stores it in Redis.
  • Cache key format: `city-data:<city_name>`

Error Handling

  • 404 Not Found: Returned when the city cannot be found in the external weather API.
  • 400 Bad Request: Returned for invalid API requests (e.g., missing city parameter).
  • 500 Internal Server Error: Returned for unexpected errors or issues with the external API.
  • 408 Request Timeout: Returned if the external API takes too long to respond.

Docker Setup

You can also run the Redis server using Docker Compose for easier setup. Create a `docker-compose.yml` file like this:

```yml version: '3' services: redis: image: redis:latest container_name: redis-weather-app ports: - '6379:6379' ```

To spin up Redis using Docker Compose: ```bash docker-compose up -d ```


Contributing

If you want to contribute to this project:

  1. Fork the repository.
  2. Create a new branch.
  3. Make your changes and commit them.
  4. Push to your branch and create a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Credits

This project idea is taken from roadmap.sh.

Final Notes:

  • This weather app is built for the 20 Backend Challenge and focuses on essential backend functionalities like API integration, caching, and error handling.
  • Feel free to modify, extend, or optimize the code to fit your own project requirements.

Enjoy building and scaling! 🎉 """

About

20 Backend Project Ideas (Weather API) that gets progressively hard

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published