summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-02-11 16:04:35 +0100
committerRoland Reichwein <mail@reichwein.it>2023-02-11 16:04:35 +0100
commit92a14d375c8cd9dabc32ccb6dcbdf83321af535f (patch)
tree5a0c22d993199f8fa0de1d18b78d5209ca9f4c81
parentdf5e04b2dafde8ff3c87ba8412a9a728f8b24b7d (diff)
Added config
-rw-r--r--Makefile6
-rw-r--r--config.cpp73
-rw-r--r--config.h25
-rw-r--r--main.cpp7
-rw-r--r--weblog.conf10
-rw-r--r--weblog.cpp49
-rw-r--r--weblog.h21
7 files changed, 125 insertions, 66 deletions
diff --git a/Makefile b/Makefile
index 30f0335..bbe1cd0 100644
--- a/Makefile
+++ b/Makefile
@@ -15,10 +15,8 @@ LDLIBS=\
-lssl -lcrypto \
-ldl
-PROGSRC=\
- weblog.cpp
-
-SRC=$(PROGSRC)
+SRC=\
+ weblog.cpp config.cpp main.cpp
all: $(PROJECTNAME)
diff --git a/config.cpp b/config.cpp
new file mode 100644
index 0000000..44e0d36
--- /dev/null
+++ b/config.cpp
@@ -0,0 +1,73 @@
+#include "config.h"
+
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/xml_parser.hpp>
+
+#include <iostream>
+#include <string>
+
+namespace pt = boost::property_tree;
+using namespace std::string_literals;
+
+namespace {
+ const std::string default_datapath {"/var/lib/blog"};
+ const std::string default_listen {"::1:9000"};
+ const std::string default_name{"blog"};
+ const std::string default_keywords{"blog, reichwein.it, weblog"};
+}
+
+Config::Config(const std::string& config_filename):
+ m_dataPath{default_datapath},
+ m_listenAddress{"::1"},
+ m_listenPort{9000},
+ m_name{default_name},
+ m_keywords{default_keywords}
+{
+
+ try {
+ pt::ptree tree;
+
+ pt::read_xml(config_filename, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace);
+
+ m_dataPath = tree.get<std::string>("config.datapath", default_datapath);
+ m_name = tree.get<std::string>("config.name", default_name);
+ std::string listen {tree.get<std::string>("config.port", default_listen)};
+ auto pos{listen.find_last_of(':')};
+ if (pos == std::string::npos)
+ throw std::runtime_error("Bad port address: "s + listen);
+
+ m_listenAddress = listen.substr(0, pos);
+ m_listenPort = std::stoi(listen.substr(pos + 1));
+ if (m_listenPort < 0 || m_listenPort > 65535)
+ throw std::runtime_error("Bad listen port: "s + std::to_string(m_listenPort));
+
+ m_keywords = tree.get<int>("config.keywords", default_keywords);
+ } catch (const std::exception& ex) {
+ std::cerr << "Error reading config file " << config_filename << ". Using defaults." << std::endl;
+ }
+}
+
+std::string Config::getDataPath() const
+{
+ return m_dataPath;
+}
+
+std::string Config::getListenAddress() const
+{
+ return m_listenAddress;
+}
+
+int Config::getListenPort() const
+{
+ return m_listenPort;
+}
+
+std::string Config::getName() const
+{
+ return m_name;
+}
+
+std::string Config::getKeywords() const
+{
+ return m_keywords;
+}
diff --git a/config.h b/config.h
new file mode 100644
index 0000000..fff7f9d
--- /dev/null
+++ b/config.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <string>
+
+const std::string default_config_filename{"/etc/weblog.conf"};
+
+class Config
+{
+private:
+ std::string m_dataPath;
+ std::string m_listenAddress; // ip address v4/v6
+ int m_listenPort;
+ std::string m_name;
+ std::string m_keywords;
+
+public:
+ Config(const std::string& config_filename = default_config_filename);
+ std::string getDataPath() const;
+
+ std::string getListenAddress() const;
+ int getListenPort() const;
+
+ std::string getName() const;
+ std::string getKeywords() const;
+};
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..84cec43
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,7 @@
+#include "weblog.h"
+
+int main(int argc, char* argv[])
+{
+ Weblog weblog;
+ return weblog.run(argc, argv);
+}
diff --git a/weblog.conf b/weblog.conf
index 80a5ec6..6dd9ab7 100644
--- a/weblog.conf
+++ b/weblog.conf
@@ -1,4 +1,6 @@
-::1:9019
-/home/ernie/testblog
-name Roland Reichweins Blog
-keywords Roland Reichwein, Blog
+<config>
+ <port>::1:9019</port>
+ <datapath>/home/ernie/testblog</datapath>
+ <name>Roland Reichweins Blog</name>
+ <keywords>Roland Reichwein, Blog</keywords>
+</config>
diff --git a/weblog.cpp b/weblog.cpp
index cc2be34..7842ac3 100644
--- a/weblog.cpp
+++ b/weblog.cpp
@@ -2,6 +2,7 @@
#include "libreichwein/mime.h"
#include "libreichwein/stringhelper.h"
+#include "libreichwein/url.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
@@ -322,36 +323,6 @@ namespace {
}
}
- std::string urlDecode(std::string s)
- {
- std::string result;
-
- size_t pos = 0;
- while (pos < s.size()) {
- char c {s[pos]};
- if (c == '+') {
- result += ' ';
- } else if (c == '%' && pos + 2 < s.size()) {
- try {
- int i = stoi(s.substr(pos + 1, 2), 0, 16);
- if (i < 0 || i > 255)
- return result;
-
- result += static_cast<char>(i);
- } catch (...) {
- return result;
- }
-
- pos += 2;
- } else {
- result += c;
- }
- pos++;
- }
-
- return result;
- }
-
std::unordered_map<std::string, std::string> SplitQueryString(std::string& s)
{
std::unordered_map<std::string, std::string> result;
@@ -362,7 +333,7 @@ namespace {
for (auto i: list) {
size_t apos = i.find('=');
if (apos != i.npos) {
- result[urlDecode(i.substr(0, apos))] = urlDecode(i.substr(apos + 1));
+ result[Reichwein::URL::urlDecode(i.substr(0, apos))] = Reichwein::URL::urlDecode(i.substr(apos + 1));
}
}
}
@@ -374,22 +345,17 @@ namespace {
} // anonymous namespace
-std::string weblog_plugin::name()
-{
- return "weblog";
-}
-
-weblog_plugin::weblog_plugin()
+Weblog::Weblog()
{
//std::cout << "Plugin constructor" << std::endl;
}
-weblog_plugin::~weblog_plugin()
+Weblog::~Weblog()
{
//std::cout << "Plugin destructor" << std::endl;
}
-std::string weblog_plugin::generate_page(
+std::string Weblog::generate_page(
std::function<std::string(const std::string& key)>& GetServerParam,
std::function<std::string(const std::string& key)>& GetRequestParam, // request including body (POST...)
std::function<void(const std::string& key, const std::string& value)>& SetResponseHeader // to be added to result string
@@ -450,7 +416,8 @@ std::string weblog_plugin::generate_page(
}
}
-bool weblog_plugin::has_own_authentication()
+void Weblog::run(int argc, char* argv[])
{
- return false;
+ return 0;
}
+
diff --git a/weblog.h b/weblog.h
index 0994b91..85eb5b1 100644
--- a/weblog.h
+++ b/weblog.h
@@ -1,23 +1,10 @@
#pragma once
-#include "../../plugin_interface.h"
-
-class weblog_plugin: public webserver_plugin_interface
+class weblog
{
public:
- weblog_plugin();
- ~weblog_plugin();
+ weblog();
+ ~weblog();
- std::string name() override;
-
- std::string generate_page(
- std::function<std::string(const std::string& key)>& GetServerParam,
- std::function<std::string(const std::string& key)>& GetRequestParam, // request including body (POST...)
- std::function<void(const std::string& key, const std::string& value)>& SetResponseHeader // to be added to result string
- ) override;
-
- bool has_own_authentication() override;
+ void run(int argc, char* argv[]);
};
-
-extern "C" BOOST_SYMBOL_EXPORT weblog_plugin webserver_plugin;
-weblog_plugin webserver_plugin;