diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2021-10-01 17:59:24 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2021-10-01 17:59:24 +0200 |
commit | 5ff3d7a51be30a0052b12f9330fdf54f3c104739 (patch) | |
tree | 07f6780c4196205fe70b4caf6486b13fba80e30f | |
parent | 2186064e52ec4029fd060323b51de973da4cee5f (diff) |
Added Markdown and Static content module.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/Content.hpp | 2 | ||||
-rw-r--r-- | src/ContentManager.cpp | 34 | ||||
-rw-r--r-- | src/ContentManager.hpp | 3 | ||||
-rw-r--r-- | src/EventManager.cpp | 9 | ||||
-rw-r--r-- | src/content/blog/Blog.cpp | 32 | ||||
-rw-r--r-- | src/content/blog/Blog.hpp | 9 | ||||
-rw-r--r-- | src/content/markdown/Markdown.cpp | 51 | ||||
-rw-r--r-- | src/content/markdown/Markdown.hpp | 28 | ||||
-rw-r--r-- | src/content/static/Static.cpp | 51 | ||||
-rw-r--r-- | src/content/static/Static.hpp | 29 | ||||
-rw-r--r-- | src/main.cpp | 38 |
11 files changed, 249 insertions, 37 deletions
diff --git a/src/Content.hpp b/src/Content.hpp index 82eb35e..18c3d70 100644 --- a/src/Content.hpp +++ b/src/Content.hpp @@ -17,7 +17,7 @@ public: virtual bool Init() = 0; virtual void Shutdown() = 0; - virtual bool Render(RequestResponse & rr, RenderData & rd) = 0; + virtual bool Render(RequestResponse & rr, RenderData & rd, std::string & out) = 0; virtual std::string const & GetUriBasePath() const = 0; virtual std::string const & GetMainTemplate() const = 0; diff --git a/src/ContentManager.cpp b/src/ContentManager.cpp index 6beef5a..ace325e 100644 --- a/src/ContentManager.cpp +++ b/src/ContentManager.cpp @@ -1,10 +1,5 @@ #include "ContentManager.hpp" -void ContentManager::SetStaticFilesystem(std::shared_ptr<Filesystem> & static_fs) -{ - m_StaticFilesystem = static_fs; -} - void ContentManager::SetTemplateSystem(std::shared_ptr<TemplateManager> & tmgr) { m_TemplateManager = tmgr; @@ -18,7 +13,7 @@ bool ContentManager::RegisterModule(std::shared_ptr<Content> ctnt) m_ContentModules[basePath] = ctnt; for (auto & redirect : rs) { - m_ContentModules[redirect] = ctnt; + m_ContentModulesRoutes[redirect] = ctnt; } return false; @@ -57,26 +52,40 @@ void ContentManager::ShutdownAll(void) } } + m_ContentModulesRoutes.clear(); m_ContentModules.clear(); } bool ContentManager::Render(char const * basePath, RequestResponse & rr, std::string & out) { + std::shared_ptr<Content> cntm; + if (m_ContentModules.find(basePath) == m_ContentModules.end()) { - return false; + if (m_ContentModulesRoutes.find(basePath) == m_ContentModulesRoutes.end()) + { + return false; + } + else + { + cntm = m_ContentModulesRoutes[basePath]; + } + } + else + { + cntm = m_ContentModules[basePath]; } RenderData rd; - auto & cntm = m_ContentModules[basePath]; auto & main = cntm->GetMainTemplate(); - if (m_ContentModules[basePath]->Render(rr, rd) == false) + out.clear(); + if (cntm->Render(rr, rd, out) == false) { return false; } - if (m_TemplateManager->RenderTemplate(main, rd, out) == false) + if (main.empty() == false && out.empty() == true && m_TemplateManager->RenderTemplate(main, rd, out) == false) { return false; } @@ -88,3 +97,8 @@ ContentModules const & ContentManager::GetAllModules() const { return m_ContentModules; } + +ContentModules const & ContentManager::GetAllModulesRoutes() const +{ + return m_ContentModulesRoutes; +} diff --git a/src/ContentManager.hpp b/src/ContentManager.hpp index 83f4bc9..9ae7271 100644 --- a/src/ContentManager.hpp +++ b/src/ContentManager.hpp @@ -28,11 +28,12 @@ public: void ShutdownAll(void); bool Render(char const * basePath, RequestResponse & rr, std::string & out); ContentModules const & GetAllModules() const; + ContentModules const & GetAllModulesRoutes() const; private: - std::shared_ptr<Filesystem> m_StaticFilesystem; std::shared_ptr<TemplateManager> m_TemplateManager; ContentModules m_ContentModules; + ContentModules m_ContentModulesRoutes; }; #endif diff --git a/src/EventManager.cpp b/src/EventManager.cpp index 3270297..bc4b5cb 100644 --- a/src/EventManager.cpp +++ b/src/EventManager.cpp @@ -133,7 +133,9 @@ bool EventManager::Init(std::string host, uint16_t port) return false; } +#if 0 event_enable_debug_logging(EVENT_DBG_ALL); +#endif cfg = event_config_new(); event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP); @@ -170,6 +172,13 @@ bool EventManager::Init(std::string host, uint16_t port) return false; } } + for (auto & cm : m_ContentManager->GetAllModulesRoutes()) + { + if (evhttp_set_cb(m_EvHttp, cm.first.c_str(), EvContentManagerInterceptor, &m_ContentManager) != 0) + { + return false; + } + } evhttp_set_gencb(m_EvHttp, EvGenericInterceptor, &m_DefaultCallback); m_EvSocket = evhttp_bind_socket_with_handle(m_EvHttp, host.c_str(), port); diff --git a/src/content/blog/Blog.cpp b/src/content/blog/Blog.cpp index 56a23c1..b473bf3 100644 --- a/src/content/blog/Blog.cpp +++ b/src/content/blog/Blog.cpp @@ -1,7 +1,11 @@ #include "Blog.hpp" -Blog::Blog(std::string uriBasePath, std::string mainTemplatePath) - : Content(), m_UriBasePath(uriBasePath), m_MainTemplatePath(mainTemplatePath) +Blog::Blog(std::string uriBasePath, std::string mainTemplatePath, std::string blogPath) + : Content(), + m_UriBasePath(uriBasePath), + m_MainTemplatePath(mainTemplatePath), + m_BlogPath(blogPath), + m_BlogEntries("", blogPath) { m_Redirections.push_back(uriBasePath + "/"); m_Redirections.push_back(uriBasePath + "/index.html"); @@ -9,18 +13,34 @@ Blog::Blog(std::string uriBasePath, std::string mainTemplatePath) bool Blog::Init() { + std::cout << "Blog entries path: " << m_BlogPath << std::endl; + + std::vector<std::string> extensions = {"json"}; + + if (m_BlogEntriesMetadata.Scan(m_BlogPath, extensions, false) == false) + { + return false; + } + + m_BlogEntries.Init(); + return true; } void Blog::Shutdown() { + std::cout << "Blog module shutdown" << std::endl; + + m_BlogEntries.Shutdown(); } -bool Blog::Render(RequestResponse & rr, RenderData & rd) +bool Blog::Render(RequestResponse & rr, RenderData & rd, std::string & out) { - rd["blah"] = "Yoh!"; - rr.UseOutputHeader(); - rr.AddOutputHeader("bla", "blubb"); + (void)rr; + (void)rd; + (void)out; + + rd["blah"] = "Yooooh!"; return true; } diff --git a/src/content/blog/Blog.hpp b/src/content/blog/Blog.hpp index 086acc7..970c7fd 100644 --- a/src/content/blog/Blog.hpp +++ b/src/content/blog/Blog.hpp @@ -2,15 +2,17 @@ #define BLOG_H 1 #include "../../Content.hpp" +#include "../../Filesystem.hpp" +#include "../markdown/Markdown.hpp" class Blog : public Content { public: - explicit Blog(std::string uriBasePath, std::string mainTemplatePath); + explicit Blog(std::string uriBasePath, std::string mainTemplatePath, std::string blogPath); bool Init(); void Shutdown(); - bool Render(RequestResponse & rr, RenderData & rd); + bool Render(RequestResponse & rr, RenderData & rd, std::string & out); std::string const & GetUriBasePath() const; std::string const & GetMainTemplate() const; @@ -19,7 +21,10 @@ public: private: std::string m_UriBasePath; std::string m_MainTemplatePath; + std::string m_BlogPath; Redirections m_Redirections; + Filesystem m_BlogEntriesMetadata; + Markdown m_BlogEntries; }; #endif diff --git a/src/content/markdown/Markdown.cpp b/src/content/markdown/Markdown.cpp new file mode 100644 index 0000000..2009772 --- /dev/null +++ b/src/content/markdown/Markdown.cpp @@ -0,0 +1,51 @@ +#include "Markdown.hpp" + +Markdown::Markdown(std::string uriBasePath, std::string markdownFilesPath) + : Content(), m_UriBasePath(uriBasePath), m_MainTemplatePath(""), m_MarkdownFilesPath(markdownFilesPath) +{ +} + +bool Markdown::Init() +{ + std::cout << "Markdown files path: " << m_MarkdownFilesPath << std::endl; + + std::vector<std::string> extensions = {"md"}; + + if (m_MarkdownFiles.Scan(m_MarkdownFilesPath, extensions, false) == false) + { + return false; + } + + return true; +} + +void Markdown::Shutdown() +{ + std::cout << "Markdown module shutdown" << std::endl; +} + +bool Markdown::Render(RequestResponse & rr, RenderData & rd, std::string & out) +{ + (void)out; + + rd["blub"] = "Yoh21!"; + rr.UseOutputHeader(); + rr.AddOutputHeader("blaaaa", "blubb"); + + return true; +} + +std::string const & Markdown::GetUriBasePath() const +{ + return m_UriBasePath; +} + +std::string const & Markdown::GetMainTemplate() const +{ + return m_MainTemplatePath; +} + +Redirections const & Markdown::GetRedirections() const +{ + return m_Redirections; +} diff --git a/src/content/markdown/Markdown.hpp b/src/content/markdown/Markdown.hpp new file mode 100644 index 0000000..41c8b4d --- /dev/null +++ b/src/content/markdown/Markdown.hpp @@ -0,0 +1,28 @@ +#ifndef MARKDOWN_H +#define MARKDOWN_H 1 + +#include "../../Content.hpp" +#include "../../Filesystem.hpp" + +class Markdown : public Content +{ +public: + explicit Markdown(std::string uriBasePath, std::string markdownFilesPath); + + bool Init(); + void Shutdown(); + bool Render(RequestResponse & rr, RenderData & rd, std::string & out); + + std::string const & GetUriBasePath() const; + std::string const & GetMainTemplate() const; + Redirections const & GetRedirections() const; + +private: + std::string m_UriBasePath; + std::string m_MainTemplatePath; + std::string m_MarkdownFilesPath; + Redirections m_Redirections; + Filesystem m_MarkdownFiles; +}; + +#endif diff --git a/src/content/static/Static.cpp b/src/content/static/Static.cpp new file mode 100644 index 0000000..5e38b76 --- /dev/null +++ b/src/content/static/Static.cpp @@ -0,0 +1,51 @@ +#include "Static.hpp" + +Static::Static(std::string uriBasePath, std::string staticFilesPath) + : Content(), m_UriBasePath(uriBasePath), m_MainTemplatePath(""), m_StaticFilesPath(staticFilesPath) +{ +} + +bool Static::Init() +{ + std::cout << "Static files path: " << m_StaticFilesPath << std::endl; + + std::vector<std::string> extensions = {"json"}; + + if (m_StaticFiles.Scan(m_StaticFilesPath, extensions, false) == false) + { + return false; + } + + return true; +} + +void Static::Shutdown() +{ + std::cout << "Static files module shutdown" << std::endl; +} + +bool Static::Render(RequestResponse & rr, RenderData & rd, std::string & out) +{ + (void)rr; + (void)rd; + (void)out; + + rd["blah"] = "Yooooh!"; + + return true; +} + +std::string const & Static::GetUriBasePath() const +{ + return m_UriBasePath; +} + +std::string const & Static::GetMainTemplate() const +{ + return m_MainTemplatePath; +} + +Redirections const & Static::GetRedirections() const +{ + return m_Redirections; +} diff --git a/src/content/static/Static.hpp b/src/content/static/Static.hpp new file mode 100644 index 0000000..85ffd8c --- /dev/null +++ b/src/content/static/Static.hpp @@ -0,0 +1,29 @@ +#ifndef STATIC_H +#define STATIC_H 1 + +#include "../../Content.hpp" +#include "../../Filesystem.hpp" +#include "../markdown/Markdown.hpp" + +class Static : public Content +{ +public: + explicit Static(std::string uriBasePath, std::string staticFilesPath); + + bool Init(); + void Shutdown(); + bool Render(RequestResponse & rr, RenderData & rd, std::string & out); + + std::string const & GetUriBasePath() const; + std::string const & GetMainTemplate() const; + Redirections const & GetRedirections() const; + +private: + std::string m_UriBasePath; + std::string m_MainTemplatePath; + std::string m_StaticFilesPath; + Redirections m_Redirections; + Filesystem m_StaticFiles; +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 98f19c5..6d41907 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,33 +88,37 @@ int main(int argc, char ** argv) port = atoi(argv[2]); } - std::shared_ptr<Filesystem> static_fs = std::make_shared<Filesystem>(); - if (argc > 3) + std::shared_ptr<TemplateManager> tmgr = std::make_shared<TemplateManager>(); + std::shared_ptr<ContentManager> ctmgr = std::make_shared<ContentManager>(); + ctmgr->SetTemplateSystem(tmgr); + { - if (static_fs->Scan(argv[3]) != true) + Filesystem static_fs; + if (argc > 3) { - return 1; + if (static_fs.Scan(argv[3]) != true) + { + return 1; + } } + static_fs.Scan("./wwwroot", {"html", "tmpl"}, true); } - static_fs->Scan("./wwwroot", {"html", "tmpl"}, true); - Filesystem dynamic_fs; - if (argc > 4) { - if (dynamic_fs.Scan(argv[4]) != true) + Filesystem dynamic_fs; + if (argc > 4) { - return 1; + if (dynamic_fs.Scan(argv[4]) != true) + { + return 1; + } } - } - dynamic_fs.Scan("./wwwroot", {"html", "tmpl"}, false); + dynamic_fs.Scan("./wwwroot", {"html", "tmpl"}, false); - std::shared_ptr<TemplateManager> tmgr = std::make_shared<TemplateManager>(); - tmgr->ParseTemplates(dynamic_fs); + tmgr->ParseTemplates(dynamic_fs); + } - std::shared_ptr<ContentManager> ctmgr = std::make_shared<ContentManager>(); - ctmgr->SetStaticFilesystem(static_fs); - ctmgr->SetTemplateSystem(tmgr); - ctmgr->RegisterModule(std::make_shared<Blog>("/blog", "index.html")); + ctmgr->RegisterModule(std::make_shared<Blog>("/blog", "index.html", "./blog")); if (ctmgr->InitAll() == false) { |