Separate database config

FossilOrigin-Name: d732158661861245565d81c0fdf391b3228ff4e5acdb1acd4c34d09d64f161e1
This commit is contained in:
nekobit 2022-10-19 03:17:40 +00:00
parent 2b973ad4fd
commit 0f1237e6b8
6 changed files with 98 additions and 45 deletions

View file

@ -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

View file

@ -16,13 +16,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <bits/getopt_ext.h>
#include <cstdlib>
#include <functional>
#include <utility>
#include <iostream>
#include <string_view>
#include <iomanip>
#include <getopt.h>
#include "control.h"
#include "database/database.h"
#include "user.h"
using CTLFunction_t = std::function<int(ArgvVector_t)>;
@ -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;
}

View file

@ -20,7 +20,6 @@
#include <vector>
#include <string>
#include <getopt.h>
/* Control mode (aka. ctl mode) is a parameter controlled
* mode where the user can configure, create users, manipulate options,

View file

@ -19,6 +19,7 @@
#pragma once
#include <string>
#include <memory>
#include "user.h"
namespace DB
@ -51,4 +52,6 @@ namespace DB
};
private:
};
std::shared_ptr<DB::Database> load_db_from_cfg();
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include "database.h"
#include "sqlite/sqlite.h"
#include "config/config_db.h"
#include "config/config_loader.h"
#include "logger.h"
std::shared_ptr<DB::Database> DB::load_db_from_cfg()
{
std::shared_ptr<DB::Database> 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<DB::SQLite>(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;
}

View file

@ -60,36 +60,10 @@ int start_wormhole()
Logger::instance() << "Loading config...";
std::shared_ptr<DB::Database> 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<DB::SQLite>(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<DB::Database> 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<HTTP::Server> 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)
{