2) Added env_reader functions (env.hpp and env.cpp) 3) Program looks for a .env on startup and creates one based on user input if not found. 4) Refactored log_parsing and ip_to_geo to use the global env variables for parsing and ip lookup from the local .mmdb database 5) CMakeLists.txt is now cross platform functional 6) Added various cross platform checks for creating .env variables
32 lines
916 B
C++
32 lines
916 B
C++
|
|
#include <exception>
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <map>
|
|
#include <string>
|
|
|
|
|
|
#include "ip_to_geo.hpp"
|
|
#include "../third_party/include/GeoLite2PP.hpp"
|
|
|
|
loc_data iplookup(const std::string& ip) {
|
|
const char* env_db_path = std::getenv("DB_PATH");
|
|
std::string db_path = (env_db_path != nullptr) ? env_db_path : "data/GeoLite2-City.mmdb";
|
|
|
|
|
|
loc_data location;
|
|
|
|
try {
|
|
GeoLite2PP::DB db(db_path);
|
|
std::map<std::string, std::string> geo_data = db.get_all_fields(ip, "en");
|
|
location.country = geo_data["country"];
|
|
location.subdivision = geo_data["subdivision"];
|
|
location.city = geo_data["city"];
|
|
location.latitutde = geo_data["latitude"];
|
|
location.longitude = geo_data["longitude"];
|
|
}
|
|
catch (const std::exception& e) {
|
|
//std::cerr << "Database failed to load: " << e.what() << std::endl;
|
|
}
|
|
return location;
|
|
}; |