-
Notifications
You must be signed in to change notification settings - Fork 0
AnyPlugin
In order to achieve HTML display of arbitrary content based on AnyPlugin, we need the following steps
- create a new .cc/.cpp/.cxx file (at your desired location);
- define a class deriving from AnyPlugin;
- implement the two pure virtual functions of AnyPlugin in the derived class;
- register plugin;
- rebuild the project;
- modify the json file;
- run the project and you can see what we show in the browser.
- advanced: display the json file in the form of a table.
At the location we want, create a new .cc/.cpp/.cxx file for writing our HTML-displaying plugin.
In this example, we create a new HtmlShowExample.cc
file.
Load the header file we will use #include "topling/side_plugin_factory.h"
.
AnyPlugin is a pure abstract class, including two functions, Update and ToSring, which need to be implemented by derived classes.
First, we define an AnyPlugin-derived class HtmlShowExample, and implement its two pure virtual functions as follows:
#include "topling/side_plugin_factory.h"
namespace rocksdb {
class HtmlShowExample : public AnyPlugin {
public:
void Update(const json&, const SidePluginRepo&) {}
std::string ToString(const json& dump_options, const SidePluginRepo&) const {}
};
} // namespace rocksdb
Note
- When we implement HTML display, the Update function will not be used, but it still needs to be defined and left empty.
- In the ToString function, we only need to pass in its first parameter dump_options, but we don't have to do anything with this parameter.
The Update function will not be used here, so it is defined as empty and we will ignore it. Let’s talk about the ToString function below. The return value of the ToString function is of std::string type, and the returned string will be printed in the browser without distinction. We modify the ToString function above as follows:
std::string ToString(const json& dump_options, const SidePluginRepo&) const {
return "This is an example of HTML show.";
}
In this way, we have implemented a simple HTML display plugin.
At the end of the HtmlShowExample.cc
file, in the topling namespace, enter the following code:
ROCKSDB_REG_DEFAULT_CONS(HtmlShowExample, AnyPlugin);
ROCKSDB_REG_AnyPluginManip("HtmlShowExample");
-
ROCKSDB_REG_DEFAULT_CONS(param1, param2, param3)
: This macro applies to derived classes that do not use(const json&, const SidePluginRepo&)
constructor parameters.-
param1
: The plugin registration name to be written in json, string type, and need to add double quotes. -
param2
: The plugin registration class name to be written in json. -
param3
: Fill in the parent class, in this case AnyPlugin. - Note: If the values of
param1
andparam2
are the same,param1
can be omitted, as shown in the above code.
-
-
ROCKSDB_REG_AnyPluginManip(param1)
: Used to register plugins derived from the AnyPlugin class.- The parameter is the registration name of the plugin to be written in json, which is the same as
param1
ofROCKSDB_REG_DEFAULT_CONS
.
- The parameter is the registration name of the plugin to be written in json, which is the same as
First, we need to add our custom HtmlShowExample.cc to the build file, it may be CmakeLists.txt, Makefile, or a compilation script, depending on the build method of your project. Then, rebuild the project.
Find our Home, add a field
"AnyPlugin": {
"html-show-example": "HtmlShowExample"
},
-
AnyPlugin
: The abstract class name our plugins inherit from.-
html-show-example
: The variable name for our plugin object. -
HtmlShowExample
: The plugin registration name written in json, which is the same asparam1
ofROCKSDB_REG_DEFAULT_CONS
above.
-
Run the built project, enter
192.168.31.2:8088/AnyPlugin/html-show-example
in the browser, and you can see that the browser printed out the value returned by the ToString function in our code "This is an example of HTML show.".
-
192.168.31.2:8088
: The address and listening port of our database. The listening port number is set in thelistening_ports
field in thehttp
field of the json configuration file. -
AnyPlugin
: The abstract class we use. -
html-show-example
: variable name for our plugin object
Directly displayed string files is quite unreadable. We can store the configuration items that need to be displayed in a json object, and use the JsonToString
function to print the contents of the json object to the browser in the form of a table.
Modify our previous ToString function code, and keep other parts unchanged.
std::string ToString(const json& dump_options, const SidePluginRepo&) const {
json js;
js["key1"] = "This is an example of HTML show 1.";
js["key2"] = 2;
return JsonToString(js, dump_options);
}
Run the compiled project, enter
192.168.31.2:8088/AnyPlugin/html-show-example?html=1
in the browser.
-
html=1
:url parameter, indicating that the displayed content will be printed in the form of a table
For more information on url parameters and HTML display information, see the documentation.
Now we can see in the browser what we output as a table.
![](https://raw.githubusercontent.com/gukaifeng/PicGo/master/img/%E5%9F%BA%E4%BA%8E%20AnyPlugin%20%20%E5%AE%9E%E7%8E%B0%E5%AF%B9%E4%BB%BB%E6%84%8F%E5%86%85%E5%AE%B9%E7%9A%84%20HTML%20%E5%B1%95%E7%A4%BA_2.png)