From 2be63668af1cadf846ae2d44a0fd5c909ceaf47e Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 4 Apr 2020 11:32:54 +0200 Subject: Add plugins --- Makefile | 22 +++++++++++----------- config.cpp | 15 +++++++++++++++ config.h | 4 ++++ plugin.cpp | 36 ++++++++++++++++++++++++++++++++++++ plugin.h | 5 +++++ plugin_interface.h | 12 ++++++++++++ webserver.conf | 3 +++ webserver.cpp | 3 +++ 8 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 plugin.cpp create mode 100644 plugin.h create mode 100644 plugin_interface.h 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& Config::PluginDirectories() const +{ + return m_plugin_directories; +} + +const std::vector& Config::Sites() const +{ + return m_sites; +} + +const std::vector& 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& PluginDirectories() const; + const std::vector& Sites() const; + const std::vector& 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 +#include +#include + +#include +#include + +#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 plugin = dll::import(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 + +#include + +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 @@ www-data www-data + + plugins antcom.de 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 #include @@ -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; } } + -- cgit v1.2.3