2021-05-27 17:37:23 +02:00
|
|
|
#ifndef WEB_APPLICATION_H
|
|
|
|
#define WEB_APPLICATION_H
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-05-27 17:06:42 +02:00
|
|
|
#include "core/object.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;
|
|
|
|
|
2021-05-27 17:37:23 +02:00
|
|
|
class WebApplication {
|
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
|
|
|
|
2021-04-28 22:58:13 +02:00
|
|
|
void handle_request(Request *request);
|
|
|
|
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
|
|
|
|
2021-05-27 17:37:23 +02:00
|
|
|
WebApplication();
|
|
|
|
virtual ~WebApplication();
|
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
|