diff --git a/src/config/config_db.h b/src/config/config_db.h index 89819a8..1ed9084 100644 --- a/src/config/config_db.h +++ b/src/config/config_db.h @@ -27,10 +27,10 @@ namespace Config { struct Database { - std::string name; + std::string name{"wormhole"}; /** SQLite only */ - std::string file; - DB::Database::Type type; + std::string file{"wormhole.sqlite"}; + DB::Database::Type type{DB::Database::Type::Unset}; }; void load_database(Config::Database& cfg, YAML::Node& node); diff --git a/src/database/database.h b/src/database/database.h index 8649a96..506309d 100644 --- a/src/database/database.h +++ b/src/database/database.h @@ -30,7 +30,8 @@ namespace DB enum class Type { SQLite, - PostgreSQL + PostgreSQL, + Unset, }; private: }; diff --git a/src/database/sqlite/sqlite.cpp b/src/database/sqlite/sqlite.cpp index 3453b9d..0d5a429 100644 --- a/src/database/sqlite/sqlite.cpp +++ b/src/database/sqlite/sqlite.cpp @@ -16,15 +16,30 @@ * along with this program. If not, see . */ +#include #include #include "sqlite.h" +#include using namespace DB; SQLite::SQLite(std::filesystem::path path) : DB::Database(), path{std::move(path)}, - db(nullptr, SQLiteDeleter()) + db{nullptr} { - + using namespace std::string_literals; + if (sqlite3_open(this->path.c_str(), &db)) + { + const std::string err_msg = "Can't open database: "s + std::string(sqlite3_errmsg(db)); + sqlite3_close(db); + throw std::runtime_error(err_msg); + } + + Logger::instance().log("SQLite3 database opened at "s + this->path.string(), Logger::Level::DEBUG); +} + +SQLite::~SQLite() +{ + sqlite3_close(db); } diff --git a/src/database/sqlite/sqlite.h b/src/database/sqlite/sqlite.h index 8e3a00e..b774b08 100644 --- a/src/database/sqlite/sqlite.h +++ b/src/database/sqlite/sqlite.h @@ -29,10 +29,12 @@ namespace DB { public: explicit SQLite(std::filesystem::path path); - virtual ~SQLite() = default; + virtual ~SQLite(); + + private: std::filesystem::path path; struct SQLiteDeleter { void operator()(sqlite3* db) { sqlite3_close(db); } }; - std::unique_ptr db; + sqlite3* db; }; } diff --git a/src/http/microhttpd_server.cpp b/src/http/microhttpd_server.cpp index 83ba7fd..d2f13fe 100644 --- a/src/http/microhttpd_server.cpp +++ b/src/http/microhttpd_server.cpp @@ -27,6 +27,22 @@ using namespace HTTP; namespace { + HTTP::Request::Type str_to_method(std::string method) + { + if (method == "GET") + return HTTP::Request::Type::GET; + else if (method == "POST") + return HTTP::Request::Type::POST; + else if (method == "PUT") + return HTTP::Request::Type::PUT; + else if (method == "DELETE") + return HTTP::Request::Type::DELETE; + else if (method == "PATCH") + return HTTP::Request::Type::PATCH; + else + return HTTP::Request::Type::UNSUPPORTED; + } + enum MHD_Result new_connection(void* cls, MHD_Connection* conn, const char* url, @@ -41,7 +57,7 @@ namespace enum MHD_Result ret; // Create generic request from args - HTTP::Request req{HTTP::Request::Type::GET, url}; + HTTP::Request req{str_to_method(method), url}; std::optional resp = that->handle_request(req); // TODO Slow to copy each time... Do alternative here. diff --git a/src/http/request.h b/src/http/request.h index 15da616..cd43567 100644 --- a/src/http/request.h +++ b/src/http/request.h @@ -61,6 +61,7 @@ namespace HTTP PUT, DELETE, PATCH, + UNSUPPORTED, }; /** diff --git a/src/main.cpp b/src/main.cpp index b0f668d..80528e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,6 +47,9 @@ int start_wormhole() // Select Database switch (Config::instance().config.database.type) { + case DB::Database::Type::Unset: + Logger::instance().log("Database not set in config.yaml, using SQLite3!", Logger::Level::WARN); + // Fall case DB::Database::Type::SQLite: database = std::make_shared(Config::instance().config.database.file); break; @@ -78,7 +81,8 @@ int start_wormhole() server->start(); - + Logger::instance() << "HTTP server running on port "s + std::to_string(Config::instance().config.http.port); + getchar(); return EXIT_SUCCESS; }