polished up event loop changes
Some checks failed
Docker Build & Publish / build (push) Failing after 55m49s
Some checks failed
Docker Build & Publish / build (push) Failing after 55m49s
This commit is contained in:
parent
058c395095
commit
409024e04a
18 changed files with 354 additions and 428 deletions
112
src/api_main.cpp
112
src/api_main.cpp
|
@ -1,112 +0,0 @@
|
|||
#include "../lib/anthracite.hpp"
|
||||
#include "../lib/backends/backend.hpp"
|
||||
#include "../lib/http/constants.hpp"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
|
||||
using namespace anthracite;
|
||||
|
||||
using CallbackType = std::unique_ptr<http::response> (*)(http::request&);
|
||||
class api_backend : public backends::backend {
|
||||
|
||||
class RouteNode {
|
||||
public:
|
||||
std::optional<CallbackType> callback;
|
||||
|
||||
RouteNode()
|
||||
: callback(std::nullopt)
|
||||
{
|
||||
}
|
||||
std::unordered_map<std::string, RouteNode> routes;
|
||||
};
|
||||
|
||||
RouteNode root;
|
||||
|
||||
std::unique_ptr<http::response> default_route(http::request& req)
|
||||
{
|
||||
std::unique_ptr<http::response> resp = std::make_unique<http::response>();
|
||||
|
||||
resp->add_body("Not Found");
|
||||
resp->add_header(http::header("Content-Type", "application/json"));
|
||||
resp->add_status(http::status_codes::NOT_FOUND);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
std::unique_ptr<http::response> find_handler(http::request& req)
|
||||
{
|
||||
std::string filename = req.path().substr(1);
|
||||
std::vector<std::string> result;
|
||||
std::stringstream ss(filename);
|
||||
std::string item;
|
||||
|
||||
RouteNode* cur = &root;
|
||||
while (getline(ss, item, '/')) {
|
||||
if (cur->routes.find(item) == cur->routes.end()) {
|
||||
if (cur->routes.find("*") == cur->routes.end()) {
|
||||
break;
|
||||
} else {
|
||||
cur = &cur->routes["*"];
|
||||
}
|
||||
} else {
|
||||
cur = &cur->routes[item];
|
||||
}
|
||||
}
|
||||
|
||||
if (cur->callback.has_value()) {
|
||||
return cur->callback.value()(req);
|
||||
} else {
|
||||
return default_route(req);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<http::response> handle_request(http::request& req) override
|
||||
{
|
||||
return find_handler(req);
|
||||
}
|
||||
|
||||
public:
|
||||
api_backend()
|
||||
{
|
||||
root.routes = std::unordered_map<std::string, RouteNode>();
|
||||
}
|
||||
|
||||
void register_endpoint(std::string pathspec, CallbackType callback)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
std::stringstream ss(pathspec);
|
||||
std::string item;
|
||||
|
||||
RouteNode* cur = &root;
|
||||
while (getline(ss, item, '/')) {
|
||||
cur->routes[item] = RouteNode {};
|
||||
cur = &cur->routes[item];
|
||||
}
|
||||
|
||||
cur->callback = callback;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<http::response> handle_request(http::request& req)
|
||||
{
|
||||
std::unique_ptr<http::response> resp = std::make_unique<http::response>();
|
||||
|
||||
resp->add_body(R"({"user": "endpoint"}")");
|
||||
resp->add_header(http::header("Content-Type", "application/json"));
|
||||
resp->add_status(http::status_codes::OK);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto args = std::span(argv, size_t(argc));
|
||||
api_backend ab;
|
||||
ab.register_endpoint("users/*", handle_request);
|
||||
//anthracite_main(argc, argv, ab);
|
||||
}
|
|
@ -1,15 +1,35 @@
|
|||
#include "../lib/anthracite.hpp"
|
||||
#include "../lib/backends/file_backend.hpp"
|
||||
#include "../lib/config/config.hpp"
|
||||
#include "../lib/log/log.hpp"
|
||||
#include "../lib/thread_mgr/event_loop.hpp"
|
||||
#include "signal.h"
|
||||
#include "string.h"
|
||||
#include <memory>
|
||||
|
||||
using namespace anthracite;
|
||||
std::shared_ptr<anthracite::thread_mgr::thread_mgr> server = nullptr;
|
||||
|
||||
extern "C" void signalHandler(int signum)
|
||||
{
|
||||
anthracite::log::warn << "Caught signal SIG" << sigabbrev_np(signum) << ", exiting Anthracite" << std::endl;
|
||||
if (server != nullptr) {
|
||||
server->stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
backends::file_backend fb("./www");
|
||||
config::config cfg(5);
|
||||
cfg.add_http_config(config::http_config(8080));
|
||||
cfg.add_https_config(config::https_config(8081, "", ""));
|
||||
anthracite::log::logger.initialize(anthracite::log::LOG_LEVEL_INFO);
|
||||
anthracite::log::info << "Starting Anthracite, a higher performance web server" << std::endl;
|
||||
signal(SIGINT, signalHandler);
|
||||
|
||||
anthracite_main(fb, cfg);
|
||||
anthracite::backends::file_backend fb("./www");
|
||||
anthracite::config::config cfg(5, 10000);
|
||||
cfg.add_http_config(anthracite::config::http_config(8080));
|
||||
// cfg.add_https_config(config::https_config(8081, "", ""));
|
||||
|
||||
server = std::make_shared<anthracite::thread_mgr::event_loop>(fb, cfg);
|
||||
|
||||
server->start();
|
||||
|
||||
anthracite::log::info << "Stopping Anthracite, a higher performance web server" << std::endl;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue