diff --git a/lib/framework/HttpEndpoint.h b/lib/framework/HttpEndpoint.h index 859003689..aa8bdea54 100644 --- a/lib/framework/HttpEndpoint.h +++ b/lib/framework/HttpEndpoint.h @@ -14,33 +14,37 @@ using namespace std::placeholders; // for `_1` etc template -class HttpGetEndpoint { +class HttpEndpoint { + protected: + JsonStateReader _stateReader; + JsonStateUpdater _stateUpdater; + StatefulService * _statefulService; + size_t _bufferSize; + SecurityManager * _securityManager; + AuthenticationPredicate _authenticationPredicate; + PsychicHttpServer * _server; + const char * _servicePath; + public: - HttpGetEndpoint(JsonStateReader stateReader, - StatefulService * statefulService, - PsychicHttpServer * server, - const char * servicePath, - SecurityManager * securityManager, - AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN, - size_t bufferSize = DEFAULT_BUFFER_SIZE) + HttpEndpoint(JsonStateReader stateReader, + JsonStateUpdater stateUpdater, + StatefulService * statefulService, + PsychicHttpServer * server, + const char * servicePath, + SecurityManager * securityManager, + AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN, + size_t bufferSize = DEFAULT_BUFFER_SIZE) : _stateReader(stateReader) + , _stateUpdater(stateUpdater) , _statefulService(statefulService) - , _bufferSize(bufferSize) , _server(server) , _servicePath(servicePath) , _securityManager(securityManager) - , _authenticationPredicate(authenticationPredicate) { + , _authenticationPredicate(authenticationPredicate) + , _bufferSize(bufferSize) { } - protected: - JsonStateReader _stateReader; - StatefulService * _statefulService; - size_t _bufferSize; - PsychicHttpServer * _server; - const char * _servicePath; - SecurityManager * _securityManager; - AuthenticationPredicate _authenticationPredicate; - + // register the web server on() endpoints void registerURI() { _server->on(_servicePath, HTTP_GET, @@ -52,42 +56,7 @@ class HttpGetEndpoint { return response.send(); }, _authenticationPredicate)); - } -}; -template -class HttpPostEndpoint { - public: - HttpPostEndpoint(JsonStateReader stateReader, - JsonStateUpdater stateUpdater, - StatefulService * statefulService, - PsychicHttpServer * server, - const char * servicePath, - SecurityManager * securityManager, - AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN, - size_t bufferSize = DEFAULT_BUFFER_SIZE) - : _stateReader(stateReader) - , _stateUpdater(stateUpdater) - , _statefulService(statefulService) - , _server(server) - , _servicePath(servicePath) - , _securityManager(securityManager) - , _authenticationPredicate(authenticationPredicate) - , _bufferSize(bufferSize) { - } - - - protected: - JsonStateReader _stateReader; - JsonStateUpdater _stateUpdater; - StatefulService * _statefulService; - size_t _bufferSize; - SecurityManager * _securityManager; - AuthenticationPredicate _authenticationPredicate; - PsychicHttpServer * _server; - const char * _servicePath; - - void registerURI() { _server->on(_servicePath, HTTP_POST, _securityManager->wrapCallback( @@ -102,10 +71,8 @@ class HttpPostEndpoint { if (outcome == StateUpdateResult::ERROR) { return request->reply(400); } else if ((outcome == StateUpdateResult::CHANGED) || (outcome == StateUpdateResult::CHANGED_RESTART)) { - // TODO see if this works as intended. Before the stat was updated on an onDisconnect - // request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); }); - // TODO add support for https - _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); // persist the changes to the FS + // persist the changes to the FS + _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); } PsychicJsonResponse response = PsychicJsonResponse(request, false, _bufferSize); @@ -114,34 +81,13 @@ class HttpPostEndpoint { _statefulService->read(jsonObject, _stateReader); if (outcome == StateUpdateResult::CHANGED_RESTART) { - return request->reply(205); // reboot required + response.setCode(205); // reboot required } + return response.send(); }, _authenticationPredicate)); } }; -template -class HttpEndpoint : public HttpGetEndpoint, public HttpPostEndpoint { - public: - HttpEndpoint(JsonStateReader stateReader, - JsonStateUpdater stateUpdater, - StatefulService * statefulService, - PsychicHttpServer * server, - const char * servicePath, - SecurityManager * securityManager, - AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN, - size_t bufferSize = DEFAULT_BUFFER_SIZE) - : HttpGetEndpoint(stateReader, statefulService, server, servicePath, securityManager, authenticationPredicate, bufferSize) - , HttpPostEndpoint(stateReader, stateUpdater, statefulService, server, servicePath, securityManager, authenticationPredicate, bufferSize) { - } - - // register the web server on() endpoints - void registerURI() { - HttpGetEndpoint::registerURI(); - HttpPostEndpoint::registerURI(); - } -}; - #endif diff --git a/lib_standalone/HttpEndpoint.h b/lib_standalone/HttpEndpoint.h index 859003689..ca3293b09 100644 --- a/lib_standalone/HttpEndpoint.h +++ b/lib_standalone/HttpEndpoint.h @@ -102,9 +102,6 @@ class HttpPostEndpoint { if (outcome == StateUpdateResult::ERROR) { return request->reply(400); } else if ((outcome == StateUpdateResult::CHANGED) || (outcome == StateUpdateResult::CHANGED_RESTART)) { - // TODO see if this works as intended. Before the stat was updated on an onDisconnect - // request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); }); - // TODO add support for https _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); // persist the changes to the FS }