Add get function to tree, and implement basic tree in router

This commit is contained in:
Pablu23
2024-11-13 13:23:56 +01:00
parent e0d1ac42f7
commit 1797b3c0ef
6 changed files with 116 additions and 137 deletions

View File

@@ -1,6 +1,5 @@
#include "http.hpp"
#include "router.hpp"
#include "tree.hpp"
using namespace http;
@@ -10,70 +9,35 @@ void HelloWorld(Request req, Response *res) {
}
int main() {
Tree t;
Router router(8181);
t.AddPath("/test/dummy", [](Request req, Response *res) {
// Allow all Methods
router.Handle("GET /helloWorld", HelloWorld);
router.Handle("GET /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("GET /", [](Request req, Response *res) {
res->SetPayload("Main");
res->SetContentType("text/plain");
});
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;
router.Start();
return 0;
}