Framework update

This commit is contained in:
stephb9959
2021-12-10 13:21:38 -08:00
parent 935139067f
commit 4a80848fd9
12 changed files with 89 additions and 71 deletions

View File

@@ -92,7 +92,7 @@ add_executable( owfms
src/storage/storage_firmwares.h src/storage/storage_history.cpp src/storage/storage_firmwares.h src/storage/storage_history.cpp
src/storage/storage_history.h src/storage/storage_deviceTypes.cpp src/storage/storage_history.h src/storage/storage_deviceTypes.cpp
src/storage/storage_deviceInfo.cpp src/storage/storage_deviceInfo.h src/storage/storage_deviceInfo.cpp src/storage/storage_deviceInfo.h
src/APIServers.cpp src/RESTAPI/RESTAPI_Routers.cpp
src/Dashboard.cpp src/Dashboard.h src/Dashboard.cpp src/Dashboard.h
src/Daemon.cpp src/Daemon.h src/Daemon.cpp src/Daemon.h
src/StorageService.cpp src/StorageService.h src/StorageService.cpp src/StorageService.h

2
build
View File

@@ -1 +1 @@
24 26

View File

@@ -14,8 +14,8 @@
namespace OpenWifi { namespace OpenWifi {
Poco::Net::HTTPRequestHandler * RESTAPI_external_server(const char *Path, RESTAPIHandler::BindingMap &Bindings, Poco::Net::HTTPRequestHandler * RESTAPI_ExtRouter(const char *Path, RESTAPIHandler::BindingMap &Bindings,
Poco::Logger & L, RESTAPI_GenericServer & S) { Poco::Logger & L, RESTAPI_GenericServer & S, uint64_t TransactionId) {
return RESTAPI_Router< return RESTAPI_Router<
RESTAPI_firmwaresHandler, RESTAPI_firmwaresHandler,
RESTAPI_firmwareHandler, RESTAPI_firmwareHandler,
@@ -25,17 +25,17 @@ namespace OpenWifi {
RESTAPI_connectedDeviceHandler, RESTAPI_connectedDeviceHandler,
RESTAPI_historyHandler, RESTAPI_historyHandler,
RESTAPI_deviceReportHandler RESTAPI_deviceReportHandler
>(Path,Bindings,L, S); >(Path,Bindings,L, S, TransactionId);
} }
Poco::Net::HTTPRequestHandler * RESTAPI_internal_server(const char *Path, RESTAPIHandler::BindingMap &Bindings, Poco::Net::HTTPRequestHandler * RESTAPI_IntRouter(const char *Path, RESTAPIHandler::BindingMap &Bindings,
Poco::Logger & L, RESTAPI_GenericServer & S) { Poco::Logger & L, RESTAPI_GenericServer & S, uint64_t TransactionId) {
return RESTAPI_Router_I< return RESTAPI_Router_I<
RESTAPI_firmwaresHandler, RESTAPI_firmwaresHandler,
RESTAPI_firmwareHandler, RESTAPI_firmwareHandler,
RESTAPI_system_command, RESTAPI_system_command,
RESTAPI_connectedDevicesHandler, RESTAPI_connectedDevicesHandler,
RESTAPI_connectedDeviceHandler RESTAPI_connectedDeviceHandler
>(Path, Bindings, L, S); >(Path, Bindings, L, S, TransactionId);
} }
} }

View File

@@ -10,12 +10,13 @@
namespace OpenWifi { namespace OpenWifi {
class RESTAPI_connectedDeviceHandler : public RESTAPIHandler { class RESTAPI_connectedDeviceHandler : public RESTAPIHandler {
public: public:
RESTAPI_connectedDeviceHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) RESTAPI_connectedDeviceHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L, : RESTAPIHandler(bindings, L,
std::vector<std::string> std::vector<std::string>
{Poco::Net::HTTPRequest::HTTP_GET, {Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS}, Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server, Server,
TransactionId,
Internal) {} Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/connectedDevice/{serialNumber}"};} static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/connectedDevice/{serialNumber}"};}

View File

@@ -11,12 +11,13 @@
namespace OpenWifi { namespace OpenWifi {
class RESTAPI_connectedDevicesHandler : public RESTAPIHandler { class RESTAPI_connectedDevicesHandler : public RESTAPIHandler {
public: public:
RESTAPI_connectedDevicesHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) RESTAPI_connectedDevicesHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L, : RESTAPIHandler(bindings, L,
std::vector<std::string> std::vector<std::string>
{Poco::Net::HTTPRequest::HTTP_GET, {Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS}, Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server, Server,
TransactionId,
Internal) {} Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/connectedDevices"};} static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/connectedDevices"};}
void DoGet() final; void DoGet() final;

View File

@@ -10,12 +10,13 @@
namespace OpenWifi { namespace OpenWifi {
class RESTAPI_deviceReportHandler : public RESTAPIHandler { class RESTAPI_deviceReportHandler : public RESTAPIHandler {
public: public:
RESTAPI_deviceReportHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) RESTAPI_deviceReportHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L, : RESTAPIHandler(bindings, L,
std::vector<std::string> std::vector<std::string>
{Poco::Net::HTTPRequest::HTTP_GET, {Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS}, Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server, Server,
TransactionId,
Internal) {} Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/deviceReport"};} static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/deviceReport"};}
void DoGet() final; void DoGet() final;

View File

@@ -10,12 +10,13 @@
namespace OpenWifi { namespace OpenWifi {
class RESTAPI_firmwareAgeHandler : public RESTAPIHandler { class RESTAPI_firmwareAgeHandler : public RESTAPIHandler {
public: public:
RESTAPI_firmwareAgeHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) RESTAPI_firmwareAgeHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L, : RESTAPIHandler(bindings, L,
std::vector<std::string> std::vector<std::string>
{Poco::Net::HTTPRequest::HTTP_GET, {Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS}, Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server, Server,
TransactionId,
Internal) {} Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/firmwareAge"};} static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/firmwareAge"};}
void DoGet() final; void DoGet() final;

View File

@@ -10,7 +10,7 @@
namespace OpenWifi { namespace OpenWifi {
class RESTAPI_firmwareHandler : public RESTAPIHandler { class RESTAPI_firmwareHandler : public RESTAPIHandler {
public: public:
RESTAPI_firmwareHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) RESTAPI_firmwareHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L, : RESTAPIHandler(bindings, L,
std::vector<std::string> std::vector<std::string>
{Poco::Net::HTTPRequest::HTTP_GET, {Poco::Net::HTTPRequest::HTTP_GET,
@@ -19,6 +19,7 @@ namespace OpenWifi {
Poco::Net::HTTPRequest::HTTP_DELETE, Poco::Net::HTTPRequest::HTTP_DELETE,
Poco::Net::HTTPRequest::HTTP_OPTIONS}, Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server, Server,
TransactionId,
Internal) {} Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/firmware/{id}"};} static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/firmware/{id}"};}
void DoGet() final; void DoGet() final;

View File

@@ -10,12 +10,13 @@
namespace OpenWifi { namespace OpenWifi {
class RESTAPI_firmwaresHandler : public RESTAPIHandler { class RESTAPI_firmwaresHandler : public RESTAPIHandler {
public: public:
RESTAPI_firmwaresHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) RESTAPI_firmwaresHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L, : RESTAPIHandler(bindings, L,
std::vector<std::string> std::vector<std::string>
{Poco::Net::HTTPRequest::HTTP_GET, {Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS}, Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server, Server,
TransactionId,
Internal) {} Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/firmwares"};} static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/firmwares"};}

View File

@@ -11,13 +11,14 @@
namespace OpenWifi { namespace OpenWifi {
class RESTAPI_historyHandler : public RESTAPIHandler { class RESTAPI_historyHandler : public RESTAPIHandler {
public: public:
RESTAPI_historyHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) RESTAPI_historyHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L, : RESTAPIHandler(bindings, L,
std::vector<std::string> std::vector<std::string>
{Poco::Net::HTTPRequest::HTTP_GET, {Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_DELETE, Poco::Net::HTTPRequest::HTTP_DELETE,
Poco::Net::HTTPRequest::HTTP_OPTIONS}, Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server, Server,
TransactionId,
Internal) {} Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/revisionHistory/{serialNumber}"};} static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/revisionHistory/{serialNumber}"};}
void DoGet() final; void DoGet() final;

View File

@@ -21,7 +21,9 @@ namespace OpenWifi {
Poco::URI DestinationURI(Svc.PrivateEndPoint); Poco::URI DestinationURI(Svc.PrivateEndPoint);
DestinationURI.setPath(PathRewrite); DestinationURI.setPath(PathRewrite);
DestinationURI.setQuery(SourceURI.getQuery()); DestinationURI.setQuery(SourceURI.getQuery());
// std::cout << DestinationURI.getHost() << ":" << DestinationURI.getPort() << "/" << DestinationURI.getPathAndQuery() << std::endl;
// std::cout << " Source: " << SourceURI.toString() << std::endl;
// std::cout << "Destination: " << DestinationURI.toString() << std::endl;
Poco::Net::HTTPSClientSession Session(DestinationURI.getHost(), DestinationURI.getPort()); Poco::Net::HTTPSClientSession Session(DestinationURI.getHost(), DestinationURI.getPort());
Session.setKeepAlive(true); Session.setKeepAlive(true);
@@ -48,8 +50,10 @@ namespace OpenWifi {
std::stringstream SS; std::stringstream SS;
try { try {
auto Body = P.parse(Request->stream()).extract<Poco::JSON::Object::Ptr>(); auto Body = P.parse(Request->stream()).extract<Poco::JSON::Object::Ptr>();
Poco::JSON::Stringifier::stringify(Body,SS); Poco::JSON::Stringifier::condense(Body,SS);
} catch(...) { SS << "\r\n\r\n";
} catch(const Poco::Exception &E) {
Logger.log(E);
} }
if(SS.str().empty()) { if(SS.str().empty()) {

View File

@@ -1522,6 +1522,7 @@ namespace OpenWifi {
Poco::Logger &l, Poco::Logger &l,
std::vector<std::string> Methods, std::vector<std::string> Methods,
RESTAPI_GenericServer & Server, RESTAPI_GenericServer & Server,
uint64_t TransactionId,
bool Internal=false, bool Internal=false,
bool AlwaysAuthorize=true, bool AlwaysAuthorize=true,
bool RateLimited=false, bool RateLimited=false,
@@ -1531,11 +1532,13 @@ namespace OpenWifi {
Logger_(l), Logger_(l),
Methods_(std::move(Methods)), Methods_(std::move(Methods)),
Server_(Server), Server_(Server),
TransactionId_(TransactionId),
Internal_(Internal), Internal_(Internal),
AlwaysAuthorize_(AlwaysAuthorize), AlwaysAuthorize_(AlwaysAuthorize),
RateLimited_(RateLimited), RateLimited_(RateLimited),
MyRates_(Profile), MyRates_(Profile),
SubOnlyService_(SubscriberOnly){ SubOnlyService_(SubscriberOnly)
{
} }
inline bool RoleIsAuthorized(const std::string & Path, const std::string & Method, std::string & Reason) { inline bool RoleIsAuthorized(const std::string & Path, const std::string & Method, std::string & Reason) {
@@ -1992,12 +1995,13 @@ namespace OpenWifi {
Poco::JSON::Parser IncomingParser_; Poco::JSON::Parser IncomingParser_;
RESTAPI_GenericServer & Server_; RESTAPI_GenericServer & Server_;
RateLimit MyRates_; RateLimit MyRates_;
uint64_t TransactionId_;
}; };
class RESTAPI_UnknownRequestHandler : public RESTAPIHandler { class RESTAPI_UnknownRequestHandler : public RESTAPIHandler {
public: public:
RESTAPI_UnknownRequestHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server) RESTAPI_UnknownRequestHandler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId)
: RESTAPIHandler(bindings, L, std::vector<std::string>{}, Server) {} : RESTAPIHandler(bindings, L, std::vector<std::string>{}, Server, TransactionId) {}
inline void DoGet() override {}; inline void DoGet() override {};
inline void DoPost() override {}; inline void DoPost() override {};
inline void DoPut() override {}; inline void DoPut() override {};
@@ -2016,30 +2020,30 @@ namespace OpenWifi {
} }
template<typename T, typename... Args> template<typename T, typename... Args>
RESTAPIHandler * RESTAPI_Router(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger, RESTAPI_GenericServer & Server) { RESTAPIHandler * RESTAPI_Router(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger, RESTAPI_GenericServer & Server, uint64_t TransactionId) {
static_assert(test_has_PathName_method((T*)nullptr), "Class must have a static PathName() method."); static_assert(test_has_PathName_method((T*)nullptr), "Class must have a static PathName() method.");
if(RESTAPIHandler::ParseBindings(RequestedPath,T::PathName(),Bindings)) { if(RESTAPIHandler::ParseBindings(RequestedPath,T::PathName(),Bindings)) {
return new T(Bindings, Logger, Server, false); return new T(Bindings, Logger, Server, false, TransactionId);
} }
if constexpr (sizeof...(Args) == 0) { if constexpr (sizeof...(Args) == 0) {
return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server); return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server, TransactionId);
} else { } else {
return RESTAPI_Router<Args...>(RequestedPath, Bindings, Logger, Server); return RESTAPI_Router<Args...>(RequestedPath, Bindings, Logger, Server, TransactionId);
} }
} }
template<typename T, typename... Args> template<typename T, typename... Args>
RESTAPIHandler * RESTAPI_Router_I(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger, RESTAPI_GenericServer & Server) { RESTAPIHandler * RESTAPI_Router_I(const std::string & RequestedPath, RESTAPIHandler::BindingMap &Bindings, Poco::Logger & Logger, RESTAPI_GenericServer & Server, uint64_t TransactionId) {
static_assert(test_has_PathName_method((T*)nullptr), "Class must have a static PathName() method."); static_assert(test_has_PathName_method((T*)nullptr), "Class must have a static PathName() method.");
if(RESTAPIHandler::ParseBindings(RequestedPath,T::PathName(),Bindings)) { if(RESTAPIHandler::ParseBindings(RequestedPath,T::PathName(),Bindings)) {
return new T(Bindings, Logger, Server, true); return new T(Bindings, Logger, Server, true, TransactionId);
} }
if constexpr (sizeof...(Args) == 0) { if constexpr (sizeof...(Args) == 0) {
return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server); return new RESTAPI_UnknownRequestHandler(Bindings,Logger, Server, TransactionId);
} else { } else {
return RESTAPI_Router_I<Args...>(RequestedPath, Bindings, Logger, Server); return RESTAPI_Router_I<Args...>(RequestedPath, Bindings, Logger, Server, TransactionId);
} }
} }
@@ -2410,17 +2414,17 @@ namespace OpenWifi {
inline ALBHealthCheckServer * ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); } inline ALBHealthCheckServer * ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); }
Poco::Net::HTTPRequestHandler * RESTAPI_external_server(const char *Path, RESTAPIHandler::BindingMap &Bindings, Poco::Net::HTTPRequestHandler * RESTAPI_ExtRouter(const char *Path, RESTAPIHandler::BindingMap &Bindings,
Poco::Logger & L, RESTAPI_GenericServer & S); Poco::Logger & L, RESTAPI_GenericServer & S, uint64_t Id);
Poco::Net::HTTPRequestHandler * RESTAPI_internal_server(const char *Path, RESTAPIHandler::BindingMap &Bindings, Poco::Net::HTTPRequestHandler * RESTAPI_IntRouter(const char *Path, RESTAPIHandler::BindingMap &Bindings,
Poco::Logger & L, RESTAPI_GenericServer & S); Poco::Logger & L, RESTAPI_GenericServer & S, uint64_t Id);
class RESTAPI_server : public SubSystemServer { class RESTAPI_ExtServer : public SubSystemServer {
public: public:
static RESTAPI_server *instance() { static RESTAPI_ExtServer *instance() {
static RESTAPI_server *instance_ = new RESTAPI_server; static RESTAPI_ExtServer *instance_ = new RESTAPI_ExtServer;
return instance_; return instance_;
} }
int Start() override; int Start() override;
@@ -2434,9 +2438,9 @@ namespace OpenWifi {
inline void reinitialize(Poco::Util::Application &self) override; inline void reinitialize(Poco::Util::Application &self) override;
inline Poco::Net::HTTPRequestHandler *CallServer(const char *Path) { inline Poco::Net::HTTPRequestHandler *CallServer(const char *Path, uint64_t Id) {
RESTAPIHandler::BindingMap Bindings; RESTAPIHandler::BindingMap Bindings;
return RESTAPI_external_server(Path, Bindings, Logger_, Server_); return RESTAPI_ExtRouter(Path, Bindings, Logger_, Server_, Id);
} }
private: private:
@@ -2444,18 +2448,18 @@ namespace OpenWifi {
Poco::ThreadPool Pool_; Poco::ThreadPool Pool_;
RESTAPI_GenericServer Server_; RESTAPI_GenericServer Server_;
RESTAPI_server() noexcept: RESTAPI_ExtServer() noexcept:
SubSystemServer("RESTAPIServer", "RESTAPIServer", "openwifi.restapi") SubSystemServer("RESTAPI_ExtServer", "RESTAPIServer", "openwifi.restapi")
{ {
} }
}; };
inline RESTAPI_server * RESTAPI_server() { return RESTAPI_server::instance(); }; inline RESTAPI_ExtServer * RESTAPI_ExtServer() { return RESTAPI_ExtServer::instance(); };
class RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { class ExtRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory {
public: public:
RequestHandlerFactory(RESTAPI_GenericServer & Server) : explicit ExtRequestHandlerFactory(RESTAPI_GenericServer & Server) :
Logger_(RESTAPI_server::instance()->Logger()), Logger_(RESTAPI_ExtServer::instance()->Logger()),
Server_(Server) Server_(Server)
{ {
@@ -2464,15 +2468,16 @@ namespace OpenWifi {
inline Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override { inline Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override {
Poco::URI uri(Request.getURI()); Poco::URI uri(Request.getURI());
auto *Path = uri.getPath().c_str(); auto *Path = uri.getPath().c_str();
return RESTAPI_server()->CallServer(Path); return RESTAPI_ExtServer()->CallServer(Path, TransactionId_++);
} }
private: private:
Poco::Logger &Logger_; static inline std::atomic_uint64_t TransactionId_ = 1;
RESTAPI_GenericServer &Server_; Poco::Logger &Logger_;
RESTAPI_GenericServer &Server_;
}; };
inline int RESTAPI_server::Start() { inline int RESTAPI_ExtServer::Start() {
Logger_.information("Starting."); Logger_.information("Starting.");
Server_.InitLogging(); Server_.InitLogging();
@@ -2491,7 +2496,7 @@ namespace OpenWifi {
Params->setMaxQueued(200); Params->setMaxQueued(200);
Params->setKeepAlive(true); Params->setKeepAlive(true);
auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new RequestHandlerFactory(Server_), Pool_, Sock, Params); auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new ExtRequestHandlerFactory(Server_), Pool_, Sock, Params);
NewServer->start(); NewServer->start();
RESTServers_.push_back(std::move(NewServer)); RESTServers_.push_back(std::move(NewServer));
} }
@@ -2499,11 +2504,11 @@ namespace OpenWifi {
return 0; return 0;
} }
class RESTAPI_InternalServer : public SubSystemServer { class RESTAPI_IntServer : public SubSystemServer {
public: public:
static RESTAPI_InternalServer *instance() { static RESTAPI_IntServer *instance() {
static RESTAPI_InternalServer *instance_ = new RESTAPI_InternalServer; static RESTAPI_IntServer *instance_ = new RESTAPI_IntServer;
return instance_; return instance_;
} }
@@ -2517,39 +2522,40 @@ namespace OpenWifi {
inline void reinitialize(Poco::Util::Application &self) override; inline void reinitialize(Poco::Util::Application &self) override;
inline Poco::Net::HTTPRequestHandler *CallServer(const char *Path) { inline Poco::Net::HTTPRequestHandler *CallServer(const char *Path, uint64_t Id) {
RESTAPIHandler::BindingMap Bindings; RESTAPIHandler::BindingMap Bindings;
return RESTAPI_internal_server(Path, Bindings, Logger_, Server_); return RESTAPI_IntRouter(Path, Bindings, Logger_, Server_, Id);
} }
private: private:
std::vector<std::unique_ptr<Poco::Net::HTTPServer>> RESTServers_; std::vector<std::unique_ptr<Poco::Net::HTTPServer>> RESTServers_;
Poco::ThreadPool Pool_; Poco::ThreadPool Pool_;
RESTAPI_GenericServer Server_; RESTAPI_GenericServer Server_;
RESTAPI_InternalServer() noexcept: SubSystemServer("RESTAPIInternalServer", "REST-ISRV", "openwifi.internal.restapi") RESTAPI_IntServer() noexcept: SubSystemServer("RESTAPI_IntServer", "REST-ISRV", "openwifi.internal.restapi")
{ {
} }
}; };
inline RESTAPI_InternalServer * RESTAPI_InternalServer() { return RESTAPI_InternalServer::instance(); }; inline RESTAPI_IntServer * RESTAPI_IntServer() { return RESTAPI_IntServer::instance(); };
class InternalRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory { class IntRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory {
public: public:
InternalRequestHandlerFactory(RESTAPI_GenericServer & Server) : explicit IntRequestHandlerFactory(RESTAPI_GenericServer & Server) :
Logger_(RESTAPI_InternalServer()->Logger()), Logger_(RESTAPI_IntServer()->Logger()),
Server_(Server){} Server_(Server){}
inline Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override { inline Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override {
Poco::URI uri(Request.getURI()); Poco::URI uri(Request.getURI());
auto *Path = uri.getPath().c_str(); auto *Path = uri.getPath().c_str();
return RESTAPI_InternalServer()->CallServer(Path); return RESTAPI_IntServer()->CallServer(Path, TransactionId_);
} }
private: private:
Poco::Logger & Logger_; static inline std::atomic_uint64_t TransactionId_ = 1;
RESTAPI_GenericServer & Server_; Poco::Logger &Logger_;
RESTAPI_GenericServer &Server_;
}; };
inline int RESTAPI_InternalServer::Start() { inline int RESTAPI_IntServer::Start() {
Logger_.information("Starting."); Logger_.information("Starting.");
Server_.InitLogging(); Server_.InitLogging();
@@ -2567,7 +2573,7 @@ namespace OpenWifi {
Params->setMaxQueued(200); Params->setMaxQueued(200);
Params->setKeepAlive(true); Params->setKeepAlive(true);
auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new InternalRequestHandlerFactory(Server_), Pool_, Sock, Params); auto NewServer = std::make_unique<Poco::Net::HTTPServer>(new IntRequestHandlerFactory(Server_), Pool_, Sock, Params);
NewServer->start(); NewServer->start();
RESTServers_.push_back(std::move(NewServer)); RESTServers_.push_back(std::move(NewServer));
} }
@@ -2848,8 +2854,8 @@ namespace OpenWifi {
// add the default services // add the default services
SubSystems_.push_back(KafkaManager()); SubSystems_.push_back(KafkaManager());
SubSystems_.push_back(ALBHealthCheckServer()); SubSystems_.push_back(ALBHealthCheckServer());
SubSystems_.push_back(RESTAPI_server()); SubSystems_.push_back(RESTAPI_ExtServer());
SubSystems_.push_back(RESTAPI_InternalServer()); SubSystems_.push_back(RESTAPI_IntServer());
Poco::Net::initializeSSL(); Poco::Net::initializeSSL();
Poco::Net::HTTPStreamFactory::registerFactory(); Poco::Net::HTTPStreamFactory::registerFactory();
@@ -3419,14 +3425,14 @@ namespace OpenWifi {
Consumer.unsubscribe(); Consumer.unsubscribe();
} }
inline void RESTAPI_server::reinitialize(Poco::Util::Application &self) { inline void RESTAPI_ExtServer::reinitialize(Poco::Util::Application &self) {
MicroService::instance().LoadConfigurationFile(); MicroService::instance().LoadConfigurationFile();
Logger_.information("Reinitializing."); Logger_.information("Reinitializing.");
Stop(); Stop();
Start(); Start();
} }
void RESTAPI_InternalServer::reinitialize(Poco::Util::Application &self) { void RESTAPI_IntServer::reinitialize(Poco::Util::Application &self) {
MicroService::instance().LoadConfigurationFile(); MicroService::instance().LoadConfigurationFile();
Logger_.information("Reinitializing."); Logger_.information("Reinitializing.");
Stop(); Stop();
@@ -3435,12 +3441,13 @@ namespace OpenWifi {
class RESTAPI_system_command : public RESTAPIHandler { class RESTAPI_system_command : public RESTAPIHandler {
public: public:
RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal) RESTAPI_system_command(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L, : RESTAPIHandler(bindings, L,
std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_POST, std::vector<std::string>{Poco::Net::HTTPRequest::HTTP_POST,
Poco::Net::HTTPRequest::HTTP_GET, Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS}, Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server, Server,
TransactionId,
Internal) {} Internal) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/system"};} static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/system"};}
@@ -3750,7 +3757,7 @@ namespace OpenWifi {
if (AuthClient()->IsAuthorized( SessionToken_, UserInfo_, Expired, Sub)) { if (AuthClient()->IsAuthorized( SessionToken_, UserInfo_, Expired, Sub)) {
#endif #endif
if(Server_.LogIt(Request->getMethod(),true)) { if(Server_.LogIt(Request->getMethod(),true)) {
Logger_.debug(Poco::format("X-REQ-ALLOWED(%s): User='%s@%s' Method='%s' Path='%s", Logger_.debug(Poco::format("X-REQ-ALLOWED(%s): User='%s@%s' Method='%s' Path='%s'",
UserInfo_.userinfo.email, UserInfo_.userinfo.email,
Utils::FormatIPv6(Request->clientAddress().toString()), Utils::FormatIPv6(Request->clientAddress().toString()),
Request->clientAddress().toString(), Request->clientAddress().toString(),
@@ -3760,7 +3767,7 @@ namespace OpenWifi {
return true; return true;
} else { } else {
if(Server_.LogBadTokens(true)) { if(Server_.LogBadTokens(true)) {
Logger_.debug(Poco::format("X-REQ-DENIED(%s): Method='%s' Path='%s", Logger_.debug(Poco::format("X-REQ-DENIED(%s): Method='%s' Path='%s'",
Utils::FormatIPv6(Request->clientAddress().toString()), Utils::FormatIPv6(Request->clientAddress().toString()),
Request->getMethod(), Request->getURI())); Request->getMethod(), Request->getURI()));
} }