aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2021-04-27 15:15:40 +0200
committerToni Uhlig <matzeton@googlemail.com>2021-04-27 15:15:40 +0200
commitc0483779dac4d8fd5fcc736ae5ffa8ddede0e511 (patch)
tree42408069a518e15eb1052cc62a09d6855517cf5b
parent51d779256c74cc541da306db5629ed510df5a944 (diff)
ContentManager / Filesystem classes as preparation for a SimpleBlog.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--CMakeLists.txt4
-rw-r--r--src/Content.hpp9
-rw-r--r--src/ContentManager.cpp1
-rw-r--r--src/ContentManager.hpp14
-rw-r--r--src/Filesystem.cpp61
-rw-r--r--src/Filesystem.hpp25
-rw-r--r--src/main.cpp13
-rw-r--r--wwwroot/index.html31
8 files changed, 156 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index cb16281..376875d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,7 +6,7 @@ if(NOT CMAKE_BUILD_TYPE)
endif()
set(CMAKE_CXX_FLAGS "-Wall -Wextra -std=c++17")
-set(CMAKE_CXX_FLAGS_DEBUG "-g")
+set(CMAKE_CXX_FLAGS_DEBUG "-g3")
set(CMAKE_CXX_FLAGS_RELEASE "-Os -fno-pic -fomit-frame-pointer -flto -fno-rtti -ffunction-sections -fdata-sections -fsanitize=address -fsanitize=leak -fsanitize=leak")
set(CMAKE_EXE_LINKER_FLAGS "-flto")
@@ -22,4 +22,4 @@ add_executable(cpp-web ${SOURCES})
if(CMAKE_BUILD_TYPE MATCHES Release)
set_target_properties(cpp-web PROPERTIES LINK_FLAGS "-no-pie -flto -Wl,--gc-sections -Wl,-z,norelro -Wl,--hash-style=gnu -Wl,--build-id=none")
endif()
-target_link_libraries(cpp-web pthread event)
+target_link_libraries(cpp-web stdc++fs pthread event)
diff --git a/src/Content.hpp b/src/Content.hpp
new file mode 100644
index 0000000..c630568
--- /dev/null
+++ b/src/Content.hpp
@@ -0,0 +1,9 @@
+#ifndef CONTENT_H
+#define CONTENT_H 1
+
+class Content {
+public:
+ virtual bool Render();
+};
+
+#endif
diff --git a/src/ContentManager.cpp b/src/ContentManager.cpp
new file mode 100644
index 0000000..94bbb15
--- /dev/null
+++ b/src/ContentManager.cpp
@@ -0,0 +1 @@
+#include "ContentManager.hpp"
diff --git a/src/ContentManager.hpp b/src/ContentManager.hpp
new file mode 100644
index 0000000..8a7972a
--- /dev/null
+++ b/src/ContentManager.hpp
@@ -0,0 +1,14 @@
+#ifndef CONTENTMANAGER_H
+#define CONTENTMANAGER_H 1
+
+#include "Content.hpp"
+
+class ContentManager {
+public:
+ ContentManager() {}
+ ~ContentManager() {}
+
+ bool Register(Content ctnt);
+};
+
+#endif
diff --git a/src/Filesystem.cpp b/src/Filesystem.cpp
new file mode 100644
index 0000000..18b8725
--- /dev/null
+++ b/src/Filesystem.cpp
@@ -0,0 +1,61 @@
+#include "Filesystem.hpp"
+
+#include <cstring>
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+
+static std::string make_path_relative(std::string &path, std::string &root) {
+ return std::filesystem::relative(path, root);
+}
+
+bool Filesystem::AddSingleFile(std::string path, std::string root) {
+ std::ifstream ifs(path, std::ios::binary | std::ios::ate);
+
+ if (!ifs) {
+ return false;
+ }
+
+ auto end = ifs.tellg();
+ if (end <= 0) {
+ return false;
+ }
+ if (!ifs.seekg(0, std::ios::beg)) {
+ return false;
+ }
+
+ auto size = std::size_t(end - ifs.tellg());
+
+ if (size == 0) {
+ return false;
+ }
+
+ struct file_data fd = {};
+ try {
+ fd.data.reserve(size);
+ } catch (const std::exception &e) {
+ return false;
+ }
+
+ if (!ifs.read((char *)fd.data.data(), fd.data.size())) {
+ return false;
+ }
+
+ std::string relpath = make_path_relative(path, root);
+ if (files.count(relpath) > 0) {
+ std::cout << "Adding file: " << path << " and overwriting " << relpath
+ << std::endl;
+ } else {
+ std::cout << "Adding file: " << path << " as " << relpath << std::endl;
+ }
+ files[relpath] = fd;
+
+ return true;
+}
+
+bool Filesystem::Scan(std::string root) {
+ for (const auto &entry : std::filesystem::directory_iterator(root)) {
+ AddSingleFile(entry.path(), root);
+ }
+ return true;
+}
diff --git a/src/Filesystem.hpp b/src/Filesystem.hpp
new file mode 100644
index 0000000..139c214
--- /dev/null
+++ b/src/Filesystem.hpp
@@ -0,0 +1,25 @@
+#ifndef FILESYSTEM_H
+#define FILESYSTEM_H 1
+
+#include <map>
+#include <string>
+#include <vector>
+
+struct file_data {
+ bool inja_renderable;
+ std::vector<unsigned char> data;
+};
+
+class Filesystem {
+public:
+ Filesystem() {}
+ ~Filesystem() {}
+
+ bool AddSingleFile(std::string path, std::string root);
+ bool Scan(std::string root = "./wwwroot");
+
+private:
+ std::map<std::string, struct file_data> files;
+};
+
+#endif
diff --git a/src/main.cpp b/src/main.cpp
index 9b38c38..ea0f333 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,4 +1,5 @@
#include "EventManager.hpp"
+#include "Filesystem.hpp"
#include <event2/buffer.h>
@@ -64,6 +65,10 @@ int main(int argc, char **argv) {
char const *host = "127.0.0.1";
uint16_t port = 9000;
+ if (argc <= 1) {
+ std::cout << "usage: cpp-web [HOST] [PORT] [WWWROOTs..]" << std::endl;
+ }
+
if (argc > 1) {
host = argv[1];
}
@@ -71,6 +76,14 @@ int main(int argc, char **argv) {
port = atoi(argv[2]);
}
+ Filesystem fs;
+ for (auto i = 3; i < argc; ++i) {
+ if (fs.Scan(argv[i]) != true) {
+ return 1;
+ }
+ }
+ fs.Scan();
+
EventManager evmgr;
evmgr.setDefaultCallback(example_inja_render, {});
evmgr.Init(host, port);
diff --git a/wwwroot/index.html b/wwwroot/index.html
new file mode 100644
index 0000000..c0edd6c
--- /dev/null
+++ b/wwwroot/index.html
@@ -0,0 +1,31 @@
+<html><body>
+ Hello {{ name }}! I come from {{ city }}.<br>
+
+ Hello {{ names.1 }}!<br>
+
+ Hello {{ brother.name }}!<br>
+
+ Hello {{ brother.daughter0.name }}!<br>
+
+ {{ \"{{ no_value }}\" }}<br>
+
+ Hello{# This is a comment #}!<br>
+
+ {# --- #Todo --- #}<br>
+
+ {% for name in names %}a{% endfor %}<br>
+
+ Hello {% for name in names %}{{ name }} {% endfor %}!<br>
+
+ Hello {% for name in names %}{{ loop.index }}: {{ name }}, {% endfor %}!<br>
+
+ {% for type, name in relatives %}{{ type }}: {{ name }}, {% endfor %}<br>
+
+ {% for v in vars %}{% if v > 0 %}+{% endif %}{% endfor %}<br>
+
+ {% for name in names %}{{ loop.index }}: {{ name }}{% if not loop.is_last %}, {% endif %}{% endfor %}!<br>
+
+ {% for name in names %}{{ loop.index }}: {{ name }}{% if loop.is_last == false %}, {% endif %}{% endfor %}!<br>
+
+ {% for name in names %}a{% endfor %}<br>
+</body></html>