CMakeLists.txt

- added copy_frontend as dependency (else it wont automatically execute
request_handler.cpp
- implemented db connection
- fixed routing of short code
All Files
- Improved logging
This commit is contained in:
rawalcher 2024-12-29 18:53:46 +01:00
parent cbfc0b563c
commit 0d89ce5e2d
8 changed files with 251 additions and 89 deletions

View file

@ -1,15 +1,16 @@
#include "../includes/database_service.hpp"
#include "../includes/log_utils.hpp"
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
#include <mutex>
#include <stdexcept>
#include <mutex>
std::mutex DatabaseService::singleton_mutex;
DatabaseService* DatabaseService::INSTANCE = nullptr;
DatabaseService& DatabaseService::getInstance(const std::string& db_path, std::size_t pool_size) {
std::lock_guard<std::mutex> lock(singleton_mutex);
if (!INSTANCE) {
INSTANCE = new DatabaseService(db_path, pool_size);
@ -19,7 +20,10 @@ DatabaseService& DatabaseService::getInstance(const std::string& db_path, std::s
DatabaseService::DatabaseService(const std::string& db_path, std::size_t pool_size)
: db_path_(db_path), pool_size_(pool_size), current_index_(0) {
LOG(LogLevel::INFO, "Initializing DatabaseService");
if (pool_size_ <= 0) {
LOG(LogLevel::ERROR, "Invalid pool size");
throw std::invalid_argument("Connection pool size must be greater than zero.");
}
@ -27,13 +31,19 @@ DatabaseService::DatabaseService(const std::string& db_path, std::size_t pool_si
auto session = std::make_shared<soci::session>(soci::sqlite3, db_path_);
connection_pool_.emplace_back(session);
}
LOG(LogLevel::INFO, "Connection Pool filled");
try {
auto conn = getConnection();
*conn << "CREATE TABLE IF NOT EXISTS urls ("
"short_code CHAR(8) PRIMARY KEY, "
"original_url TEXT NOT NULL UNIQUE, "
"created_at INTEGER DEFAULT CURRENT_TIMESTAMP);";
// Ensure the table exists
auto conn = getConnection();
*conn << "CREATE TABLE IF NOT EXISTS urls ("
"short_code CHAR(8) PRIMARY KEY, "
"original_url TEXT NOT NULL UNIQUE, "
"created_at INTEGER DEFAULT CURRENT_TIMESTAMP);";
LOG(LogLevel::INFO, "Database schema initialized successfully");
} catch (const std::exception& e) {
LOG(LogLevel::ERROR, "Exception during schema creation: " + std::string(e.what()));
throw;
}
}
DatabaseService::~DatabaseService() {
@ -41,13 +51,13 @@ DatabaseService::~DatabaseService() {
}
std::shared_ptr<soci::session> DatabaseService::getConnection() {
std::lock_guard<std::mutex> lock(singleton_mutex);
if (connection_pool_.empty()) {
throw std::runtime_error("Connection pool is empty.");
}
LOG(LogLevel::INFO, "Returning connection at index: " + std::to_string(current_index_));
auto conn = connection_pool_[current_index_];
current_index_ = (current_index_ + 1) % pool_size_;
current_index_ = (current_index_ + 1) % connection_pool_.size();
return conn;
}
@ -58,6 +68,7 @@ std::string DatabaseService::generateShortUUID() {
}
void DatabaseService::shortenURL(const std::string& longURL, std::function<void(std::error_code, std::string)> callback) {
LOG(LogLevel::INFO, "Shortening URL: " + longURL);
try {
auto conn = getConnection();
std::string shortCode;
@ -73,11 +84,13 @@ void DatabaseService::shortenURL(const std::string& longURL, std::function<void(
soci::use(shortCode), soci::use(longURL);
callback({}, shortCode);
} catch (const std::exception& e) {
LOG(LogLevel::ERROR, "Failed to shorten URL: " + std::string(e.what()));
callback(std::make_error_code(std::errc::io_error), "");
}
}
void DatabaseService::getLongURL(const std::string& shortCode, std::function<void(std::error_code, std::string)> callback) {
LOG(LogLevel::INFO, "Getting long-URL from short-code: " + shortCode);
try {
auto conn = getConnection();
std::string longURL;
@ -86,9 +99,11 @@ void DatabaseService::getLongURL(const std::string& shortCode, std::function<voi
if (!longURL.empty()) {
callback({}, longURL);
} else {
LOG(LogLevel::ERROR, "Short code not found: " + shortCode);
callback(std::make_error_code(std::errc::no_such_file_or_directory), "");
}
} catch (const std::exception& e) {
LOG(LogLevel::ERROR, "Failed to retrieve long URL: " + std::string(e.what()));
callback(std::make_error_code(std::errc::io_error), "");
}
}