Implement Themes, and fix middlewares.

This commit is contained in:
Relintai 2020-11-25 21:00:22 +01:00
parent 7f4adcd13d
commit 19fa2b5e67
9 changed files with 154 additions and 67 deletions

View File

@ -1,3 +1,3 @@
g++ -o3 -g main.cpp ./core/settings.cpp ./core/request.cpp ./core/http_server.cpp ./core/file_cache.cpp ./core/application.cpp ./rdn_application.cpp \
./themes/theme.cpp \
./core/theme.cpp \
-o ./bin/server -Ilibs -lpthread -std=c++11

View File

@ -11,7 +11,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
void Application::setup_routes() {
default_error_handler_func = Application::default_fallback_error_handler;
@ -70,7 +69,7 @@ void Application::handle_request(Request *request) {
return;
}
for (uint32_t i; i < middlewares.size(); ++i) {
for (uint32_t i = 0; i < middlewares.size(); ++i) {
middlewares[i](request);
if (request->finalized)

49
core/theme.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "theme.h"
Theme::Theme() {
}
Theme::~Theme() {
}
THEME_CORE_STATIC_DEFINES(SiteTheme);
void SiteTheme::add_header(Request *request, std::string *output) {
output->append(
"<html>"
"<head>"
"</head>"
"<body>");
}
void SiteTheme::add_footer(Request *request, std::string *output) {
output->append(
"</body>"
"</html>");
}
void SiteTheme::render_index_page(Request *request, std::string *output) {
add_header(request, output);
output->append("<p>Test HTML Body</p>");
add_footer(request, output);
}
SiteTheme::SiteTheme() {
}
SiteTheme::~SiteTheme() {
}
TestSiteTheme::TestSiteTheme() {
}
TestSiteTheme::~TestSiteTheme() {
}
TestSiteTheme2::TestSiteTheme2() {
}
TestSiteTheme2::~TestSiteTheme2() {
}

76
core/theme.h Normal file
View File

@ -0,0 +1,76 @@
#ifndef THEME_H
#define THEME_H
#include "request.h"
#include <map>
#include <string>
#define THEME_CORE(_class_name) \
public: \
std::string theme_name; \
static _class_name *default_theme; \
static std::map<std::string, _class_name *> theme_map; \
\
void register_theme(const std::string &p_theme_name) { \
theme_name = p_theme_name; \
theme_map[theme_name] = this; \
} \
\
void set_theme_as_default() { \
default_theme = this; \
} \
\
void register_theme() { \
register_theme(#_class_name); \
} \
\
void unregister_theme() { \
theme_map.erase(theme_name); \
if (default_theme == this) \
default_theme = nullptr; \
} \
\
_class_name *get_theme(const std::string &p_theme_name) { \
_class_name *t = theme_map[p_theme_name]; \
if (t) \
return t; \
else \
return default_theme; \
}
#define THEME_CORE_STATIC_DEFINES(_class_name) \
_class_name *_class_name::default_theme = nullptr; \
std::map<std::string, _class_name *> _class_name::theme_map;
class Theme {
public:
Theme();
virtual ~Theme();
};
class SiteTheme : public Theme {
THEME_CORE(SiteTheme);
public:
virtual void add_header(Request *request, std::string *output);
virtual void add_footer(Request *request, std::string *output);
virtual void render_index_page(Request *request, std::string *output);
SiteTheme();
~SiteTheme();
};
class TestSiteTheme : public SiteTheme {
public:
TestSiteTheme();
~TestSiteTheme();
};
class TestSiteTheme2 : public SiteTheme {
public:
TestSiteTheme2();
~TestSiteTheme2();
};
#endif

View File

@ -17,6 +17,7 @@ int main(int argc, char **argv) {
Application *app = new MAIN_CLASS();
app->setup_routes();
app->setup_middleware();
HTTPServer *server = new HTTPServer();

View File

@ -15,7 +15,7 @@ void RDNApplication::index(Request *request) {
return;
}
body = "<html>hello world aaaaa </html>";
SiteTheme::default_theme->render_index_page(request, &body);
FileCache::get_singleton()->set_cached_body("index", body);
@ -23,7 +23,7 @@ void RDNApplication::index(Request *request) {
}
void RDNApplication::session_middleware_func(Request *request) {
std::cout << "dddd" << std::endl;
std::cout << "test: session_middleware_func called" << std::endl;
}
void RDNApplication::setup_routes() {
@ -40,9 +40,24 @@ void RDNApplication::setup_middleware() {
middlewares.push_back(RDNApplication::session_middleware_func);
}
RDNApplication::RDNApplication() :
Application() {
RDNApplication::RDNApplication() : Application() {
SiteTheme *t = new TestSiteTheme();
t->register_theme();
t->set_theme_as_default();
themes.push_back(t);
t = new TestSiteTheme2();
t->register_theme();
themes.push_back(t);
}
RDNApplication::~RDNApplication() {
for (uint32_t i = 0; i < themes.size(); ++i) {
delete themes[i];
}
themes.clear();
}

View File

@ -2,6 +2,7 @@
#define RDN_APPLICATION_H
#include "core/application.h"
#include "core/theme.h"
class RDNApplication : public Application {
public:
@ -14,6 +15,8 @@ public:
RDNApplication();
~RDNApplication();
std::vector<Theme *> themes;
};
#endif

View File

@ -1,9 +0,0 @@
#include "theme.h"
Theme::Theme() {
}
Theme::~Theme() {
}

View File

@ -1,47 +0,0 @@
#ifndef THEME_H
#define THEME_H
#include<map>
#include<string>
/*
class ThemeMap {
public:
THEME_CLASS *current_theme;
std::map<std::string, THEME_CLASS> theme_map;
ThemeMap() {
_instance = this;
}
~ThemeMap() {
_instance = nullptr;
}
static ThemeMap *get_singleton() {
return _instance;
}
private:
static ThemeMap *_instance;
};
ThemeMap *ThemeMap::_instance = nullptr;
*/
class Theme {
public:
Theme();
virtual ~Theme();
};
//forumtheme
//sitetheme
//...
//all singleton
//not templated
//and a theme map
#endif