format + README
This commit is contained in:
parent
71be773d49
commit
0ebdb34601
12 changed files with 370 additions and 345 deletions
|
@ -1,30 +1,33 @@
|
|||
#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 <iostream>
|
||||
|
||||
using namespace anthracite;
|
||||
|
||||
using CallbackType = std::unique_ptr<http::response> (*)(http::request&);
|
||||
using CallbackType = std::unique_ptr<http::response> (*)(http::request&);
|
||||
class api_backend : public backends::backend {
|
||||
|
||||
class RouteNode {
|
||||
public:
|
||||
|
||||
public:
|
||||
std::optional<CallbackType> callback;
|
||||
|
||||
RouteNode() : callback(std::nullopt) {}
|
||||
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> default_route(http::request& req)
|
||||
{
|
||||
std::unique_ptr<http::response> resp = std::make_unique<http::response>();
|
||||
|
||||
resp->add_body("Not Found");
|
||||
|
@ -34,14 +37,15 @@ class api_backend : public backends::backend {
|
|||
return resp;
|
||||
}
|
||||
|
||||
std::unique_ptr<http::response> find_handler(http::request& req) {
|
||||
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::stringstream ss(filename);
|
||||
std::string item;
|
||||
|
||||
RouteNode* cur = &root;
|
||||
while (getline (ss, item, '/')) {
|
||||
while (getline(ss, item, '/')) {
|
||||
if (cur->routes.find(item) == cur->routes.end()) {
|
||||
if (cur->routes.find("*") == cur->routes.end()) {
|
||||
break;
|
||||
|
@ -58,28 +62,28 @@ class api_backend : public backends::backend {
|
|||
} else {
|
||||
return default_route(req);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<http::response> handle_request(http::request& req) override {
|
||||
std::unique_ptr<http::response> handle_request(http::request& req) override
|
||||
{
|
||||
return find_handler(req);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
api_backend() {
|
||||
public:
|
||||
api_backend()
|
||||
{
|
||||
root.routes = std::unordered_map<std::string, RouteNode>();
|
||||
}
|
||||
|
||||
void register_endpoint(std::string pathspec, CallbackType callback) {
|
||||
void register_endpoint(std::string pathspec, CallbackType callback)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
std::stringstream ss (pathspec);
|
||||
std::stringstream ss(pathspec);
|
||||
std::string item;
|
||||
|
||||
RouteNode* cur = &root;
|
||||
while (getline (ss, item, '/')) {
|
||||
cur->routes[item] = RouteNode{};
|
||||
while (getline(ss, item, '/')) {
|
||||
cur->routes[item] = RouteNode {};
|
||||
cur = &cur->routes[item];
|
||||
}
|
||||
|
||||
|
@ -87,17 +91,19 @@ class api_backend : public backends::backend {
|
|||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<http::response> handle_request(http::request& req) {
|
||||
std::unique_ptr<http::response> resp = std::make_unique<http::response>();
|
||||
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);
|
||||
resp->add_body(R"({"user": "endpoint"}")");
|
||||
resp->add_header(http::header("Content-Type", "application/json"));
|
||||
resp->add_status(http::status_codes::OK);
|
||||
|
||||
return resp;
|
||||
return resp;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto args = std::span(argv, size_t(argc));
|
||||
api_backend ab;
|
||||
ab.register_endpoint("users/*", handle_request);
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
|
||||
using namespace anthracite;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto args = std::span(argv, size_t(argc));
|
||||
backends::file_backend fb(argc > 2 ? args[2] : "./www");
|
||||
anthracite_main(argc, argv, fb);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue