rsgit /cpp-webA hyper-hyperfast web frontend for git repositories written in Rust.
tree/src/ContentManager.cpp
ContentManager.cpp149 lines · 3.4 KiB
1#include "ContentManager.hpp"
2
3void ContentManager::SetTemplateSystem(std::shared_ptr<TemplateManager> const & tmgr)
4{
5 m_TemplateManager = tmgr;
6}
7
8bool ContentManager::RegisterModule(std::shared_ptr<Content> ctnt)
9{
10 std::string & basePath = ctnt->GetUriBasePath();
11
12 ContentManager::RemoveGarbageSlashes(basePath);
13 if (basePath.empty() == true)
14 {
15 return false;
16 }
17#if 0
18 std::cout << "Base URI: " << basePath << std::endl;
19#endif
20 m_ContentModules[basePath] = ctnt;
21
22 if (basePath.back() == '/')
23 {
24 basePath.pop_back();
25 }
26
27 return true;
28}
29
30bool ContentManager::InitAll(void)
31{
32 bool ret = true;
33
34 for (auto & content : m_ContentModules)
35 {
36 if (content.second->Init() == false)
37 {
38 ret = false;
39 }
40
41 Redirections & rs = content.second->GetRedirections();
42 for (auto & redirect : rs)
43 {
44 ContentManager::RemoveGarbageSlashes(redirect);
45 if (redirect == content.second->GetUriBasePath())
46 {
47 continue;
48 }
49 if (m_ContentModulesRoutes.find(redirect) != m_ContentModulesRoutes.end())
50 {
51 std::cerr << "Redirect URI already exists: " << redirect << std::endl;
52 }
53 else
54 {
55#if 0
56 std::cout << "Redirect URI: " << redirect << std::endl;
57#endif
58 }
59 m_ContentModulesRoutes[redirect] = content.second;
60 }
61 }
62
63 return ret;
64}
65
66void ContentManager::ShutdownAll(void)
67{
68 std::unordered_map<std::shared_ptr<Content>, bool> shutdownModules;
69
70 for (auto & content : m_ContentModules)
71 {
72 auto const & search = shutdownModules.find(content.second);
73 if (search != shutdownModules.end())
74 {
75 continue;
76 }
77 else
78 {
79 content.second->Shutdown();
80 shutdownModules[content.second] = true;
81 }
82 }
83
84 m_ContentModulesRoutes.clear();
85 m_ContentModules.clear();
86}
87
88bool ContentManager::Render(char const * basePath, RequestResponse & rr, std::string & out)
89{
90 std::shared_ptr<Content> cntm;
91
92 if (m_ContentModules.find(basePath) == m_ContentModules.end())
93 {
94 if (m_ContentModulesRoutes.find(basePath) == m_ContentModulesRoutes.end())
95 {
96 return false;
97 }
98 else
99 {
100 cntm = m_ContentModulesRoutes[basePath];
101 }
102 }
103 else
104 {
105 cntm = m_ContentModules[basePath];
106 }
107
108 auto & main = cntm->GetMainTemplate();
109
110 out.clear();
111 RenderData rd;
112 if (cntm->Render(rr, rd, out) == false)
113 {
114 return false;
115 }
116
117 if (main.empty() == false && out.empty() == true && m_TemplateManager->RenderTemplate(main, rd, out) == false)
118 {
119 return false;
120 }
121
122 return true;
123}
124
125ContentModules const & ContentManager::GetAllModules() const
126{
127 return m_ContentModules;
128}
129
130ContentModules const & ContentManager::GetAllModulesRoutes() const
131{
132 return m_ContentModulesRoutes;
133}
134
135void ContentManager::RemoveGarbageSlashes(std::string & uri_basepath)
136{
137 size_t start_pos = 0;
138 static const std::string from = "//";
139 static const std::string to = "/";
140
141 if (from.empty())
142 return;
143 while ((start_pos = uri_basepath.find(from, start_pos)) != std::string::npos)
144 {
145 uri_basepath.replace(start_pos, from.length(), to);
146 start_pos += to.length();
147 }
148}
149