summaryrefslogtreecommitdiffhomepage
path: root/plugin.cpp
blob: c0a01fc0f33aa0117bf1b51575c8f1f7258da24b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "plugin.h"

#include <boost/dll/import.hpp>
#include <boost/filesystem.hpp>

#include <iostream>
#include <filesystem>

namespace dll = boost::dll;
namespace fs = std::filesystem;
using namespace std::string_literals;

PluginLoader::PluginLoader(Config& config): m_config{config}
{
}

void PluginLoader::load_plugins()
{
 const auto& plugin_directories{m_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()};

    try {
    boost::shared_ptr<webserver_plugin_interface> plugin = dll::import<webserver_plugin_interface>(lib_path, "webserver_plugin", dll::load_mode::append_decorations);
    if (plugin) {
     if (plugin->version() != webserver_plugin_interface::interface_version)
      throw std::runtime_error("Bad interface version for "s + path.path().generic_string() + ": "s + std::to_string(plugin->version()) + " vs. "s + std::to_string(webserver_plugin_interface::interface_version));

     if (m_plugins.contains(plugin->name()))
      throw std::runtime_error("Plugin already exists: "s + plugin->name());

     m_plugins.emplace(plugin->name(), plugin);

     std::cout << "Found plugin: " << plugin->name() << std::endl;
    } else
     std::cout << "Can't load plugin from " << path.path().generic_string() << std::endl;
    } catch (const std::exception& ex) {
     std::cout << "Can't load plugin from " << path.path().generic_string() << ": " << ex.what() << std::endl;
    }
   }
  }
 }

}

bool PluginLoader::validate_config()
{
 const auto& sites{m_config.Sites()};

 for (const auto& site: sites) {
  for (const auto& path: site.paths) {
   if (path.type == Plugin) {
    std::string plugin {path.params.at("plugin")};

    if (!m_plugins.contains(plugin)) {
     std::cout << "Configured plugin " << plugin << " not found" << std::endl;
     return false;
    }
   }
  }
 }
 return true;
}