This commit is contained in:
Your Name
2025-05-03 19:56:20 +12:00
parent aafcc4cafe
commit 6c03512637
6 changed files with 32 additions and 39 deletions

View File

@@ -17,6 +17,11 @@ bool load_config(const std::string& config_path, ServerConfig& config) {
nlohmann::json j;
file >> j;
config.host = "0.0.0.0";
config.port = 8123;
config.object_store_path = "/data/storage";
config.write_tokens = {};
// Parse write tokens
if (j.contains("write_tokens")) {
config.write_tokens = j["write_tokens"].get<std::vector<std::string>>();

View File

@@ -11,7 +11,7 @@ struct ServerConfig {
std::vector<std::string> write_tokens;
std::filesystem::path object_store_path;
std::string host = "0.0.0.0";
uint16_t port = 80;
uint16_t port = 0;
};
bool load_config(const std::string& config_path, ServerConfig& config);

View File

@@ -8,34 +8,20 @@ namespace simple_object_storage {
int main(int argc, char* argv[]) {
std::filesystem::path config_path = "/data/sos_config.json";
if (!std::filesystem::exists(config_path))
config_path = std::filesystem::path(std::getenv("HOME")) / ".config/simple_object_storage/config.json";
if (!std::filesystem::exists(config_path))
std::filesystem::create_directories(config_path.parent_path());
std::filesystem::path system_config_path = "/data/sos_config.json";
std::filesystem::path user_config_path = std::filesystem::path(std::getenv("HOME")) / ".config/simple_object_storage/config.json";
std::filesystem::path config_path;
if (std::filesystem::exists(system_config_path)) config_path = system_config_path;
else if (std::filesystem::exists(user_config_path)) config_path = user_config_path;
else {
std::cout << "No config file found. Checked " << system_config_path << " and " << user_config_path << std::endl;
return 1;
}
ServerConfig config;
if (!simple_object_storage::load_config(config_path, config)) {
// output default config using heredoc to config_path
std::ofstream config_file(config_path);
config_file << R"END(
{
"write_tokens": [
"fizzle1",
"fizzle2",
"fizzle3"
],
"object_store_path": "/home/j/.simple_object_storage/",
"host": "0.0.0.0",
"port": 8123
}
)END";
config_file.close();
}
if (!simple_object_storage::load_config(config_path, config)) {
std::cerr << "Failed to load configuration at "<<config_path << std::endl;
std::cout << "Config file at " << config_path << " is not valid." << std::endl;
return 1;
}

View File

@@ -79,14 +79,12 @@ bool Server::validate_write_request(const httplib::Request &req, httplib::Respon
Server::Server(const ServerConfig& config)
: config_(config), running_(false) {
// Ensure object store directory exists
try {
std::filesystem::create_directories(config_.object_store_path);
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Failed to create object store directory: " << config_.object_store_path << " - " << e.what() << std::endl;
if (!std::filesystem::exists(config_.object_store_path)) {
std::cerr << "Object store directory does not exist: " << config_.object_store_path << std::endl;
return;
}
// Initialize the database
if (!init_db()) {
// Error already printed in init_db