cleaned the code
This commit is contained in:
parent
8097895361
commit
f498328249
@ -1,114 +1,3 @@
|
|||||||
#include "utils.hpp"
|
|
||||||
#include "databasepool.h"
|
#include "databasepool.h"
|
||||||
#include <optional>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
Database::Database() :
|
|
||||||
m_db(nullptr)
|
|
||||||
{}
|
|
||||||
|
|
||||||
Database::~Database() {
|
|
||||||
sqlite3_close(m_db);
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_stmt* Database::prepareStmt(const string& sql){
|
|
||||||
sqlite3_stmt* stmt = nullptr;
|
|
||||||
if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
|
||||||
CROW_LOG_ERROR << "Failed to prepare statement: " << sqlite3_errmsg(m_db);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return stmt;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Database::exec(const char* sqlQuery) {
|
|
||||||
char* errmsg = nullptr;
|
|
||||||
int rc = sqlite3_exec(m_db, sqlQuery, nullptr, nullptr, &errmsg);
|
|
||||||
if (rc != SQLITE_OK) {
|
|
||||||
CROW_LOG_ERROR << "SQL error: " << errmsg;
|
|
||||||
sqlite3_free(errmsg);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Database::exec(const std::string& sqlQuery){
|
|
||||||
return exec(sqlQuery.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_stmt* Database::bind(const std::string sql, const Database::QueryData& data) {
|
|
||||||
sqlite3_stmt* stmt = nullptr;
|
|
||||||
if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
|
||||||
CROW_LOG_ERROR << "Failed to prepare statement: " << sqlite3_errmsg(m_db);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < data.size(); i++) {
|
|
||||||
std::visit([stmt, i](auto&& val) {
|
|
||||||
using T = std::decay_t<decltype(val)>;
|
|
||||||
if constexpr (std::is_same_v<T, int64_t>)
|
|
||||||
sqlite3_bind_int64(stmt, i + 1, val);
|
|
||||||
else if constexpr (std::is_same_v<T, double>)
|
|
||||||
sqlite3_bind_double(stmt, i + 1, val);
|
|
||||||
else if constexpr (std::is_same_v<T, std::string>)
|
|
||||||
sqlite3_bind_text(stmt, i + 1, val.c_str(), -1, SQLITE_TRANSIENT);
|
|
||||||
}, data[i]);
|
|
||||||
}
|
|
||||||
return stmt;
|
|
||||||
}
|
|
||||||
|
|
||||||
map<string, string> Database::getStrMap(const std::string sql, const Database::QueryData& data){
|
|
||||||
map<string, string> map;
|
|
||||||
sqlite3_stmt* stmt = bind(sql, data);
|
|
||||||
if (stmt == nullptr)
|
|
||||||
return map;
|
|
||||||
|
|
||||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
||||||
string key = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
|
||||||
string value = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
|
||||||
map[key] = utils::urlDecode(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
set<string> Database::getStrSet(const string& sql){
|
|
||||||
sqlite3_stmt* stmt = prepareStmt(sql);
|
|
||||||
set<string> vec;
|
|
||||||
if (stmt == nullptr)
|
|
||||||
return vec;
|
|
||||||
|
|
||||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
||||||
string s = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
|
||||||
vec.insert(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
return vec;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<int64_t> Database::insert(const std::string& sql) {
|
|
||||||
sqlite3_stmt* stmt = prepareStmt(sql);
|
|
||||||
if (stmt == nullptr)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
if (sqlite3_step(stmt) != SQLITE_DONE) {
|
|
||||||
CROW_LOG_ERROR << "Insert failed: " << sqlite3_errmsg(m_db);
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
return sqlite3_last_insert_rowid(m_db);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Database::open(){
|
|
||||||
int rc = sqlite3_open("app.db", &m_db);
|
|
||||||
if (rc) {
|
|
||||||
CROW_LOG_ERROR << "Can't open database: " << sqlite3_errmsg(m_db);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
DatabasePool dbpool(std::thread::hardware_concurrency());
|
DatabasePool dbpool(std::thread::hardware_concurrency());
|
||||||
@ -13,75 +13,9 @@
|
|||||||
#include "ShadowrunDb.hpp"
|
#include "ShadowrunDb.hpp"
|
||||||
#include "loginDb.hpp"
|
#include "loginDb.hpp"
|
||||||
|
|
||||||
class Database {
|
namespace Database {
|
||||||
|
|
||||||
typedef std::vector<std::variant<int64_t, std::string>> QueryData;
|
|
||||||
|
|
||||||
public:
|
|
||||||
static constexpr std::string dbFile = "test.db";
|
static constexpr std::string dbFile = "test.db";
|
||||||
|
|
||||||
Database();
|
|
||||||
~Database();
|
|
||||||
|
|
||||||
bool open();
|
|
||||||
bool exec(const char* sqlQuery);
|
|
||||||
bool exec(const std::string& sqlQuery);
|
|
||||||
|
|
||||||
std::optional<int64_t> insert(const std::string& sql);
|
|
||||||
|
|
||||||
std::set<std::string> getStrSet(const std::string& sql);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
std::optional<T> getSqlData(sqlite3_stmt* stmt, int i)
|
|
||||||
{
|
|
||||||
if (stmt == nullptr)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
if constexpr (std::is_same_v<T, int>) {
|
|
||||||
return sqlite3_column_int64(stmt, i);
|
|
||||||
}
|
}
|
||||||
else if constexpr (std::is_same_v<T, std::string>){
|
|
||||||
return reinterpret_cast<const char*>(sqlite3_column_text(stmt, i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
std::optional<T> get(const std::string sql, const QueryData& data){
|
|
||||||
sqlite3_stmt* stmt = bind(sql, data);
|
|
||||||
|
|
||||||
std::optional<T> ret = getSqlData<T>(stmt, 0);
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T1, typename T2>
|
|
||||||
std::optional<std::pair<T1, T2>> get(const std::string sql, const QueryData& data){
|
|
||||||
sqlite3_stmt* stmt = bind(sql, data);
|
|
||||||
if ( (stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW ) ) {
|
|
||||||
CROW_LOG_ERROR << "Failed to run statement: " << sqlite3_errmsg(m_db);
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
std::optional<T1> v1 = getSqlData<T1>(stmt, 0);
|
|
||||||
std::optional<T2> v2 = getSqlData<T2>(stmt, 1);
|
|
||||||
|
|
||||||
if (!v1.has_value() || !v2.has_value())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
return std::make_pair(v1.value(), v2.value());
|
|
||||||
}
|
|
||||||
|
|
||||||
sqlite3_stmt* bind(const std::string sql, const QueryData& data);
|
|
||||||
|
|
||||||
std::map<std::string, std::string> getStrMap(const std::string sql, const QueryData& data);
|
|
||||||
|
|
||||||
private:
|
|
||||||
sqlite3_stmt* prepareStmt(const std::string& sql);
|
|
||||||
|
|
||||||
sqlite3* m_db;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
inline auto make_database() {
|
inline auto make_database() {
|
||||||
auto storage = sqlite_orm::make_storage(Database::dbFile,
|
auto storage = sqlite_orm::make_storage(Database::dbFile,
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "database.hpp"
|
#include "database.hpp"
|
||||||
|
|||||||
@ -34,7 +34,7 @@ std::optional<std::string> loginUser(const std::string& username, const std::str
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool initLogin(crow::SimpleApp& app)
|
void initLogin(crow::SimpleApp& app)
|
||||||
{
|
{
|
||||||
// createUser("lukas", "Trollar4928");
|
// createUser("lukas", "Trollar4928");
|
||||||
|
|
||||||
@ -70,7 +70,5 @@ bool initLogin(crow::SimpleApp& app)
|
|||||||
res.body = "Logged in";
|
res.body = "Logged in";
|
||||||
return res;
|
return res;
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4,12 +4,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <crow.h>
|
#include <crow.h>
|
||||||
#include "unordered_map"
|
|
||||||
#include "string.h"
|
|
||||||
|
|
||||||
namespace login {
|
namespace login {
|
||||||
|
|
||||||
bool initLogin(crow::SimpleApp& app);
|
void initLogin(crow::SimpleApp& app);
|
||||||
|
|
||||||
bool isLoggedIn(const crow::request& req);
|
bool isLoggedIn(const crow::request& req);
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
#include "loginDb.hpp"
|
#include "loginDb.hpp"
|
||||||
#include "databasepool.h"
|
#include "databasepool.h"
|
||||||
#include <algorithm>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|||||||
@ -1,15 +1,11 @@
|
|||||||
#ifndef __LOGINDB_H__
|
#ifndef __LOGINDB_H__
|
||||||
#define __LOGINDB_H__
|
#define __LOGINDB_H__
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
#include "sqlite_orm.h"
|
|
||||||
#include "magic_enum.hpp"
|
|
||||||
|
|
||||||
namespace login
|
namespace login
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include "json_settings.h"
|
#include "json_settings.h"
|
||||||
#include "systemd.h"
|
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
#include "login.hpp"
|
#include "login.hpp"
|
||||||
#include "ShadowrunApi.hpp"
|
#include "ShadowrunApi.hpp"
|
||||||
@ -67,6 +66,7 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shadowrun::initApi(app);
|
shadowrun::initApi(app);
|
||||||
|
login::initLogin(app);
|
||||||
|
|
||||||
// asssets is not svelte generated
|
// asssets is not svelte generated
|
||||||
CROW_ROUTE(app, "/assets/<path>")
|
CROW_ROUTE(app, "/assets/<path>")
|
||||||
@ -109,11 +109,6 @@ int main() {
|
|||||||
return crow::response(404, "text/html", data);
|
return crow::response(404, "text/html", data);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* if(!login::initLogin(app))
|
|
||||||
{
|
|
||||||
CROW_LOG_ERROR << "Failed to init Login API";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
app.loglevel(crow::LogLevel::INFO);
|
app.loglevel(crow::LogLevel::INFO);
|
||||||
app.bindaddr("0.0.0.0").port(httpPort).multithreaded().run();
|
app.bindaddr("0.0.0.0").port(httpPort).multithreaded().run();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user