mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Now handlers need to have a new Object* parameter, this can be used to optionally carry class instances. (think of it as self in python object methods, except it can be null).
This commit is contained in:
parent
4e2ef6f1e9
commit
65f7f3a6b2
@ -19,10 +19,10 @@ void Application::setup_routes() {
|
||||
}
|
||||
|
||||
void Application::setup_middleware() {
|
||||
middlewares.push_back(Application::default_routing_middleware);
|
||||
middlewares.push_back(HandlerInstance(Application::default_routing_middleware));
|
||||
}
|
||||
|
||||
void Application::default_routing_middleware(Request *request) {
|
||||
void Application::default_routing_middleware(Object *instance, Request *request) {
|
||||
std::string path = request->http_parser->getPath();
|
||||
|
||||
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
||||
@ -31,11 +31,13 @@ void Application::default_routing_middleware(Request *request) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::function<void(Request *)> func;
|
||||
HandlerInstance handler_data;
|
||||
|
||||
//std::function<void(Object *, Request *)> func;
|
||||
|
||||
if (path == "/") {
|
||||
//quick shortcut
|
||||
func = index_func;
|
||||
handler_data = index_func;
|
||||
} else {
|
||||
std::string main_route = "";
|
||||
|
||||
@ -48,16 +50,16 @@ void Application::default_routing_middleware(Request *request) {
|
||||
|
||||
main_route = path.substr(1, endpos - 1);
|
||||
|
||||
func = main_route_map[main_route];
|
||||
handler_data = main_route_map[main_route];
|
||||
}
|
||||
|
||||
if (!func) {
|
||||
if (!handler_data.handler_func) {
|
||||
send_error(404, request);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
request->handler_func = func;
|
||||
request->handler_instance = handler_data;
|
||||
request->next_stage();
|
||||
}
|
||||
|
||||
@ -131,11 +133,12 @@ Application *Application::get_instance() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
std::function<void(Request *)> Application::index_func = nullptr;
|
||||
std::map<std::string, std::function<void(Request *)> > Application::main_route_map;
|
||||
HandlerInstance Application::index_func;
|
||||
std::map<std::string, HandlerInstance> Application::main_route_map;
|
||||
std::vector<HandlerInstance> Application::middlewares;
|
||||
|
||||
std::map<int, std::function<void(int, Request *)> > Application::error_handler_map;
|
||||
std::function<void(int, Request *)> Application::default_error_handler_func = nullptr;
|
||||
std::vector<std::function<void(Request *)> > Application::middlewares;
|
||||
|
||||
Application *Application::_instance = nullptr;
|
||||
|
||||
|
@ -1,40 +1,45 @@
|
||||
#ifndef APPLICATION_H
|
||||
#define APPLICATION_H
|
||||
|
||||
#include "object.h"
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "handler_instance.h"
|
||||
|
||||
class Request;
|
||||
|
||||
class Application {
|
||||
public:
|
||||
static std::string default_error_404_body;
|
||||
static std::string default_generic_error_body;
|
||||
static std::string default_error_404_body;
|
||||
static std::string default_generic_error_body;
|
||||
|
||||
static void handle_request(Request *request);
|
||||
static void send_error(int error_code, Request *request);
|
||||
static void send_file(const std::string &path, Request *request);
|
||||
static void send_error(int error_code, Request *request);
|
||||
static void send_file(const std::string &path, Request *request);
|
||||
|
||||
static void default_fallback_error_handler(int error_code, Request *request);
|
||||
static void default_404_error_handler(int error_code, Request *request);
|
||||
static void default_fallback_error_handler(int error_code, Request *request);
|
||||
static void default_404_error_handler(int error_code, Request *request);
|
||||
|
||||
virtual void setup_routes();
|
||||
virtual void setup_middleware();
|
||||
virtual void setup_middleware();
|
||||
|
||||
static void default_routing_middleware(Request *request);
|
||||
static void default_routing_middleware(Object *instance, Request *request);
|
||||
|
||||
Application();
|
||||
virtual ~Application();
|
||||
|
||||
static Application *get_instance();
|
||||
static Application *get_instance();
|
||||
|
||||
public:
|
||||
static HandlerInstance index_func;
|
||||
static std::map<std::string, HandlerInstance> main_route_map;
|
||||
static std::vector<HandlerInstance> middlewares;
|
||||
|
||||
static std::function<void(Request *)> index_func;
|
||||
static std::map<std::string, std::function<void(Request *)> > main_route_map;
|
||||
static std::map<int, std::function<void(int, Request *)> > error_handler_map;
|
||||
static std::function<void(int, Request *)> default_error_handler_func;
|
||||
static std::vector<std::function<void(Request *)> > middlewares;
|
||||
static std::function<void(int, Request *)> default_error_handler_func;
|
||||
|
||||
private:
|
||||
static Application *_instance;
|
||||
|
13
core/handler_instance.cpp
Normal file
13
core/handler_instance.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include "handler_instance.h"
|
||||
|
||||
#include "request.h"
|
||||
#include "object.h"
|
||||
|
||||
HandlerInstance::HandlerInstance() {
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
HandlerInstance::HandlerInstance(std::function<void(Object *, Request *)> p_handler_func, Object *p_instance) {
|
||||
handler_func = p_handler_func;
|
||||
instance = p_instance;
|
||||
}
|
17
core/handler_instance.h
Normal file
17
core/handler_instance.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef HANDLER_INSTANCE_H
|
||||
#define HANDLER_INSTANCE_H
|
||||
|
||||
#include <functional>
|
||||
|
||||
class Object;
|
||||
class Request;
|
||||
|
||||
struct HandlerInstance {
|
||||
std::function<void(Object *, Request *)> handler_func;
|
||||
Object *instance;
|
||||
|
||||
HandlerInstance();
|
||||
HandlerInstance(std::function<void(Object *, Request *)> p_handler_func, Object *p_instance = nullptr);
|
||||
};
|
||||
|
||||
#endif
|
1
core/object.cpp
Normal file
1
core/object.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "object.h"
|
12
core/object.h
Normal file
12
core/object.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef OBJECT_H
|
||||
#define OBJECT_H
|
||||
|
||||
class Object {
|
||||
public:
|
||||
|
||||
Object();
|
||||
virtual ~Object();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
@ -2,11 +2,13 @@
|
||||
|
||||
void Request::next_stage() {
|
||||
if (current_middleware_index == (*middleware_stack).size()) {
|
||||
handler_func(this);
|
||||
handler_instance.handler_func(handler_instance.instance, this);
|
||||
return;
|
||||
}
|
||||
|
||||
(*middleware_stack)[current_middleware_index++](this);
|
||||
const HandlerInstance &hi = (*middleware_stack)[current_middleware_index++];
|
||||
|
||||
hi.handler_func(hi.instance, this);
|
||||
}
|
||||
|
||||
void Request::send() {
|
||||
|
@ -7,6 +7,8 @@
|
||||
#include <brynet/net/http/HttpFormat.hpp>
|
||||
#include <brynet/net/http/HttpService.hpp>
|
||||
|
||||
#include "handler_instance.h"
|
||||
|
||||
using namespace brynet;
|
||||
using namespace brynet::net;
|
||||
using namespace brynet::net::http;
|
||||
@ -18,8 +20,8 @@ public:
|
||||
HttpResponse *response;
|
||||
|
||||
uint32_t current_middleware_index;
|
||||
std::function<void(Request *)> handler_func;
|
||||
std::vector<std::function<void(Request *)> > *middleware_stack;
|
||||
HandlerInstance handler_instance;
|
||||
std::vector<HandlerInstance> *middleware_stack;
|
||||
|
||||
void next_stage();
|
||||
void send();
|
||||
|
@ -6,7 +6,9 @@
|
||||
|
||||
#include "core/file_cache.h"
|
||||
|
||||
void RDNApplication::index(Request *request) {
|
||||
#include "core/handler_instance.h"
|
||||
|
||||
void RDNApplication::index(Object *instance, Request *request) {
|
||||
std::string body;
|
||||
|
||||
if (FileCache::get_singleton()->get_cached_body("index", &body)) {
|
||||
@ -23,7 +25,7 @@ void RDNApplication::index(Request *request) {
|
||||
request->send();
|
||||
}
|
||||
|
||||
void RDNApplication::session_middleware_func(Request *request) {
|
||||
void RDNApplication::session_middleware_func(Object* instance, Request *request) {
|
||||
std::cout << "test: session_middleware_func called" << std::endl;
|
||||
|
||||
//if fail
|
||||
@ -35,9 +37,9 @@ void RDNApplication::session_middleware_func(Request *request) {
|
||||
void RDNApplication::setup_routes() {
|
||||
Application::setup_routes();
|
||||
|
||||
index_func = index;
|
||||
index_func = HandlerInstance(index);
|
||||
|
||||
main_route_map["asd"] = index;
|
||||
main_route_map["asd"] = HandlerInstance(index);
|
||||
}
|
||||
|
||||
void RDNApplication::setup_middleware() {
|
||||
|
@ -3,12 +3,13 @@
|
||||
|
||||
#include "core/application.h"
|
||||
#include "core/theme.h"
|
||||
#include "core/object.h"
|
||||
|
||||
class RDNApplication : public Application {
|
||||
public:
|
||||
static void index(Request *request);
|
||||
static void index(Object *instance, Request *request);
|
||||
|
||||
static void session_middleware_func(Request *request);
|
||||
static void session_middleware_func(Object* instance, Request *request);
|
||||
|
||||
virtual void setup_routes();
|
||||
virtual void setup_middleware();
|
||||
|
Loading…
Reference in New Issue
Block a user