Skip to content

Commit

Permalink
Added comments & Rename PageController to StaticResourceController
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Oct 13, 2023
1 parent ea4b57a commit 01fd1a0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 71 deletions.
4 changes: 2 additions & 2 deletions backend/src/App.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "./AppComponent.hpp"
#include "./controller/PageController.cc"
#include "./controller/StaticResourceController.cc"

#include "oatpp/network/Server.hpp"

Expand All @@ -14,7 +14,7 @@ void run() {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);

/* Create RoomsController and add all of its endpoints to router */
router->addController(std::make_shared<PageController>());
router->addController(std::make_shared<StaticResourceController>());

/* Get connection handler component */
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler, "http");
Expand Down
2 changes: 1 addition & 1 deletion backend/src/AppComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AppComponent {
* Create ConnectionProvider component which listens on the port
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, 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);
}());

/**
Expand Down
68 changes: 0 additions & 68 deletions backend/src/controller/PageController.cc

This file was deleted.

72 changes: 72 additions & 0 deletions backend/src/controller/StaticResourceController.cc
Original file line number Diff line number Diff line change
@@ -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>, 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 */

0 comments on commit 01fd1a0

Please sign in to comment.