Database Pool
This commit is contained in:
parent
59c1ffd027
commit
fe2fbb9ad0
23 changed files with 268 additions and 63 deletions
|
@ -13,6 +13,11 @@
|
|||
#include "handler/login.h"
|
||||
|
||||
constexpr const char* pepper = "well, not much of a secret, huh?";
|
||||
constexpr const char* dbLogin = "pica";
|
||||
constexpr const char* dbPassword = "pica";
|
||||
constexpr const char* dbName = "pica";
|
||||
constexpr const char* dbPath = "/run/mysqld/mysqld.sock";
|
||||
constexpr uint8_t dbConnectionsCount = 4;
|
||||
|
||||
constexpr const char* randomChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
constexpr uint8_t saltSize = 16;
|
||||
|
@ -28,18 +33,22 @@ Server::Server():
|
|||
requestCount(0),
|
||||
serverName(std::nullopt),
|
||||
router(),
|
||||
db(),
|
||||
pool(DB::Pool::create()),
|
||||
sessions()
|
||||
{
|
||||
std::cout << "Startig pica..." << std::endl;
|
||||
|
||||
db = DBInterface::create(DBInterface::Type::mysql);
|
||||
std::cout << "Database type: MySQL" << std::endl;
|
||||
pool->addInterfaces(
|
||||
DB::Interface::Type::mysql,
|
||||
dbConnectionsCount,
|
||||
dbLogin,
|
||||
dbPassword,
|
||||
dbName,
|
||||
dbPath
|
||||
);
|
||||
|
||||
db->setCredentials("pica", "pica");
|
||||
db->setDatabase("pica");
|
||||
DB::Resource db = pool->request();
|
||||
|
||||
db->connect("/run/mysqld/mysqld.sock");
|
||||
db->migrate(currentDbVesion);
|
||||
|
||||
router.addRoute(std::make_unique<Handler::Info>());
|
||||
|
@ -112,10 +121,12 @@ unsigned int Server::registerAccount(const std::string& login, const std::string
|
|||
if (result != ARGON2_OK)
|
||||
throw std::runtime_error(std::string("Hashing failed: ") + argon2_error_message(result));
|
||||
|
||||
DB::Resource db = pool->request();
|
||||
return db->registerAccount(login, hash);
|
||||
}
|
||||
|
||||
bool Server::validatePassword(const std::string& login, const std::string& password) {
|
||||
DB::Resource db = pool->request();
|
||||
std::string hash = db->getAccountHash(login);
|
||||
|
||||
std::string spiced = password + pepper;
|
||||
|
@ -139,9 +150,10 @@ Session& Server::openSession(const std::string& login) {
|
|||
try {
|
||||
accessToken = generateRandomString(32);
|
||||
renewToken = generateRandomString(32);
|
||||
DB::Resource db = pool->request();
|
||||
sessionId = db->createSession(login, accessToken, renewToken);
|
||||
break;
|
||||
} catch (const DBInterface::Duplicate& e) {
|
||||
} catch (const DB::Duplicate& e) {
|
||||
std::cout << "Duplicate on creating session, trying again with different tokens";
|
||||
}
|
||||
} while (--counter != 0);
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
#include "response/response.h"
|
||||
#include "router.h"
|
||||
#include "session.h"
|
||||
#include "database/dbinterface.h"
|
||||
#include "database/pool.h"
|
||||
#include "utils/helpers.h"
|
||||
#include "config.h"
|
||||
|
||||
|
@ -49,6 +49,6 @@ private:
|
|||
uint64_t requestCount;
|
||||
std::optional<std::string> serverName;
|
||||
Router router;
|
||||
std::unique_ptr<DBInterface> db;
|
||||
std::shared_ptr<DB::Pool> pool;
|
||||
Sessions sessions;
|
||||
};
|
||||
|
|
|
@ -10,6 +10,10 @@
|
|||
class Session : public Accepting {
|
||||
public:
|
||||
Session(unsigned int id, const std::string& access, const std::string& renew);
|
||||
Session(const Session&) = delete;
|
||||
Session(Session&& other);
|
||||
Session& operator = (const Session&) = delete;
|
||||
Session& operator = (Session&& other);
|
||||
|
||||
std::string getAccessToken() const;
|
||||
std::string getRenewToken() const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue