Commit e3f158e0 authored by Michael Ott's avatar Michael Ott
Browse files

Add /version query to REST API

parent 923d8efd
...@@ -26,6 +26,9 @@ ...@@ -26,6 +26,9 @@
//================================================================================ //================================================================================
#include "CARestAPI.h" #include "CARestAPI.h"
#include <dcdb/version.h>
#include "version.h"
#include <boost/beast/http/field.hpp> #include <boost/beast/http/field.hpp>
#define stdBind(fun) std::bind(&CARestAPI::fun, \ #define stdBind(fun) std::bind(&CARestAPI::fun, \
...@@ -48,7 +51,8 @@ CARestAPI::CARestAPI(serverSettings_t settings, ...@@ -48,7 +51,8 @@ CARestAPI::CARestAPI(serverSettings_t settings,
_mqttServer(mqttServer) { _mqttServer(mqttServer) {
addEndpoint("/help", {http::verb::get, stdBind(GET_help)}); addEndpoint("/help", {http::verb::get, stdBind(GET_help)});
addEndpoint("/hosts", {http::verb::get, stdBind(GET_hosts)}); addEndpoint("/version", {http::verb::get, stdBind(GET_version)});
addEndpoint("/hosts", {http::verb::get, stdBind(GET_hosts)});
addEndpoint("/average", {http::verb::get, stdBind(GET_average)}); addEndpoint("/average", {http::verb::get, stdBind(GET_average)});
addEndpoint("/quit", {http::verb::put, stdBind(PUT_quit)}); addEndpoint("/quit", {http::verb::put, stdBind(PUT_quit)});
...@@ -69,6 +73,11 @@ void CARestAPI::GET_help(endpointArgs) { ...@@ -69,6 +73,11 @@ void CARestAPI::GET_help(endpointArgs) {
res.result(http::status::ok); res.result(http::status::ok);
} }
void CARestAPI::GET_version(endpointArgs) {
res.body() = "CollectAgent " + std::string(VERSION) + " (libdcdb " + DCDB::Version::getVersion() + ")";
res.result(http::status::ok);
}
void CARestAPI::GET_hosts(endpointArgs) { void CARestAPI::GET_hosts(endpointArgs) {
if (!_mqttServer) { if (!_mqttServer) {
res.body() = "The MQTT server is not initialized!"; res.body() = "The MQTT server is not initialized!";
......
...@@ -82,6 +82,18 @@ private: ...@@ -82,6 +82,18 @@ private:
*/ */
void GET_help(endpointArgs); void GET_help(endpointArgs);
/**
* GET "/version"
*
* @brief Return version number.
*
* Queries | key | possible values | explanation
* -------------------------------------------------------------------------
* Required | - | - | -
* Optional | - | - | -
*/
void GET_version(endpointArgs);
/** /**
* GET "/hosts" * GET "/hosts"
* *
......
...@@ -259,8 +259,8 @@ bool RESTHttpsServer::validateUser(const http::request<Body>& req, Send&& send) ...@@ -259,8 +259,8 @@ bool RESTHttpsServer::validateUser(const http::request<Body>& req, Send&& send)
res.body() = "Unauthorized access!\n"; res.body() = "Unauthorized access!\n";
res.prepare_payload(); res.prepare_payload();
//GET /help does not need any authorization //GET /help and /version do not need any authorization
if (req.target() == "/help" && req.method() == http::verb::get) { if ((req.method() == http::verb::get) && ((req.target() == "/help") || (req.target() == "/version"))) {
return true; return true;
} }
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <signal.h> #include <signal.h>
#include "version.h"
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
...@@ -51,6 +52,7 @@ RestAPI::RestAPI(serverSettings_t settings, ...@@ -51,6 +52,7 @@ RestAPI::RestAPI(serverSettings_t settings,
_io(io) { _io(io) {
addEndpoint("/help", {http::verb::get, stdBind(GET_help)}); addEndpoint("/help", {http::verb::get, stdBind(GET_help)});
addEndpoint("/version", {http::verb::get, stdBind(GET_version)});
addEndpoint("/plugins", {http::verb::get, stdBind(GET_plugins)}); addEndpoint("/plugins", {http::verb::get, stdBind(GET_plugins)});
addEndpoint("/sensors", {http::verb::get, stdBind(GET_sensors)}); addEndpoint("/sensors", {http::verb::get, stdBind(GET_sensors)});
addEndpoint("/average", {http::verb::get, stdBind(GET_average)}); addEndpoint("/average", {http::verb::get, stdBind(GET_average)});
...@@ -76,6 +78,11 @@ void RestAPI::GET_help(endpointArgs) { ...@@ -76,6 +78,11 @@ void RestAPI::GET_help(endpointArgs) {
res.result(http::status::ok); res.result(http::status::ok);
} }
void RestAPI::GET_version(endpointArgs) {
res.body() = "dcdbpusher " + std::string(VERSION);
res.result(http::status::ok);
}
void RestAPI::GET_plugins(endpointArgs) { void RestAPI::GET_plugins(endpointArgs) {
std::ostringstream data; std::ostringstream data;
if (getQuery("json", queries) == "true") { if (getQuery("json", queries) == "true") {
......
...@@ -89,6 +89,18 @@ class RestAPI : public RESTHttpsServer { ...@@ -89,6 +89,18 @@ class RestAPI : public RESTHttpsServer {
*/ */
void GET_help(endpointArgs); void GET_help(endpointArgs);
/**
* GET "/version"
*
* @brief Return version number
*
* Queries | key | possible values | explanation
* -------------------------------------------------------------------------
* Required | - | - | -
* Optional | - | - | -
*/
void GET_version(endpointArgs);
/** /**
* GET "/plugins" * GET "/plugins"
* *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment