From 399e22216a18e82cbb248e8d395dbc90ed971c34 Mon Sep 17 00:00:00 2001 From: Andrea Bondavalli Date: Thu, 25 Feb 2021 18:00:01 +0100 Subject: [PATCH] - added command line option -v to daemon to return current version - set current daemon version to bondagit-1.0 - added HTTP API to return current daemon version and updated documentation - added diplay of current daemon version in WebUI Config page --- daemon/README.md | 19 +++++++++++++++++++ daemon/http_server.cpp | 7 +++++++ daemon/main.cpp | 20 +++++++++++++++----- daemon/main.hpp | 1 + webui/src/Config.js | 27 ++++++++++++++++++++++----- webui/src/Services.js | 8 ++++++++ 6 files changed, 72 insertions(+), 10 deletions(-) diff --git a/daemon/README.md b/daemon/README.md index 8dda03c..4dc9091 100644 --- a/daemon/README.md +++ b/daemon/README.md @@ -30,6 +30,13 @@ All operations returns HTTP *200* status code in case of success and HTTP *4xx* In case of failure the server returns a **text/plain** content type with the category and a description of the error occurred. **_NOTE:_** At present the embedded HTTP server doesn't implement neither HTTPS nor user authentication. +### Get Daemon Version ### +* **URL** /api/version +* **Method** GET +* **URL Params** none +* **Body Type** application/json +* **Body** [Version params](#version) + ### Get Daemon Configuration ### * **URL** /api/config * **Method** GET @@ -142,6 +149,18 @@ In case of failure the server returns a **text/plain** content type with the cat ## HTTP REST API structures ## +### JSON Version ### + +Example + { + "version:" "bondagit-1.0" + } + +where: + +> **version** +> JSON string specifying the daemon version. + ### JSON Config ### Example diff --git a/daemon/http_server.cpp b/daemon/http_server.cpp index 8ad57ef..b8e36da 100644 --- a/daemon/http_server.cpp +++ b/daemon/http_server.cpp @@ -23,6 +23,7 @@ #include #include +#include "main.hpp" #include "json.hpp" #include "log.hpp" #include "http_server.hpp" @@ -98,6 +99,12 @@ bool HttpServer::init() { set_headers(res); }); + /* get version */ + svr_.Get("/api/version", [&](const Request& req, Response& res) { + set_headers(res, "application/json"); + res.body = "{ \"version\": \"" + get_version() + "\" }"; + }); + /* get config */ svr_.Get("/api/config", [&](const Request& req, Response& res) { set_headers(res, "application/json"); diff --git a/daemon/main.cpp b/daemon/main.cpp index 56d0f2a..3c6fd75 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -35,6 +35,7 @@ namespace po = boost::program_options; namespace postyle = boost::program_options::command_line_style; namespace logging = boost::log; +static std::string version("bondagit-1.0"); static std::atomic terminate = false; void termination_handler(int signum) { @@ -47,14 +48,19 @@ bool is_terminated() { return terminate.load(); } +const std::string& get_version() { + return version; +} + int main(int argc, char* argv[]) { int rc = EXIT_SUCCESS; po::options_description desc("Options"); - desc.add_options()( - "config,c", po::value()->default_value("/etc/daemon.conf"), - "daemon configuration file")("http_port,p", po::value(), - "HTTP server port")( - "help,h", "Print this help message"); + desc.add_options() + ("version,v", "Print daemon version and exit") + ("config,c", po::value()->default_value("/etc/daemon.conf"), + "daemon configuration file") + ("http_port,p", po::value(), "HTTP server port") + ("help,h", "Print this help message"); int unix_style = postyle::unix_style | postyle::short_allow_next; po::variables_map vm; @@ -67,6 +73,10 @@ int main(int argc, char* argv[]) { po::notify(vm); + if (vm.count("version")) { + std::cout << version << '\n'; + return EXIT_SUCCESS; + } if (vm.count("help")) { std::cout << "USAGE: " << argv[0] << '\n' << desc << '\n'; return EXIT_SUCCESS; diff --git a/daemon/main.hpp b/daemon/main.hpp index f9d5e68..0b83a5a 100644 --- a/daemon/main.hpp +++ b/daemon/main.hpp @@ -21,5 +21,6 @@ #define _MAIN_HPP_ bool is_terminated(); +const std::string& get_version(); #endif diff --git a/webui/src/Config.js b/webui/src/Config.js index 0115f45..9aaf0bc 100644 --- a/webui/src/Config.js +++ b/webui/src/Config.js @@ -30,6 +30,7 @@ class Config extends Component { constructor(props) { super(props); this.state = { + version: '', httpPort: '', rtspPort: '', rtspPortErr: false, @@ -57,7 +58,8 @@ class Config extends Component { macAddr: '', ipAddr: '', errors: 0, - isConfigLoading: false + isConfigLoading: false, + isVersionLoading: false }; this.onSubmit = this.onSubmit.bind(this); this.inputIsValid = this.inputIsValid.bind(this); @@ -65,6 +67,16 @@ class Config extends Component { componentDidMount() { this.setState({isConfigLoading: true}); + this.setState({isVersionLoading: true}); + RestAPI.getVersion() + .then(response => response.json()) + .then( + data => this.setState( + { + version: data.version, + isVersionLoading: false + })) + .catch(err => this.setState({isVersionLoading: false})); RestAPI.getConfig() .then(response => response.json()) .then( @@ -105,6 +117,7 @@ class Config extends Component { !this.state.rtspPortErr && !this.state.sapIntervalErr && !this.state.syslogServerErr && + !this.state.isVersionLoading && !this.state.isConfigLoading; } @@ -130,6 +143,14 @@ class Config extends Component { render() { return (
+ {this.state.isVersionLoading ? :

Version

} + + + + + +
+
{this.state.isConfigLoading ? :

Audio Config

} @@ -140,10 +161,6 @@ class Config extends Component {