44 lines
1.2 KiB
C++
44 lines
1.2 KiB
C++
#include <fstream>
|
|
#include "json.hpp"
|
|
#include "json_settings.h"
|
|
#include "utils.hpp"
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
using json = nlohmann::json;
|
|
using namespace::AppSettings;
|
|
|
|
Settings AppSettings::deafult(){
|
|
return Settings {
|
|
.http_port = 3010,
|
|
.db_path = "/var/lib/shadowrun-server/shadowrun.db",
|
|
.domain = "*"
|
|
};
|
|
}
|
|
|
|
Settings AppSettings::load() {
|
|
ifstream file(settingsFile);
|
|
if (!file.is_open()) {
|
|
std::cerr << "Failed to load settings file" << settingsFile << " Loading default settings" << std::endl;
|
|
return AppSettings::deafult();
|
|
}
|
|
|
|
std::stringstream buffer;
|
|
buffer << file.rdbuf(); // Read the whole file into the stringstream
|
|
std::string fileContents = buffer.str(); // Convert to std::string
|
|
auto result = utils::parseJson(fileContents);
|
|
|
|
if(!result){
|
|
std::cerr << "failed to parse settings file, Loading default settings" << std::endl;
|
|
return AppSettings::deafult();
|
|
}
|
|
|
|
try {
|
|
return result.value().get<Settings>();
|
|
} catch (...) {
|
|
std::cerr << "failed to parse settings file, Loading default settings";
|
|
return AppSettings::deafult();
|
|
}
|
|
}
|
|
|