Files
localTube/Modules/Server/src/main.cpp
2021-06-04 00:58:25 +02:00

82 lines
1.9 KiB
C++

//
// Created by Kaonnull on 25.05.2021.
//
#include <string_view>
#include <fmt/format.h>
#include <httplib.h>
#include <soci/soci.h>
#include <soci/sqlite3/soci-sqlite3.h>
#include <spdlog/spdlog.h>
#include "api.hpp"
#include "tables.hpp"
#include "worker.hpp"
int main()
{
spdlog::set_level(spdlog::level::debug);
constexpr std::string_view sqlite_db_name = "db.sqlite";
constexpr std::string_view version = "Version 1";
soci::register_factory_sqlite3();
spdlog::info("Opening sqlite3 db: {}", sqlite_db_name);
soci::session sql("sqlite3", fmt::format("db={}", sqlite_db_name));
if (!sql.is_connected())
{
spdlog::error("sqlite connection failed!");
return -1;
}
try
{
Version v;
sql << "SELECT * FROM Version", soci::into(v);
if (v.version == version)
{
spdlog::info("DB Loaded");
} else
{
spdlog::error("DB Version miss match. Try deleting the sqlite file.");
return -1;
}
}
catch (soci::soci_error const& err)
{
spdlog::info("DB not initialised. Creating db...");
spdlog::info("Creating Version table");
{
auto version_table = sql.create_table("Version");
version_table.column("ID", soci::dt_integer);
version_table.column("version_info", soci::dt_string)("NOT NULL");
version_table.primary_key("Version", "ID");
}
Version v { 1, std::string { version } };
sql << "insert into Version(ID, version_info) values(:ID, :version_info)", soci::use(v);
Task::create_table(sql);
File::create_table(sql);
}
Worker worker(sql);
httplib::Server srv;
srv.set_mount_point("/", "www/");
srv.set_mount_point("/files", "www/");
srv.set_mount_point("/status", "www/");
Api api(sql, srv);
spdlog::info("Listing on 0.0.0.0 Port 80");
srv.listen("0.0.0.0", 8888);
}