tufao
1.3.0
An asynchronous web framework for C++ built on top of Qt
|
This class provides a plugin-based request handler. More...
Public Slots | |
bool | handleRequest (Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response) override |
Handle the request using the loaded plugins and rules. More... | |
Public Member Functions | |
HttpPluginServer (QObject *parent=0) | |
Constructs a null HttpPluginServer object. More... | |
HttpPluginServer (const QString &configFile, QObject *parent=0) | |
Constructs a HttpPluginServer object. More... | |
~HttpPluginServer () | |
Destroys the object. | |
bool | setConfig (const QString &file) |
Set the configuration file used to handle requests. More... | |
QString | config () const |
Returns the path of the last configuration file used. More... | |
Public Member Functions inherited from Tufao::AbstractHttpServerRequestHandler | |
operator std::function< bool (HttpServerRequest &, HttpServerResponse &)>() | |
Implicit conversion operator to std::function functor object. More... | |
virtual bool | handleRequest (Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response)=0 |
Handles the request using the response object. More... | |
This class provides a plugin-based request handler.
Use it if you need to change the application code without restart the server.
It maintains its own set of rules and is as powerful as HttpServerRequestRouter (and internally uses it), but, in contrast, exports its configuration through a json file and will use handlers loaded from plugins.
The file format is described in the HttpPluginServer::setConfig method.
|
explicit |
Constructs a null HttpPluginServer object.
parent
is passed to the QObject constructor.
|
explicit |
Constructs a HttpPluginServer object.
parent
is passed to the QObject constructor.
configFile
is used as configuration file.
QString Tufao::HttpPluginServer::config | ( | ) | const |
Returns the path of the last configuration file used.
This file is used to handle requests, loading the appropriate plugins, generating actual handlers and mapping them to the rules described in this file.
|
overrideslot |
Handle the request using the loaded plugins and rules.
bool Tufao::HttpPluginServer::setConfig | ( | const QString & | file | ) |
Set the configuration file used to handle requests.
After the file is set, HttpPluginServer will watch the file for changes and reload its config when the file changes.
true | if HttpPluginServer finds the file. |
false | if HttpPluginServer can't find the file. |
Call this method with an empty string if you want to clear the plugin server.
An simplified use case to describing how HttpPluginServer reacts to changes follows:
HttpPluginServer object come back to the default-constructed state.
If the last config had "version: 0", then it means no more monitoring either (this is what default-constructed state means).
If the last config had "version: 1", then HttpPluginServer will (after the cleanup) start to monitor the containing folder, waiting until a config file with the same name is available again to resume its operation.
The configuration file format is json-based. If you aren't used to JSON, read the json specification.
The file must have a root json object with 3 attributes:
An example follows:
{ version: 1, plugins: [ { name: "home", path: "/home/vinipsmaker/Projetos/tufao-project42/build/plugins/libhome.so", customData: {appName: "Hello World", root: "/"} }, { name: "user", path: "show_user.so", dependencies: ["home"] }, { name: "404", path: "/usr/lib/tufao/plugins/notfound.so", customData: "<h1>Not Found</h1><p>I'm sorry, but it's your fault</p>" } ], requests: [ { path: "^/$", plugin: "home", method: "GET" }, { path: "^/user/(\w*)$", plugin: "user" }, { path: "", plugin: "404" } ] }
The requests attribute is used to seed data to a HttpServerRequestRouter object. Because this, you can use features like return false from a handler to allow another handler handle a request.