summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-04 11:32:54 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-04 11:32:54 +0200
commit2be63668af1cadf846ae2d44a0fd5c909ceaf47e (patch)
treeaf80bc23d9b1dde815a3cf44285c6719490d282a
parent12972923e74e3dd174f3ce3e59c2db5ca9b400eb (diff)
Add plugins
-rw-r--r--Makefile22
-rw-r--r--config.cpp15
-rw-r--r--config.h4
-rw-r--r--plugin.cpp36
-rw-r--r--plugin.h5
-rw-r--r--plugin_interface.h12
-rw-r--r--webserver.conf3
-rw-r--r--webserver.cpp3
8 files changed, 89 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index bb93b0d..65c3ada 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ CXX=clang++
endif
ifeq ($(shell which $(CXX)),)
-CXX=g++-9
+#CXX=g++-9
endif
ifeq ($(CXXFLAGS),)
@@ -23,7 +23,7 @@ CXXFLAGS+= -Wall -I.
CXXFLAGS+= -pthread
ifeq ($(CXX),clang++-10)
-CXXFLAGS+=-std=c++20 -stdlib=libc++
+CXXFLAGS+=-std=c++20 #-stdlib=libc++
else
CXXFLAGS+=-std=c++2a
endif
@@ -39,16 +39,17 @@ LIBS=\
-lboost_filesystem \
-lboost_regex \
-lpthread \
--lssl -lcrypto
+-lssl -lcrypto \
+-ldl
ifeq ($(CXX),clang++-10)
LIBS+= \
-fuse-ld=lld-10 \
--lc++ \
--lc++abi
+-lstdc++
+#-lc++ \
+#-lc++abi
#-lc++fs
-#-lstdc++ \
-#-lstdc++fs \
+#-lstdc++fs
else
LIBS+= \
-lstdc++
@@ -58,7 +59,8 @@ endif
PROGSRC=\
config.cpp \
http.cpp \
- http_debian10.cpp
+ http_debian10.cpp \
+ plugin.cpp
TESTSRC=\
test-webserver.cpp \
@@ -68,7 +70,7 @@ TESTSRC=\
SRC=$(PROGSRC) webserver.cpp
-all: test-$(PROJECTNAME) $(PROJECTNAME)
+all: $(PROJECTNAME) test-$(PROJECTNAME)
./test-$(PROJECTNAME)
./webserver -c webserver.conf
@@ -126,8 +128,6 @@ zip: clean
zip -r ../$(PROJECTNAME).zip *
ls -l ../$(PROJECTNAME).zip
-
-
.PHONY: clean all zip install deb deb-src debs all $(DISTROS)
-include $(wildcard $(SRC:.cpp=.d))
diff --git a/config.cpp b/config.cpp
index c71ce9e..d28a3c1 100644
--- a/config.cpp
+++ b/config.cpp
@@ -102,6 +102,21 @@ std::string Config::Group() const
return m_group;
}
+const std::vector<std::string>& Config::PluginDirectories() const
+{
+ return m_plugin_directories;
+}
+
+const std::vector<Site>& Config::Sites() const
+{
+ return m_sites;
+}
+
+const std::vector<Socket>& Config::Sockets() const
+{
+ return m_sockets;
+}
+
void Config::dump() const
{
std::cout << "=== Configuration ===========================" << std::endl;
diff --git a/config.h b/config.h
index e69b298..4838ea6 100644
--- a/config.h
+++ b/config.h
@@ -56,6 +56,10 @@ class Config
std::string User() const;
std::string Group() const;
+ const std::vector<std::string>& PluginDirectories() const;
+ const std::vector<Site>& Sites() const;
+ const std::vector<Socket>& Sockets() const;
+
void dump() const;
};
diff --git a/plugin.cpp b/plugin.cpp
new file mode 100644
index 0000000..9c47ed2
--- /dev/null
+++ b/plugin.cpp
@@ -0,0 +1,36 @@
+#include "plugin.h"
+
+#include <boost/dll/import.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/filesystem.hpp>
+
+#include <iostream>
+#include <filesystem>
+
+#include "plugin_interface.h"
+
+namespace dll = boost::dll;
+namespace fs = std::filesystem;
+using namespace std::string_literals;
+
+void load_plugins(Config& config)
+{
+ const auto& plugin_directories{config.PluginDirectories()};
+
+ for (const auto& dir: plugin_directories) {
+ for (auto& path: fs::recursive_directory_iterator(dir)) {
+ if (path.is_regular_file()) {
+
+ dll::fs::path lib_path{path.path()};
+
+ boost::shared_ptr<webserver_plugin_interface> plugin = dll::import<webserver_plugin_interface>(lib_path, "webserver_plugin", dll::load_mode::append_decorations);
+ if (!plugin)
+ throw std::runtime_error("Can't load plugin");
+
+ std::cout << "Plugin: " << plugin->generate_page("a") << std::endl;
+ }
+ }
+ }
+
+}
+
diff --git a/plugin.h b/plugin.h
new file mode 100644
index 0000000..1c88ff9
--- /dev/null
+++ b/plugin.h
@@ -0,0 +1,5 @@
+#pragma once
+
+#include "config.h"
+
+void load_plugins(Config& config);
diff --git a/plugin_interface.h b/plugin_interface.h
new file mode 100644
index 0000000..72da0b9
--- /dev/null
+++ b/plugin_interface.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <boost/config.hpp>
+
+#include <string>
+
+class BOOST_SYMBOL_VISIBLE webserver_plugin_interface {
+public:
+ virtual std::string generate_page(std::string path) = 0;
+ virtual ~webserver_plugin_interface(){}
+};
+
diff --git a/webserver.conf b/webserver.conf
index 42bc383..91ea90b 100644
--- a/webserver.conf
+++ b/webserver.conf
@@ -1,8 +1,11 @@
<webserver>
<user>www-data</user>
<group>www-data</group>
+ <!--
<plugin-directory><a c="d">b<e>f</e></a>/usr/lib/webserver/plugins</plugin-directory>
<plugin-directory>/usr/local/lib/webserver/plugins</plugin-directory>
+ -->
+ <plugin-directory>plugins</plugin-directory>
<sites>
<site>
<name>antcom.de</name>
diff --git a/webserver.cpp b/webserver.cpp
index b079707..2574272 100644
--- a/webserver.cpp
+++ b/webserver.cpp
@@ -1,5 +1,6 @@
#include "config.h"
#include "http.h"
+#include "plugin.h"
#include <exception>
#include <iostream>
@@ -32,9 +33,11 @@ int main(int argc, char* argv[])
try {
Config config{config_filename};
+ load_plugins(config);
return http_server(argc, argv);
} catch (const std::exception& ex) {
std::cout << "Error: " << ex.what() << std::endl;
return 1;
}
}
+