package main

import (
	"errors"
	"fmt"
	"log"
	"net/netip"

	"galaxy/geoip"
)

func main() {
	resolver, err := geoip.Open("/srv/geoip/GeoLite2-Country.mmdb")
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := resolver.Close(); err != nil {
			log.Fatal(err)
		}
	}()

	country, err := resolver.Country(netip.MustParseAddr("81.2.69.160"))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(country)

	fromString, err := resolver.CountryString("203.0.113.10")
	switch {
	case err == nil:
		fmt.Println(fromString)
	case errors.Is(err, geoip.ErrCountryNotFound):
		// Fail open: keep request processing and treat the country as unknown.
	default:
		log.Fatal(err)
	}
}
