Skip to content

Commit

Permalink
Added support of the base path for endpoints registration
Browse files Browse the repository at this point in the history
  • Loading branch information
EDDragonWolf committed May 13, 2022
1 parent 2f97535 commit 45180e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/oatpp/web/server/HttpRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,27 @@ std::shared_ptr<HttpRouter> HttpRouter::createShared() {
return std::make_shared<HttpRouter>();
}

void HttpRouter::route(const std::shared_ptr<server::api::Endpoint>& endpoint) {
route(endpoint->info()->method, endpoint->info()->path, endpoint->handler);
void HttpRouter::route(const std::shared_ptr<server::api::Endpoint>& endpoint, const String& basePath) {
const String base = basePath ? basePath : "";
String path = endpoint->info()->path;
if((base->size() > 0 && base->data()[base->size() - 1] == '/') || path->data()[0] == '/') {
path = base + path;
} else {
path = base + "/" + path;
}
route(endpoint->info()->method, path, endpoint->handler);
}

void HttpRouter::route(const server::api::Endpoints& endpoints) {
void HttpRouter::route(const server::api::Endpoints& endpoints, const String& basePath) {
for(auto& e : endpoints.list) {
route(e);
route(e, basePath);
}
}

std::shared_ptr<server::api::ApiController> HttpRouter::addController(const std::shared_ptr<server::api::ApiController>& controller) {
std::shared_ptr<server::api::ApiController> HttpRouter::addController(
const std::shared_ptr<server::api::ApiController>& controller, const String& basePath) {
m_controllers.push_back(controller);
route(controller->getEndpoints());
route(controller->getEndpoints(), basePath);
return controller;
}

Expand Down
8 changes: 5 additions & 3 deletions src/oatpp/web/server/HttpRouter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ class HttpRouter : public HttpRouterTemplate<std::shared_ptr<HttpRequestHandler>
static std::shared_ptr<HttpRouter> createShared();

using HttpRouterTemplate::route;
void route(const std::shared_ptr<server::api::Endpoint>& endpoint);
void route(const server::api::Endpoints& endpoints);
void route(const std::shared_ptr<server::api::Endpoint>& endpoint, const String& basePath = String());
void route(const server::api::Endpoints& endpoints, const String& basePath = String());

/**
* Add controller and route its' endpoints.
* @param controller
* @return - `std::shared_ptr` to the controller added.
*/
std::shared_ptr<server::api::ApiController> addController(const std::shared_ptr<server::api::ApiController>& controller);
std::shared_ptr<server::api::ApiController> addController(
const std::shared_ptr<server::api::ApiController>& controller,
const String& basePath = String());

};

Expand Down

0 comments on commit 45180e8

Please sign in to comment.