Add working tree

This commit is contained in:
Pablu23
2024-11-13 11:57:12 +01:00
parent 601e0269c1
commit e0d1ac42f7
4 changed files with 221 additions and 32 deletions

View File

@@ -1,5 +1,6 @@
#include "http.hpp"
#include "router.hpp"
#include "tree.hpp"
using namespace http;
@@ -9,36 +10,70 @@ void HelloWorld(Request req, Response *res) {
}
int main() {
Router router(8181);
Tree t;
// Allow all Methods
router.Handle("/helloWorld", HelloWorld);
router.Handle("/healthz", [](Request req, Response *res) {
res->SetStatusCode(statuscode::OK);
res->SetPayload(std::vector<std::byte>());
res->SetContentType("text/plain");
});
// Only allow GET
router.Handle("GET /echo/{name}", [](Request req, Response *res) {
std::string name = req.path.Get("name").value_or("No Name given");
res->SetPayload("Hello " + name);
res->SetContentType("text/plain");
});
// Only allow POST
router.Handle("POST /echo/{name}", [](Request req, Response *res) {
std::string name = req.path.Get("name").value_or("No Name given");
res->SetPayload("Hello with Post" + name);
res->SetContentType("text/plain");
});
router.Handle("/", [](Request req, Response *res) {
t.AddPath("/test/dummy", [](Request req, Response *res) {
res->SetPayload("Main");
res->SetContentType("text/plain");
});
router.Start();
return 0;
t.AddPath("/test/dummy/main", [](Request req, Response *res) {
res->SetPayload("Main");
res->SetContentType("text/plain");
});
t.AddPath("/test/dummy/main2", [](Request req, Response *res) {
res->SetPayload("Main");
res->SetContentType("text/plain");
});
t.AddPath("/test/dummy2/main", [](Request req, Response *res) {
res->SetPayload("Main");
res->SetContentType("text/plain");
});
t.AddPath("/var/main", [](Request req, Response *res) {
res->SetPayload("Main");
res->SetContentType("text/plain");
});
t.AddPath("/test/dummy2", [](Request req, Response *res) {
res->SetPayload("Main");
res->SetContentType("text/plain");
});
t.DebugPrint();
// Router router(8181);
//
// // Allow all Methods
// router.Handle("/helloWorld", HelloWorld);
//
// router.Handle("/healthz", [](Request req, Response *res) {
// res->SetStatusCode(statuscode::OK);
// res->SetPayload(std::vector<std::byte>());
// res->SetContentType("text/plain");
// });
//
// // Only allow GET
// router.Handle("GET /echo/{name}", [](Request req, Response *res) {
// std::string name = req.path.Get("name").value_or("No Name given");
// res->SetPayload("Hello " + name);
// res->SetContentType("text/plain");
// });
//
// // Only allow POST
// router.Handle("POST /echo/{name}", [](Request req, Response *res) {
// std::string name = req.path.Get("name").value_or("No Name given");
// res->SetPayload("Hello with Post" + name);
// res->SetContentType("text/plain");
// });
//
// router.Handle("/", [](Request req, Response *res) {
// res->SetPayload("Main");
// res->SetContentType("text/plain");
// });
//
// router.Start();
// return 0;
}