1) First draft of the TUI functionality
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
This commit is contained in:
@@ -1,43 +1,16 @@
|
||||
#include "ip_to_geo.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
||||
#include "ip_to_geo.hpp"
|
||||
#include "../third_party/include/GeoLite2PP.hpp"
|
||||
|
||||
void load_env_file(const std::string& env_path) {
|
||||
std::ifstream file(env_path);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Warning: Could not open " << env_path << " file." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
// Skip empty lines or comments
|
||||
if (line.empty() || line[0] == '#') continue;
|
||||
|
||||
std::size_t delimiter_pos = line.find('=');
|
||||
if (delimiter_pos != std::string::npos) {
|
||||
std::string key = line.substr(0, delimiter_pos);
|
||||
std::string value = line.substr(delimiter_pos + 1);
|
||||
|
||||
// Set the variable globally in the execution environment
|
||||
setenv(key.c_str(), value.c_str(), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loc_data iplookup(const std::string& ip) {
|
||||
|
||||
// 1. Load variables from the local .env file
|
||||
load_env_file();
|
||||
|
||||
// 2. Fetch the path out of the environment variables
|
||||
const char* env_db_path = std::getenv("DB_PATH");
|
||||
// Fallback to a default path if the environment variable wasn't set
|
||||
std::string db_path = (env_db_path != nullptr) ? env_db_path : "data/GeoLite2-City.mmdb";
|
||||
|
||||
|
||||
@@ -53,7 +26,7 @@ loc_data iplookup(const std::string& ip) {
|
||||
location.longitude = geo_data["longitude"];
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Database failed to load: " << e.what() << std::endl;
|
||||
//std::cerr << "Database failed to load: " << e.what() << std::endl;
|
||||
}
|
||||
return location;
|
||||
};
|
||||
Reference in New Issue
Block a user