Init database
FossilOrigin-Name: 5a38a1f8591b6f7387937cf36357b1bf10bc5f1e5d10a154c6d4bd87dc40b08e
This commit is contained in:
parent
ecb4162ae4
commit
04f9710a6a
7 changed files with 49 additions and 10 deletions
|
@ -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);
|
||||
|
|
|
@ -30,7 +30,8 @@ namespace DB
|
|||
enum class Type
|
||||
{
|
||||
SQLite,
|
||||
PostgreSQL
|
||||
PostgreSQL,
|
||||
Unset,
|
||||
};
|
||||
private:
|
||||
};
|
||||
|
|
|
@ -16,15 +16,30 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include "sqlite.h"
|
||||
#include <logger.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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<sqlite3, SQLiteDeleter> db;
|
||||
sqlite3* db;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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<HTTP::Response> resp = that->handle_request(req);
|
||||
|
||||
// TODO Slow to copy each time... Do alternative here.
|
||||
|
|
|
@ -61,6 +61,7 @@ namespace HTTP
|
|||
PUT,
|
||||
DELETE,
|
||||
PATCH,
|
||||
UNSUPPORTED,
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<DB::SQLite>(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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue