| Home | API Documentation |
Internal: Mug · ThorsMug · ThorsSlack · NisseServer · NisseHTTP · ThorsSocket · ThorsCrypto · ThorsSerializer · ThorsMongo · ThorsLogging · ThorsIOUtil
Implementation details for the ThorsMug plugin interface.
Source: third/Mug/src/ThorsMug/
ThorsMug is a minimal library consisting of a single header (MugPlugin.h) that defines the plugin interface. Plugins are compiled as shared libraries and loaded by the Mug server at runtime.
class MugPlugin {
public:
virtual ~MugPlugin() {}
virtual void start(NisHttp::HTTPHandler& handler) = 0;
virtual void stop(NisHttp::HTTPHandler& handler) = 0;
};
start() is called when the plugin is loaded. The plugin should register its HTTP routes on the provided HTTPHandler.stop() is called when the plugin is being unloaded. The plugin should remove its routes.A convenience base class that automates route registration:
class MugPluginSimple : public MugPlugin {
public:
virtual void start(NisHttp::HTTPHandler& handler) override {
auto data = getAction();
for (auto& item : data) {
handler.addPath(item.method, item.path,
std::move(item.action), std::move(item.validate));
}
}
virtual void stop(NisHttp::HTTPHandler& handler) override {
auto data = getAction();
for (auto& item : data) {
handler.remPath(item.method, item.path);
}
}
virtual std::vector<Action> getAction() = 0;
};
Note: getAction() is called in both start() and stop(), so it must be idempotent and return the same routes both times.
struct Action {
NisHttp::Method method;
std::string path;
NisHttp::HTTPAction action;
NisHttp::HTTPValidate validate = [](NisHttp::Request const&){return true;};
};
Every plugin shared library must export a C function matching MugFunc:
extern "C" {
typedef ThorsAnvil::ThorsMug::MugPlugin*(*MugFunc)(char const* config);
}
The config parameter is the JSON string from the Mug configuration file’s config field for this plugin instance. The function must return a heap-allocated MugPlugin* (ownership transfers to Mug).
dlopen() on the shared library path.dlsym() to find the mugPlugin symbol.MugFunc and calls it with the config string.MugPlugin* is stored alongside the HTTPHandler reference.plugin->start(handler).plugin->stop(handler).MugPlugin*.dlclose() on the shared library.The PathMatcher in NisseHTTP stores actions as structured types rather than std::function directly. This is important because after dlclose(), any function pointers from the unloaded library become invalid. The stop() method must remove all routes before the library is unloaded.
During hot-reload, stop() is called on the old plugin instance, then the library is unloaded and reloaded. The new MugFunc is called to create a fresh plugin instance, and start() is called. This means: