2022-01-07 20:11:06 +01:00
|
|
|
#ifndef WEB_ROOT_H
|
|
|
|
#define WEB_ROOT_H
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-01-08 10:06:11 +01:00
|
|
|
#include "web_router_node.h"
|
2020-11-25 00:20:41 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
#include "mutex"
|
|
|
|
|
2020-12-01 12:56:04 +01:00
|
|
|
#include "handler_instance.h"
|
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
class Request;
|
2022-01-07 20:11:06 +01:00
|
|
|
class WebServer;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-01-08 10:06:11 +01:00
|
|
|
class WebRoot : public WebRouterNode {
|
|
|
|
RCPP_OBJECT(WebRoot, WebRouterNode);
|
2021-11-21 14:40:47 +01:00
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
public:
|
2020-12-01 12:56:04 +01:00
|
|
|
static std::string default_error_404_body;
|
|
|
|
static std::string default_generic_error_body;
|
2020-11-26 11:32:33 +01:00
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void handle_request_main(Request *request);
|
2021-04-28 22:58:13 +02:00
|
|
|
void send_error(int error_code, Request *request);
|
|
|
|
void send_file(const std::string &path, Request *request);
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-12-01 12:56:04 +01:00
|
|
|
static void default_fallback_error_handler(int error_code, Request *request);
|
|
|
|
static void default_404_error_handler(int error_code, Request *request);
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-12-08 16:22:18 +01:00
|
|
|
virtual void load_settings();
|
2020-11-25 00:20:41 +01:00
|
|
|
virtual void setup_routes();
|
2020-12-01 12:56:04 +01:00
|
|
|
virtual void setup_middleware();
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-04-28 22:58:13 +02:00
|
|
|
void default_routing_middleware(Object *instance, Request *request);
|
2020-11-26 12:10:21 +01:00
|
|
|
|
2020-12-01 23:37:10 +01:00
|
|
|
virtual void migrate();
|
|
|
|
|
2021-04-28 22:58:13 +02:00
|
|
|
void register_request_update(Request *request);
|
|
|
|
void unregister_request_update(Request *request);
|
|
|
|
void update();
|
2021-02-09 01:25:24 +01:00
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
WebServer *get_server();
|
|
|
|
|
|
|
|
WebRoot();
|
|
|
|
virtual ~WebRoot();
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-12-01 12:56:04 +01:00
|
|
|
public:
|
2021-04-28 22:58:13 +02:00
|
|
|
HandlerInstance index_func;
|
|
|
|
std::map<std::string, HandlerInstance> main_route_map;
|
|
|
|
std::vector<HandlerInstance> middlewares;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-04-28 22:58:13 +02:00
|
|
|
std::map<int, std::function<void(int, Request *)> > error_handler_map;
|
|
|
|
std::function<void(int, Request *)> default_error_handler_func;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
protected:
|
2021-04-28 22:58:13 +02:00
|
|
|
std::mutex _update_registered_requests_mutex;
|
|
|
|
std::vector<Request *> _update_registered_requests;
|
2020-11-25 00:20:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|