From 0f1237e6b8aa6680b5a8804e4e1a45b4f883a270 Mon Sep 17 00:00:00 2001 From: nekobit Date: Wed, 19 Oct 2022 03:17:40 +0000 Subject: [PATCH] Separate database config FossilOrigin-Name: d732158661861245565d81c0fdf391b3228ff4e5acdb1acd4c34d09d64f161e1 --- CMakeLists.txt | 1 + src/control.cpp | 41 +++++++++++++++++++++----- src/control.h | 1 - src/database/database.h | 3 ++ src/database/database_config.cpp | 48 +++++++++++++++++++++++++++++++ src/main.cpp | 49 ++++++++------------------------ 6 files changed, 98 insertions(+), 45 deletions(-) create mode 100644 src/database/database_config.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b4e84b0..5b55c5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ add_executable(wormhole src/main.cpp) add_library(wormhole_lib src/jsonhelper.cpp src/database/database.cpp + src/database/database_config.cpp src/database/sqlite/sqlite.cpp src/instance/instance.cpp src/http/httpserver.cpp diff --git a/src/control.cpp b/src/control.cpp index 5f8c400..ba7b603 100644 --- a/src/control.cpp +++ b/src/control.cpp @@ -16,13 +16,17 @@ * along with this program. If not, see . */ +#include #include #include #include #include #include #include +#include #include "control.h" +#include "database/database.h" +#include "user.h" using CTLFunction_t = std::function; @@ -34,17 +38,40 @@ struct CTLOptions }; namespace CTL { - int create_user(ArgvVector_t) + int create_user(ArgvVector_t args) + { + int option_idx; + auto db = DB::load_db_from_cfg(); + + + option long_options[] = { + { "name", required_argument, 0, 'n' }, + { "email", no_argument, 0, 'e' }, + { "nickname", no_argument, 0, 'N' }, + { 0, 0, 0, 0 } + }; + + int rc = getopt_long(args.size(), args.data(), "abc:d:012", + long_options, &option_idx); + + User props; + props.email = ""; + props.local = true; + props.acct = "test"; + props.birthday = 0; + props.display_name = "nekocels stay losing"; + + db->create_user(props); + + return 0; + } + + int delete_user(ArgvVector_t args) { return 0; } - int delete_user(ArgvVector_t) - { - return 0; - } - - int luser_local(ArgvVector_t) + int luser_local(ArgvVector_t args) { return 0; } diff --git a/src/control.h b/src/control.h index cf32b10..714d44b 100644 --- a/src/control.h +++ b/src/control.h @@ -20,7 +20,6 @@ #include #include -#include /* Control mode (aka. ctl mode) is a parameter controlled * mode where the user can configure, create users, manipulate options, diff --git a/src/database/database.h b/src/database/database.h index d298d6b..e0162f2 100644 --- a/src/database/database.h +++ b/src/database/database.h @@ -19,6 +19,7 @@ #pragma once #include +#include #include "user.h" namespace DB @@ -51,4 +52,6 @@ namespace DB }; private: }; + + std::shared_ptr load_db_from_cfg(); } diff --git a/src/database/database_config.cpp b/src/database/database_config.cpp new file mode 100644 index 0000000..061e661 --- /dev/null +++ b/src/database/database_config.cpp @@ -0,0 +1,48 @@ +/* + * Wormhole - Federated social network + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include "database.h" +#include "sqlite/sqlite.h" +#include "config/config_db.h" +#include "config/config_loader.h" +#include "logger.h" + +std::shared_ptr DB::load_db_from_cfg() +{ + std::shared_ptr database = nullptr; + + // 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; + case DB::Database::Type::PostgreSQL: + Logger::instance() << "Not implemented!"; + return nullptr; + default: + Logger::instance() << "Unsupported Database!"; + return nullptr; + } + + return database; +} diff --git a/src/main.cpp b/src/main.cpp index eadea45..00de7e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,36 +60,10 @@ int start_wormhole() Logger::instance() << "Loading config..."; - std::shared_ptr database = nullptr; - - try - { - Config::instance().load_config(CONFIG_DIR "/config.yaml"); - } - catch (const YAML::BadFile& err) - { - return EXIT_FAILURE; - } - - // 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; - case DB::Database::Type::PostgreSQL: - Logger::instance() << "Not implemented!"; - return EXIT_FAILURE; - default: - Logger::instance() << "Unsupported Database!"; - return EXIT_FAILURE; - } + std::shared_ptr database = DB::load_db_from_cfg(); // Passed as std::any to each route function - RouteArgs args{database.get(), }; + RouteArgs args{database.get()}; // Start HTTPD Server std::unique_ptr server = @@ -97,15 +71,6 @@ int start_wormhole() database->try_migration(); - // Insert user - // User user{}; - // user.email = ""; - // user.local = true; - // user.acct = "nekocel"; - // user.birthday = 0; - // user.display_name = "nekocels stay losing"; - // database->create_user(user); - init_routes(server); // TODO Move to another file @@ -159,6 +124,16 @@ int main(int argc, char** argv) { int res = EXIT_SUCCESS; + // Initialize config + try + { + Config::instance().load_config(CONFIG_DIR "/config.yaml"); + } + catch (const YAML::BadFile& err) + { + return EXIT_FAILURE; + } + // Start wormhole as is, no arguments needed if (argc <= 1) {