tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
Tufão's plugin system

When you are developing web applications, usually you are faced with the problem of develop applications that will be running for years, without stop.

Solutions in interpreted languages usually handle this problem by reloading the source file each time it changes. In C++, we can achieve a similar behaviour through plugins.

Tufão provides the class Tufao::HttpPluginServer and a QtCreator's plugin to facilitate the integration of Qt plugins and Tufao::HttpServerRequestRouter.

Understanding by example

To help you understand the Tufão's plugin system, let's revisit the application created in Revisiting Hello World!. This application should be the same generated by the application template, under Tufão QtCreator's plugin.

The Tufao::HttpPluginServer class

The Tufao::HttpServerRequestRouter provides a robust request router interface and would be useful if we could tell it to use a plugin located in the file system as a handler. This is where Tufao::HttpPluginServer enters.

Tufao::HttpPluginServer implements the Tufao::AbstractHttpServerRequestHandler interface, then you can use it as a handler in Tufao::HttpServerRequestRouter. Tufao::HttpPluginServer has its own set of mapping rules and handlers, but it will load its handlers from plugins.

In the plugin server created previously, we pass a file in the constructor. This is how we set the plugins from Tufao::HttpPluginServer. This file can be edited using any text editor and Tufão will reload it when the file changes. See Tufao::HttpPluginServer::setConfig to learn how edit this file.

The plugin

qtcreator_pluginserver.png
The plugin template

Enough text! Let's create our plugin. First, you must open QtCreator, go to the new project dialog and select the plugin template in the Tufão Web Server project.

You'll get the following files:

  • *.pro: The name of this file depends on the name of the project. Tells qmake to use the lib TEMPLATE and the plugin CONFIG.
  • plugin.h: Defines a class that implements the Tufao::HttpServerPlugin interface, needed by Tufao::HttpPluginServer.
  • plugin.cpp: Contains the implementation of Plugin members.

The Tufao::HttpServerPlugin class is just a factory used by Tufao::HttpPluginServer to instantiate new handler objects. If you intend to create a plugin to be used by Tufao::HttpPluginServer, you must:

  1. Use Qt's plugin system
    1. Create a new qmake-based project
    2. Use lib template and plugin config
  2. Implement the Tufao::HttpServerPlugin interface in the plugin class (and don't forget the Q_INTERFACES macro)
  3. Use Q_PLUGIN_METADATA to tell Qt about your plugin's metadata

See Tufao::HttpServerPlugin for more details.

The glue!

Finally we have:

  • An application supporting plugins
  • A plugin

The only thing missing here is tell the application to use the plugin we created. To do that, we need:

  • Edit the config file used by the plugin server of our application. In the previous application, this file is any file named routes.json placed in the application's working dir.

Here is a basic one (adapt and use it):

{
"version": 0,
"plugins": [
{
"name": "myplugin",
"path": "/path/to/your/plugin/binary"
}
],
"requests": [
{
"path": "^/plugin$",
"plugin": "myplugin"
}
]
}

When you ran your application, Tufão tried to find the routes.json file and failed. Now you'll restart the application and the application will succeed to find the routes.json file.

After Tufão finds the routes.json file, it will watch this file for changes and reload the plugins when it happens.

See also
Tufao::HttpServerPlugin