add webserver.hpp

This commit is contained in:
lucaspalomodevelop 2023-11-06 21:01:02 +01:00
parent bcac027fb5
commit 866830d61a
2 changed files with 93 additions and 0 deletions

View File

@ -14,5 +14,6 @@
// #include "src/faker/faker.hpp"
#include "src/exceptions/exceptions.hpp"
#include "src/classes/command.hpp"
#include "src/classes/webserver.hpp"
#endif // __LPSTD_HPP__

92
src/classes/webserver.hpp Normal file
View File

@ -0,0 +1,92 @@
#ifndef __LPSTD_WEBSERVER_H__
#define __LPSTD_WEBSERVER_H__
#include "../patterns/singleton.hpp"
#include <iostream>
#include <string>
#include <vector>
namespace lpstd
{
namespace classes
{
namespace web
{
struct route
{
std::string path;
std::string method;
void (*func)();
};
}
class WebServer : public lpstd::Singleton<WebServer>
{
private:
std::vector<web::route> routes = {};
public:
WebServer(/* args */)
{
}
~WebServer()
{
}
void addRoute(web::route route)
{
this->routes.push_back(route);
}
void addRoute(std::string path, std::string method, void (*func)())
{
web::route route = {path : path, method : method, func : func};
this->routes.push_back(route);
}
void get(std::string path, void (*func)())
{
this->addRoute(path, "GET", func);
}
void post(std::string path, void (*func)())
{
this->addRoute(path, "POST", func);
}
void put(std::string path, void (*func)())
{
this->addRoute(path, "PUT", func);
}
void patch(std::string path, void (*func)())
{
this->addRoute(path, "PATCH", func);
}
void del(std::string path, void (*func)())
{
this->addRoute(path, "DELETE", func);
}
void run(int port = 3000)
{
for (auto &route : this->routes)
{
std::cout << route.path << std::endl;
}
std::cout << "Running on port " << port << std::endl;
std::cout << "Listening..." << std::endl;
}
};
} // namespace classes
} // namespace
#endif // __LPSTD_WEBSERVER_H__