dev add geo country service
Test Workflow / test (push) Successful in 3s

This commit is contained in:
vladp
2026-04-15 10:25:25 +07:00
parent 0bb952c4e6
commit c60aeec6f5
3 changed files with 27 additions and 7 deletions
@@ -1,6 +1,7 @@
package ru.soune.nocopy.service.geo;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CountryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
@@ -21,13 +22,9 @@ public class GeoCountryService {
this.dbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
}
public String getCountry(String input) {
public String getCountryName(String input) {
try {
String domain = extractDomain(input);
InetAddress ip = InetAddress.getByName(domain);
CountryResponse response = dbReader.country(ip);
CountryResponse response = extractCountry(input);
return response.getCountry().getName();
} catch (Exception e) {
@@ -36,6 +33,25 @@ public class GeoCountryService {
}
}
public String getCountryCode(String input) {
try {
CountryResponse response = extractCountry(input);
return response.getCountry().getIsoCode();
} catch (Exception e) {
log.error("Failed for input: {}", input, e);
return "UNKNOWN";
}
}
private CountryResponse extractCountry(String input) throws IOException, GeoIp2Exception {
String domain = extractDomain(input);
InetAddress ip = InetAddress.getByName(domain);
return dbReader.country(ip);
}
private String extractDomain(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input is empty");