переделки для того, чтобы библиотека api заводилась

This commit is contained in:
2024-10-30 16:16:56 +03:00
parent 815a081a4c
commit 9037c6b329
9 changed files with 192 additions and 88 deletions

View File

@@ -15,6 +15,8 @@
#include <memory>
#include <fstream>
#include "terminal_api_driver.h"
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
@@ -70,9 +72,10 @@ void init_logging() {
log::add_common_attributes();
}
static void initResources(http::server::Server& s) {
static void initResources(http::server::Server& s, std::shared_ptr<api_driver::ApiDriver>& api) {
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/", "static/login.html", mime_types::text_html));
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/favicon.ico", "static/favicon.png", mime_types::image_png));
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/style.css", "static/style.css", mime_types::text_css));
s.resources.emplace_back(std::make_unique<http::resource::StaticFileResource>("/js/vue.js", "static/js/vue.js", mime_types::javascript));
s.resources.emplace_back(std::make_unique<http::resource::GenericResource>("/api/statistics", [](const auto& req, auto& rep) {
@@ -86,6 +89,20 @@ static void initResources(http::server::Server& s) {
const char* json = R"({"key":"value"})";
rep.content.insert(rep.content.end(), json, json + strlen(json));
}));
s.resources.emplace_back(std::make_unique<http::resource::GenericResource>("/api/mainStatistics", [api](const auto& req, auto& rep) {
if (req.method != "GET") {
http::server::stockReply(http::server::bad_request, rep);
}
rep.status = http::server::ok;
rep.headers.clear();
rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)});
std::string result = R"({"mainState":)";
result += api->loadTerminalState();
result += "}";
rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
}));
}
int main(int argc, char *argv[]) {
@@ -110,12 +127,14 @@ int main(int argc, char *argv[]) {
BOOST_LOG_TRIVIAL(info) << "Starting RELEASE build" << argv[0];
#endif
auto api = std::make_shared<api_driver::ApiDriver>();
// Initialise the server.
std::unique_ptr<http::server::Server> s;
if (strcmp(argv[1], "nossl") == 0) {
s = std::make_unique<http::server::Server>(argv[2], argv[3]);
initResources(*s);
initResources(*s, api);
s->run();
} else if (strcmp(argv[1], "ssl") == 0) {
@@ -131,15 +150,12 @@ int main(int argc, char *argv[]) {
});
ctx->set_options(ssl::context::default_workarounds | ssl::context::no_sslv2 | ssl::context::single_dh_use);
ctx->use_certificate_chain(boost::asio::buffer(cert));
ctx->use_private_key(boost::asio::buffer(key), ssl::context::file_format::pem);
ctx->use_tmp_dh(boost::asio::buffer(dh));
s = std::make_unique<http::server::Server>(argv[2], argv[3], ctx);
initResources(*s);
initResources(*s, api);
s->run();
} else {
std::cerr << "Unsupported ssl mode: " << argv[1] << std::endl;

View File

@@ -0,0 +1,26 @@
#include "terminal_api_driver.h"
#include "terminal_api/ControlProtoCInterface.h"
api_driver::ApiDriver::ApiDriver() {
CP_Login("admin", "pass", &sid, &access);
}
std::string api_driver::ApiDriver::loadTerminalState() {
return R"({"rxState":0,"txState":0,"testState":0})";
}
std::string api_driver::ApiDriver::loadTxStatistics() {
return R"("{"error":"no impl"}")";
}
std::string api_driver::ApiDriver::loadRxStatistics() {
return R"("{"error":"no impl"}")";
}
std::string api_driver::ApiDriver::loadDeviceStatistics() {
return R"("{"error":"no impl"}")";
}
api_driver::ApiDriver::~ApiDriver() = default;

37
src/terminal_api_driver.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef TERMINAL_API_DRIVER_H
#define TERMINAL_API_DRIVER_H
#include <string>
#include <terminal_api/ControlProtoCInterface.h>
namespace api_driver {
/**
* Это ApiDriver. Все ответы он будет возвращать в виде json.
*/
class ApiDriver {
public:
explicit ApiDriver();
/**
* Запросить общее состояние терминала
* @return {"rxState":0,"txState":0,"testState":0}
*/
std::string loadTerminalState();
std::string loadTxStatistics();
std::string loadRxStatistics();
std::string loadDeviceStatistics();
~ApiDriver();
private:
TSID sid{0};
unsigned int access{0};
};
}
#endif //TERMINAL_API_DRIVER_H