diff --git a/backend/src/App.cpp b/backend/src/App.cpp index db264c11..c188eff6 100644 --- a/backend/src/App.cpp +++ b/backend/src/App.cpp @@ -1,5 +1,5 @@ #include "./AppComponent.hpp" -#include "./controller/PageController.cc" +#include "./controller/StaticResourceController.cc" #include "oatpp/network/Server.hpp" @@ -14,7 +14,7 @@ void run() { OATPP_COMPONENT(std::shared_ptr, router); /* Create RoomsController and add all of its endpoints to router */ - router->addController(std::make_shared()); + router->addController(std::make_shared()); /* Get connection handler component */ OATPP_COMPONENT(std::shared_ptr, connectionHandler, "http"); diff --git a/backend/src/AppComponent.hpp b/backend/src/AppComponent.hpp index 65180999..ad5c99d9 100644 --- a/backend/src/AppComponent.hpp +++ b/backend/src/AppComponent.hpp @@ -33,7 +33,7 @@ class AppComponent { * Create ConnectionProvider component which listens on the port */ OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionProvider)([] { - return oatpp::network::tcp::server::ConnectionProvider::createShared({"0.0.0.0", 8000, oatpp::network::Address::IP_4}); + return oatpp::network::tcp::server::ConnectionProvider::createShared({"0.0.0.0", 8000, oatpp::network::Address::IP_4}, true); }()); /** diff --git a/backend/src/controller/PageController.cc b/backend/src/controller/PageController.cc deleted file mode 100644 index cee6b486..00000000 --- a/backend/src/controller/PageController.cc +++ /dev/null @@ -1,68 +0,0 @@ - -#ifndef PageController_cc -#define PageController_cc - -#include "fmt/format.h" -#include "spdlog/spdlog.h" - -#include "AbstractController.cc" - -#include "oatpp/core/macro/codegen.hpp" -#include "oatpp/core/macro/component.hpp" - -#include OATPP_CODEGEN_BEGIN(ApiController) - -class PageController : public AbstractController -{ -private: - typedef PageController __ControllerType; - -public: - PageController(OATPP_COMPONENT(std::shared_ptr, objectMapper)) - : AbstractController(objectMapper) - { - } - -public: - ENDPOINT_ASYNC("GET", "/", Root){ - ENDPOINT_ASYNC_INIT(Root) - - Action act() override{ - return _return(PageController::return_file_response(controller, Status::CODE_200, "frontend/public/index.html")); - } - }; - - ENDPOINT_ASYNC("GET", "/{path}", Path){ - ENDPOINT_ASYNC_INIT(Path) - - Action act() override{ - auto path = request->getPathVariable("path"); - spdlog::info("The page ({}) has been visited", std::string(path)); - std::string html_file_path = fmt::format("frontend/public/{}.html", std::string(path)); - if (PageController::exists(html_file_path)) - { - return _return(PageController::return_file_response(controller, Status::CODE_200, html_file_path)); - } - std::string file_path = fmt::format("frontend/public/{}", std::string(path)); - if (PageController::exists(file_path)) - { - return _return(PageController::return_file_response(controller, Status::CODE_200, file_path)); - } - return _return(controller->createResponse(Status::CODE_404, "404")); - } - }; - - ENDPOINT_ASYNC("GET", "/build/{path}", BuildPath){ - ENDPOINT_ASYNC_INIT(BuildPath) - - Action act() override{ - auto path = request->getPathVariable("path"); - std::string file_path = fmt::format("frontend/public/build/{}", std::string(path)); - return _return(PageController::return_file_response(controller, Status::CODE_200, file_path)); - } - }; -}; - -#include OATPP_CODEGEN_END(ApiController) //<-- codegen end - -#endif /* PageController_cc */ \ No newline at end of file diff --git a/backend/src/controller/StaticResourceController.cc b/backend/src/controller/StaticResourceController.cc new file mode 100644 index 00000000..465da21b --- /dev/null +++ b/backend/src/controller/StaticResourceController.cc @@ -0,0 +1,72 @@ + +#ifndef StaticResourceController_cc +#define StaticResourceController_cc + +#include "fmt/format.h" +#include "spdlog/spdlog.h" + +#include "AbstractController.cc" + +#include "oatpp/core/macro/codegen.hpp" +#include "oatpp/core/macro/component.hpp" + +#include OATPP_CODEGEN_BEGIN(ApiController) + +/** + * @brief For handling static pages + * + */ +class StaticResourceController : public AbstractController +{ +private: + typedef StaticResourceController __ControllerType; + +public: + StaticResourceController(OATPP_COMPONENT(std::shared_ptr, objectMapper)) + : AbstractController(objectMapper) + { + } + +public: + /** + * @brief Set the index of the page to the index html + * + */ + ENDPOINT_ASYNC("GET", "/", Root){ + ENDPOINT_ASYNC_INIT(Root) + + Action act() override{ + return _return(StaticResourceController::return_file_response(controller, Status::CODE_200, "frontend/public/index.html")); + } + }; + + /** + * @brief Try and load resource from path tail + * + */ + ENDPOINT_ASYNC("GET", "*", Path){ + ENDPOINT_ASYNC_INIT(Path) + + Action act() override{ + auto path = request->getPathTail(); + const oatpp::data::stream::Context::Properties connectionProperties + = request->getConnection()->getInputStreamContext().getProperties(); + spdlog::info("Resource [{}] has been loaded by {}:{}", std::string(path), connectionProperties.get("peer_address")->c_str(), connectionProperties.get("peer_port")->c_str()); + std::string html_file_path = fmt::format("frontend/public/{}.html", std::string(path)); + if (StaticResourceController::exists(html_file_path)) + { + return _return(StaticResourceController::return_file_response(controller, Status::CODE_200, html_file_path)); + } + std::string file_path = fmt::format("frontend/public/{}", std::string(path)); + if (StaticResourceController::exists(file_path)) + { + return _return(StaticResourceController::return_file_response(controller, Status::CODE_200, file_path)); + } + return _return(controller->createResponse(Status::CODE_404, "404")); + } + }; +}; + +#include OATPP_CODEGEN_END(ApiController) //<-- codegen end + +#endif /* StaticResourceController_cc */ \ No newline at end of file