Skip to content

ljufa/geoip

Repository files navigation

GeoIP & Weather Service

A fast, lightweight web service written in Rust (using Axum) that provides Geolocation, Weather, and Timezone/Datetime information based on IP addresses.

It uses MaxMind DB for local IP geolocation and the OpenWeather API for weather data.

Features

  • IP Geolocation: Get detailed geographic information (City, Country, Coordinates, Timezone) for a given IP address or the caller's IP.
  • Weather Data: Current weather conditions based on the geolocation of the IP address.
  • Weather Forecast: Weather forecast based on the geolocation of the IP address.
  • Datetime Information: Current local time and timezone details for the IP address.
  • Caching: Implements in-memory caching (using moka) for weather and forecast data to minimize external API calls.
  • Docker Ready: Includes a Dockerfile and docker-compose.yaml for easy deployment.

Prerequisites

  1. MaxMind GeoLite2 Database: You need a MaxMind City database file (city.mmdb). You can download a free GeoLite2 City database from MaxMind.
  2. OpenWeather API Key: Sign up at OpenWeather to get a free API key for weather data.
  3. Rust & Cargo (for local development): If you wish to build from source.
  4. Docker & Docker Compose (for containerized deployment): Recommended for running the service.

Setup & Configuration

  1. Clone the repository:

    git clone <repository-url>
    cd geoip
  2. Environment Variables: Copy the example .env file and fill in your secrets.

    cp .env.example .env

    Edit .env and set:

    • OPENWEATHER_API_KEY: Your OpenWeather API Key.
  3. MaxMind Database: Place your city.mmdb file in the root of the project or update the path in docker-compose.yaml to point to its location. By default, the docker-compose.yaml expects it at /city.mmdb inside the container, which you might want to mount as a volume.

Running with Docker Compose

The easiest way to run the service is using Docker Compose.

You can use the prebuilt image published to Docker Hub, or build it locally.

Using the Prebuilt Image

Create a docker-compose.yaml file:

version: "3"
services:
  geoip:
    image: ljufa/geoip:latest
    container_name: geoip
    restart: unless-stopped
    ports:
      - "8080:8080"
    environment:
      - SERVER_BIND_ADDRESS=0.0.0.0:8080
      - DB_FILE_PATH=/city.mmdb
      - RUST_LOG=geoip=info,tower_http=info,hyper=info
      - OPENWEATHER_API_KEY=${OPENWEATHER_API_KEY}
    volumes:
      - ./city.mmdb:/city.mmdb:ro

Run it using:

docker-compose up -d

Building Locally

If you have the source code, you can build and run it using the provided docker-compose.yaml:

docker-compose up -d --build

This will build the Docker image and start the geoip service, exposing it on port 8080.

API Endpoints

  • GET /ip or GET /ip/:ip Returns geolocation data for the caller's IP or the specified IP address.
  • GET /weather or GET /weather/:ip Returns current weather data for the location of the caller's IP or the specified IP address.
  • GET /weather/forecast or GET /weather/forecast/:ip Returns the weather forecast for the location of the caller's IP or the specified IP address.
  • GET /datetime or GET /datetime/:ip Returns the current date and time for the timezone associated with the caller's IP or the specified IP address.

Development

To run the project locally without Docker:

export DB_FILE_PATH=./data/city.mmdb # Path to your MaxMind DB
export OPENWEATHER_API_KEY=your_api_key
export SERVER_BIND_ADDRESS=0.0.0.0:8080
cargo run

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors