database start + cmake policy update
This commit is contained in:
parent
d0f7e54f7e
commit
2923f6f5c5
4 changed files with 69 additions and 7 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
cmake_policy(SET CMP0167 OLD)
|
||||||
|
|
||||||
project(short-link VERSION 1.0 LANGUAGES CXX)
|
project(short-link VERSION 1.0 LANGUAGES CXX)
|
||||||
|
|
||||||
|
|
@ -14,7 +15,7 @@ add_executable(Application src/main.cpp
|
||||||
src/request_handler.cpp
|
src/request_handler.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
find_package(Boost REQUIRED COMPONENTS filesystem system)
|
find_package(Boost REQUIRED filesystem system)
|
||||||
|
|
||||||
if(Boost_FOUND)
|
if(Boost_FOUND)
|
||||||
include_directories(${Boost_INCLUDE_DIRS})
|
include_directories(${Boost_INCLUDE_DIRS})
|
||||||
|
|
|
||||||
28
includes/database_service.hpp
Normal file
28
includes/database_service.hpp
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
#ifndef DATABASE_SERVICE_HPP
|
||||||
|
#define DATABASE_SERVICE_HPP
|
||||||
|
|
||||||
|
#include <boost/asio/io_context.hpp>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <boost/redis/connection.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
class DatabaseService {
|
||||||
|
public:
|
||||||
|
static DatabaseService& getInstance(boost::asio::io_context& io_context, std::size_t pool_size);
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<boost::redis::connection> getConnection(); //retrive a connection from the connection pool
|
||||||
|
|
||||||
|
DatabaseService(boost::asio::io_context& io_context, std::size_t pool_size = 4);
|
||||||
|
~DatabaseService();
|
||||||
|
DatabaseService(DatabaseService const&);
|
||||||
|
void operator=(DatabaseService const&);
|
||||||
|
|
||||||
|
boost::asio::io_context& io_context_;
|
||||||
|
std::size_t pool_size_;
|
||||||
|
std::vector<std::shared_ptr<boost::redis::connection>> connection_pool_;
|
||||||
|
std::size_t current_index_ = 0; //index used for round robin selection
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
16
src/database_connection.cpp
Normal file
16
src/database_connection.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#include "../includes/database_service.hpp"
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
|
||||||
|
DatabaseService& DatabaseService::getInstance(boost::asio::io_context &io_context, std::size_t pool_size) {
|
||||||
|
static DatabaseService INSTANCE;
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseService::DatabaseService(boost::asio::io_context& io_context, std::size_t pool_size) : io_context_(io_context), pool_size_(pool_size) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseService::~DatabaseService() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,31 @@
|
||||||
#include "../includes/request_handler.hpp"
|
#include "../includes/request_handler.hpp"
|
||||||
#include <boost/beast/http/field.hpp>
|
#include <boost/beast/http/field.hpp>
|
||||||
|
#include <boost/beast/http/file_body.hpp>
|
||||||
#include <boost/beast/http/message.hpp>
|
#include <boost/beast/http/message.hpp>
|
||||||
#include <boost/beast/http/status.hpp>
|
#include <boost/beast/http/status.hpp>
|
||||||
#include <boost/beast/http/string_body.hpp>
|
#include <boost/beast/http/string_body.hpp>
|
||||||
|
|
||||||
http::response<http::string_body> RequestHandler::handle(const http::request<http::string_body>& request) {
|
http::response<http::string_body> RequestHandler::handle(const http::request<http::string_body>& request) {
|
||||||
http::response<http::string_body> response{http::status::ok, request.version()};
|
string_view target = request.target();
|
||||||
response.set(http::field::server, "Beast");
|
|
||||||
response.set(http::field::content_type, "text/plain");
|
if (target == "/") {
|
||||||
response.body() = "Hello, World!";
|
//case 1: "/" -> serve angular frontend or static frontend what ever
|
||||||
response.prepare_payload();
|
http::response<http::string_body> response;
|
||||||
return response;
|
response.result(http::status::ok);
|
||||||
|
response.version(request.version());
|
||||||
|
response.set(http::field::server, "Beast");
|
||||||
|
response.set(http::field::content_type, "text/html");
|
||||||
|
response.keep_alive();
|
||||||
|
|
||||||
|
//todo: load angular application / plain html & js
|
||||||
|
response.body() = "<html><h1>TEST</h1></html>";
|
||||||
|
|
||||||
|
response.prepare_payload();
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
//case 2: "/url" -> redirect to expanded url
|
||||||
|
//case 3: neither -> redirect to 404
|
||||||
|
|
||||||
|
return http::response<http::string_body>{http::status::bad_request, request.version()};
|
||||||
}
|
}
|
||||||
Reference in a new issue