mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +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() {
|
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();
|
std::string path = request->http_parser->getPath();
|
||||||
|
|
||||||
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
||||||
@ -31,11 +31,13 @@ void Application::default_routing_middleware(Request *request) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::function<void(Request *)> func;
|
HandlerInstance handler_data;
|
||||||
|
|
||||||
|
//std::function<void(Object *, Request *)> func;
|
||||||
|
|
||||||
if (path == "/") {
|
if (path == "/") {
|
||||||
//quick shortcut
|
//quick shortcut
|
||||||
func = index_func;
|
handler_data = index_func;
|
||||||
} else {
|
} else {
|
||||||
std::string main_route = "";
|
std::string main_route = "";
|
||||||
|
|
||||||
@ -48,16 +50,16 @@ void Application::default_routing_middleware(Request *request) {
|
|||||||
|
|
||||||
main_route = path.substr(1, endpos - 1);
|
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);
|
send_error(404, request);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
request->handler_func = func;
|
request->handler_instance = handler_data;
|
||||||
request->next_stage();
|
request->next_stage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,11 +133,12 @@ Application *Application::get_instance() {
|
|||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::function<void(Request *)> Application::index_func = nullptr;
|
HandlerInstance Application::index_func;
|
||||||
std::map<std::string, std::function<void(Request *)> > Application::main_route_map;
|
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::map<int, std::function<void(int, Request *)> > Application::error_handler_map;
|
||||||
std::function<void(int, Request *)> Application::default_error_handler_func = nullptr;
|
std::function<void(int, Request *)> Application::default_error_handler_func = nullptr;
|
||||||
std::vector<std::function<void(Request *)> > Application::middlewares;
|
|
||||||
|
|
||||||
Application *Application::_instance = nullptr;
|
Application *Application::_instance = nullptr;
|
||||||
|
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
#ifndef APPLICATION_H
|
#ifndef APPLICATION_H
|
||||||
#define APPLICATION_H
|
#define APPLICATION_H
|
||||||
|
|
||||||
|
#include "object.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "handler_instance.h"
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
|
|
||||||
class Application {
|
class Application {
|
||||||
@ -23,18 +26,20 @@ public:
|
|||||||
virtual void setup_routes();
|
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();
|
Application();
|
||||||
virtual ~Application();
|
virtual ~Application();
|
||||||
|
|
||||||
static Application *get_instance();
|
static Application *get_instance();
|
||||||
|
|
||||||
static std::function<void(Request *)> index_func;
|
public:
|
||||||
static std::map<std::string, std::function<void(Request *)> > main_route_map;
|
static HandlerInstance index_func;
|
||||||
|
static std::map<std::string, HandlerInstance> main_route_map;
|
||||||
|
static std::vector<HandlerInstance> middlewares;
|
||||||
|
|
||||||
static std::map<int, std::function<void(int, Request *)> > error_handler_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::function<void(int, Request *)> default_error_handler_func;
|
||||||
static std::vector<std::function<void(Request *)> > middlewares;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Application *_instance;
|
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() {
|
void Request::next_stage() {
|
||||||
if (current_middleware_index == (*middleware_stack).size()) {
|
if (current_middleware_index == (*middleware_stack).size()) {
|
||||||
handler_func(this);
|
handler_instance.handler_func(handler_instance.instance, this);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
(*middleware_stack)[current_middleware_index++](this);
|
const HandlerInstance &hi = (*middleware_stack)[current_middleware_index++];
|
||||||
|
|
||||||
|
hi.handler_func(hi.instance, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::send() {
|
void Request::send() {
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
#include <brynet/net/http/HttpFormat.hpp>
|
#include <brynet/net/http/HttpFormat.hpp>
|
||||||
#include <brynet/net/http/HttpService.hpp>
|
#include <brynet/net/http/HttpService.hpp>
|
||||||
|
|
||||||
|
#include "handler_instance.h"
|
||||||
|
|
||||||
using namespace brynet;
|
using namespace brynet;
|
||||||
using namespace brynet::net;
|
using namespace brynet::net;
|
||||||
using namespace brynet::net::http;
|
using namespace brynet::net::http;
|
||||||
@ -18,8 +20,8 @@ public:
|
|||||||
HttpResponse *response;
|
HttpResponse *response;
|
||||||
|
|
||||||
uint32_t current_middleware_index;
|
uint32_t current_middleware_index;
|
||||||
std::function<void(Request *)> handler_func;
|
HandlerInstance handler_instance;
|
||||||
std::vector<std::function<void(Request *)> > *middleware_stack;
|
std::vector<HandlerInstance> *middleware_stack;
|
||||||
|
|
||||||
void next_stage();
|
void next_stage();
|
||||||
void send();
|
void send();
|
||||||
|
@ -6,7 +6,9 @@
|
|||||||
|
|
||||||
#include "core/file_cache.h"
|
#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;
|
std::string body;
|
||||||
|
|
||||||
if (FileCache::get_singleton()->get_cached_body("index", &body)) {
|
if (FileCache::get_singleton()->get_cached_body("index", &body)) {
|
||||||
@ -23,7 +25,7 @@ void RDNApplication::index(Request *request) {
|
|||||||
request->send();
|
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;
|
std::cout << "test: session_middleware_func called" << std::endl;
|
||||||
|
|
||||||
//if fail
|
//if fail
|
||||||
@ -35,9 +37,9 @@ void RDNApplication::session_middleware_func(Request *request) {
|
|||||||
void RDNApplication::setup_routes() {
|
void RDNApplication::setup_routes() {
|
||||||
Application::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() {
|
void RDNApplication::setup_middleware() {
|
||||||
|
@ -3,12 +3,13 @@
|
|||||||
|
|
||||||
#include "core/application.h"
|
#include "core/application.h"
|
||||||
#include "core/theme.h"
|
#include "core/theme.h"
|
||||||
|
#include "core/object.h"
|
||||||
|
|
||||||
class RDNApplication : public Application {
|
class RDNApplication : public Application {
|
||||||
public:
|
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_routes();
|
||||||
virtual void setup_middleware();
|
virtual void setup_middleware();
|
||||||
|
Loading…
Reference in New Issue
Block a user