weather-api

repository·master·Indexed 19 days ago

https://github.com/robertoduessmann/weather-api

A RESTful API written in Go that provides current weather information and short-term forecasts for specific cities. It features a CacheClient for data storage with TTL-based expiration and provides endpoints such as /weather/{city} (v1) and /v2/weather/{city} for unit-customized weather data.

Tokens
1K
Snippets
7
Records
8
Agent score
65%

What's inside weather-api

  1. Build and run weather-api locally

    master

    To run the weather-api service on your local machine (optimized for Mac users), use the Go toolchain to build the binary and then execute it.

    1. Build the project using go build.
    2. Run the resulting binary with ./weather-api.
    go build
    ./weather-api
  2. Example weather API response

    master

    When requesting weather for a city like Curitiba, the API returns a JSON payload structured as follows:

    {
      "temperature": "29 °C",
      "wind": "20 km/h",
      "description": "Partly cloudy",
      "forecast": [
        {
          "day": "1",
          "temperature": "27 °C",
          "wind": "12 km/h"
        },
        {
          "day": "2",
          "temperature": "22 °C",
          "wind": "8 km/h"
        }
      ]
    }
  3. Get current weather for a city

    master

    The API provides a single endpoint to retrieve current weather data and a short-term forecast for a specific city. Use a GET request to the /weather/{city} path.

    Endpoint: http://localhost:3000/weather/{city}

    Response Format: The API returns a JSON object containing:

    • temperature: Current temperature string (e.g., "29 °C")
    • wind: Current wind speed string (e.g., "20 km/h")
    • description: Weather condition description (e.g., "Partly cloudy")
    • forecast: An array of objects containing day, temperature, and wind for upcoming days.
    curl http://localhost:3000/weather/Curitiba
  4. API Routes and Endpoints

    master

    The weather-api provides weather information through several HTTP GET endpoints. The API is versioned, with v2 supporting query parameters for units.

    Endpoints

    Current Weather (v1)

    • Path: /weather/{city}
    • Method: GET
    • Description: Retrieves current weather for the specified city.

    Current Weather (v2)

    • Path: /v2/weather/{city}
    • Method: GET
    • Query Parameters:
      • unit: Specifies the unit of measurement (e.g., metric or imperial).
    • Description: Retrieves current weather for the specified city with unit customization.
  5. Initialize a new CacheClient

    master

    Use NewCacheClient(ttl time.Duration) to create a new instance of a CacheClient. The ttl (Time To Live) parameter defines the duration for which items remain valid in the cache. The client automatically runs a background goroutine that cleans up expired items every 10 seconds.

    import (
    	"time"
    	"your-project/cache"
    )
    
    // Create a client where items expire after 5 minutes
    client := cache.NewCacheClient(5 * time.Minute)
  6. Use CacheClient.Get to retrieve values

    master

    The Get(key string) (any, bool) method retrieves a value from the cache. It returns the value and a boolean indicating if the key was found and has not expired. If the item is expired, it returns nil, false even if the key exists in the map.

    value, found := client.Get("my-key")
    if found {
    	fmt.Println("Found value:", value)
    } else {
    	fmt.Println("Key not found or expired")
    }
  7. Use CacheClient.Put to store values

    master

    The Put(key string, value any) bool method stores a value in the cache associated with the provided key. The expiration time for the item is calculated as time.Now().Add(ttl), where ttl is the duration configured when the client was initialized. It returns true upon successful insertion.

    success := client.Put("weather_data", someDataStruct)
    if success {
    	fmt.Println("Data cached successfully")
    }