Setup
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "server.hpp"
|
||||
#include "store.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
@@ -14,9 +15,11 @@
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/signal_set.hpp>
|
||||
#include <boost/asio/use_future.hpp>
|
||||
#include <boost/json/object.hpp>
|
||||
|
||||
#include <ligo.hpp>
|
||||
#include <ligo/detail/errors.hpp>
|
||||
#include <modulus.hpp>
|
||||
|
||||
|
||||
@@ -114,6 +117,23 @@ auto fpol::run(fpol::server_options const &options) -> int {
|
||||
if (!options.web_root.empty()) {
|
||||
add_web_routes(router, options.web_root);
|
||||
}
|
||||
auto store = fpol::store{io.get_executor(), options.database};
|
||||
auto initialized = boost::asio::co_spawn(io, store.initialize(), boost::asio::use_future);
|
||||
io.run();
|
||||
initialized.get();
|
||||
io.restart();
|
||||
for (auto const &action : {"rules", "catalog", "capacity", "score-preview"}) {
|
||||
auto method = std::string_view{action} == "rules" || std::string_view{action} == "catalog" ? "GET" : "POST";
|
||||
router.add_route(method, std::string{"/api/"} + action, [&store, action = std::string{action}](ligo::request_context, boost::json::value input) -> boost::asio::awaitable<std::expected<boost::json::value, modulus::error>> {
|
||||
try {
|
||||
co_return co_await store.handle(action, std::move(input));
|
||||
} catch (std::invalid_argument const &error) {
|
||||
co_return std::unexpected(ligo::detail::make_error(ligo::detail::error_invalid_argument, error.what()));
|
||||
} catch (std::out_of_range const &error) {
|
||||
co_return std::unexpected(ligo::detail::make_error(ligo::detail::error_invalid_argument, error.what()));
|
||||
}
|
||||
});
|
||||
}
|
||||
auto http_options = ligo::http::server_options{};
|
||||
http_options.on_error = [](modulus::error error) { modulus::log_warn(error.describe()); };
|
||||
auto server = ligo::http::server{io, {.host = options.host, .port = options.port}, std::move(router), std::move(http_options)};
|
||||
|
||||
Reference in New Issue
Block a user