2021-11-21 14:41:40 +01:00
|
|
|
#include "wp_application.h"
|
|
|
|
|
|
|
|
#include "core/http/request.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "core/file_cache.h"
|
|
|
|
|
|
|
|
#include "core/http/handler_instance.h"
|
|
|
|
|
|
|
|
#include "core/database/database_manager.h"
|
|
|
|
|
|
|
|
#include "core/html/html_builder.h"
|
|
|
|
#include "core/http/http_session.h"
|
|
|
|
#include "core/http/session_manager.h"
|
|
|
|
|
|
|
|
void WPApplication::index(Object *instance, Request *request) {
|
|
|
|
|
|
|
|
request->body += "test";
|
|
|
|
request->compile_and_send_body();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::blog(Object *instance, Request *request) {
|
2021-11-21 14:55:34 +01:00
|
|
|
request->body += "test blog";
|
2021-11-21 14:41:40 +01:00
|
|
|
request->compile_and_send_body();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::routing_middleware(Object *instance, Request *request) {
|
2021-11-21 14:55:34 +01:00
|
|
|
String path = request->get_path_full();
|
2021-11-21 14:41:40 +01:00
|
|
|
|
|
|
|
WPApplication *app = Object::cast_to<WPApplication>(instance);
|
|
|
|
|
|
|
|
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
|
|
|
app->send_file(path, request);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:55:34 +01:00
|
|
|
bool handled = false;
|
|
|
|
|
2021-11-21 14:41:40 +01:00
|
|
|
if (request->get_path_segment_count() == 0) {
|
2021-11-21 14:55:34 +01:00
|
|
|
handled = true;
|
|
|
|
|
2021-11-21 14:50:03 +01:00
|
|
|
request->handler_instance = app->index_func;
|
2021-11-21 14:41:40 +01:00
|
|
|
} else {
|
2021-11-21 14:55:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
const String main_route = request->get_current_path_segment();
|
2021-11-21 14:41:40 +01:00
|
|
|
|
|
|
|
request->push_path();
|
|
|
|
|
|
|
|
if (main_route == "blog") {
|
2021-11-21 14:55:34 +01:00
|
|
|
handled = true;
|
2021-11-21 14:50:03 +01:00
|
|
|
request->handler_instance = app->blog_func;
|
2021-11-21 14:41:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:55:34 +01:00
|
|
|
if (!handled) {
|
2021-11-21 14:41:40 +01:00
|
|
|
app->send_error(404, request);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->next_stage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::setup_routes() {
|
|
|
|
DWebApplication::setup_routes();
|
|
|
|
|
2021-11-21 14:50:03 +01:00
|
|
|
index_func = HandlerInstance(index, this);
|
|
|
|
blog_func = HandlerInstance(blog, this);
|
2021-11-21 14:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::setup_middleware() {
|
|
|
|
middlewares.push_back(HandlerInstance(routing_middleware, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::migrate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::compile_menu() {
|
|
|
|
HTMLBuilder bh;
|
|
|
|
|
|
|
|
bh.meta()->charset_utf_8();
|
|
|
|
bh.title();
|
2021-11-21 14:50:03 +01:00
|
|
|
bh.w("WPSaver");
|
2021-11-21 14:41:40 +01:00
|
|
|
bh.ctitle();
|
|
|
|
|
2021-11-21 14:50:03 +01:00
|
|
|
bh.link()->rel_stylesheet()->href("site.css");
|
2021-11-21 14:41:40 +01:00
|
|
|
bh.write_tag();
|
|
|
|
|
2021-11-21 14:50:03 +01:00
|
|
|
header = bh.result;
|
2021-11-21 14:41:40 +01:00
|
|
|
|
|
|
|
HTMLBuilder bf;
|
|
|
|
|
|
|
|
bf.cdiv();
|
|
|
|
bf.footer();
|
|
|
|
bf.cfooter();
|
|
|
|
|
|
|
|
footer = bf.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
WPApplication::WPApplication() :
|
|
|
|
DWebApplication() {
|
|
|
|
|
|
|
|
compile_menu();
|
|
|
|
}
|
|
|
|
|
|
|
|
WPApplication::~WPApplication() {
|
|
|
|
}
|
|
|
|
|