From 19fa2b5e67bcd2aa798c71a770f64b7ceaded8c7 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 25 Nov 2020 21:00:22 +0100 Subject: [PATCH] Implement Themes, and fix middlewares. --- compile_linux.sh | 2 +- core/application.cpp | 7 ++-- core/theme.cpp | 49 ++++++++++++++++++++++++++++ core/theme.h | 76 ++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 1 + rdn_application.cpp | 27 ++++++++++++---- rdn_application.h | 3 ++ themes/theme.cpp | 9 ------ themes/theme.h | 47 --------------------------- 9 files changed, 154 insertions(+), 67 deletions(-) create mode 100644 core/theme.cpp create mode 100644 core/theme.h delete mode 100644 themes/theme.cpp delete mode 100644 themes/theme.h diff --git a/compile_linux.sh b/compile_linux.sh index 97d9607..7b669d0 100755 --- a/compile_linux.sh +++ b/compile_linux.sh @@ -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 diff --git a/core/application.cpp b/core/application.cpp index 0c8fa81..254a943 100644 --- a/core/application.cpp +++ b/core/application.cpp @@ -11,7 +11,6 @@ #include #include -#include 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) @@ -93,7 +92,7 @@ void Application::send_error(int error_code, Request *request) { void Application::send_file(const std::string &path, Request *request) { std::string fp = FileCache::get_singleton()->wwwroot + path; - + FILE *f = fopen(fp.c_str(), "rb"); if (!f) { @@ -105,7 +104,7 @@ void Application::send_file(const std::string &path, Request *request) { fseek(f, 0, SEEK_END); long fsize = ftell(f); - fseek(f, 0, SEEK_SET); /* same as rewind(f); */ + fseek(f, 0, SEEK_SET); /* same as rewind(f); */ std::string body; body.resize(fsize); diff --git a/core/theme.cpp b/core/theme.cpp new file mode 100644 index 0000000..07e632f --- /dev/null +++ b/core/theme.cpp @@ -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( + "" + "" + "" + ""); +} + +void SiteTheme::add_footer(Request *request, std::string *output) { + output->append( + "" + ""); +} + +void SiteTheme::render_index_page(Request *request, std::string *output) { + add_header(request, output); + + output->append("

Test HTML Body

"); + + add_footer(request, output); +} + +SiteTheme::SiteTheme() { +} + +SiteTheme::~SiteTheme() { +} + +TestSiteTheme::TestSiteTheme() { +} + +TestSiteTheme::~TestSiteTheme() { +} + +TestSiteTheme2::TestSiteTheme2() { +} + +TestSiteTheme2::~TestSiteTheme2() { +} diff --git a/core/theme.h b/core/theme.h new file mode 100644 index 0000000..f12fa7f --- /dev/null +++ b/core/theme.h @@ -0,0 +1,76 @@ +#ifndef THEME_H +#define THEME_H + +#include "request.h" +#include +#include + +#define THEME_CORE(_class_name) \ +public: \ + std::string theme_name; \ + static _class_name *default_theme; \ + static std::map 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 _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 \ No newline at end of file diff --git a/main.cpp b/main.cpp index 2d47b66..0df776f 100644 --- a/main.cpp +++ b/main.cpp @@ -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(); diff --git a/rdn_application.cpp b/rdn_application.cpp index b3ed9b5..0c05c1f 100644 --- a/rdn_application.cpp +++ b/rdn_application.cpp @@ -12,18 +12,18 @@ void RDNApplication::index(Request *request) { if (FileCache::get_singleton()->get_cached_body("index", &body)) { request->response->setBody(body); - return; + return; } - body = "hello world aaaaa "; + SiteTheme::default_theme->render_index_page(request, &body); - FileCache::get_singleton()->set_cached_body("index", body); + FileCache::get_singleton()->set_cached_body("index", body); request->response->setBody(body); } 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(); } \ No newline at end of file diff --git a/rdn_application.h b/rdn_application.h index 8956a83..6754233 100644 --- a/rdn_application.h +++ b/rdn_application.h @@ -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 themes; }; #endif \ No newline at end of file diff --git a/themes/theme.cpp b/themes/theme.cpp deleted file mode 100644 index 2476bdc..0000000 --- a/themes/theme.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "theme.h" - -Theme::Theme() { - -} - -Theme::~Theme() { - -} diff --git a/themes/theme.h b/themes/theme.h deleted file mode 100644 index 67acf39..0000000 --- a/themes/theme.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef THEME_H -#define THEME_H - -#include -#include - -/* -class ThemeMap { -public: - THEME_CLASS *current_theme; - std::map 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 \ No newline at end of file