Skip to content

Commit

Permalink
feat: add custom api path to swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
Bycob authored and mergify[bot] committed Mar 15, 2023
1 parent 07f6ff3 commit 4fe0df7
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 9 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,8 @@ if (USE_TENSORRT)
endif()

set(OATPP_VERSION "1.3.0")
set(OATPP_SWAGGER_VERSION "1.3.0")
# 1.3 + allow to change api path
set(OATPP_SWAGGER_VERSION "ed5251c580e2e98beb50d818bcea8ddc91419d8c")
# 1.3 + memory leak patch
set(OATPP_ZLIB_VERSION "e7318068ae1b24b7d690f374ffbcd9c8f691336b")
ExternalProject_Add(
Expand Down Expand Up @@ -1073,7 +1074,7 @@ if(USE_OATPP_SWAGGER)
oatpp-swagger
PREFIX oatpp-swagger
URL https://github.com/oatpp/oatpp-swagger/archive/${OATPP_SWAGGER_VERSION}.tar.gz
URL_HASH SHA256=4d8db6d73095a786a521a71e8ba6b535fea62237faaaa57d98257312627d3a45
URL_HASH SHA256=afe026d6ba10f5cf43e65578acb80bb82f189d311ed6a0d7c4b8f4bd2b07e55f
BUILD_IN_SOURCE true
INSTALL_COMMAND ""
CMAKE_ARGS
Expand Down
2 changes: 1 addition & 1 deletion docker/cpu-arm.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ WORKDIR /opt/deepdetect/build/main
VOLUME ["/data"]

# Set entrypoint
CMD ./dede -host 0.0.0.0
ENTRYPOINT ["./dede", "-host", "0.0.0.0"]
EXPOSE 8080
2 changes: 1 addition & 1 deletion docker/cpu.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,6 @@ RUN /opt/deepdetect/get_models.sh
RUN /opt/deepdetect/check-dede-deps.sh

WORKDIR /opt/deepdetect/build/main
CMD /opt/deepdetect/start-dede.sh -host 0.0.0.0
ENTRYPOINT ["/opt/deepdetect/start-dede.sh", "-host", "0.0.0.0"]
VOLUME ["/data"]
EXPOSE 8080
2 changes: 1 addition & 1 deletion docker/gpu.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,6 @@ RUN /opt/deepdetect/get_models.sh
RUN /opt/deepdetect/check-dede-deps.sh

WORKDIR /opt/deepdetect/build/main
CMD /opt/deepdetect/start-dede.sh -host 0.0.0.0
ENTRYPOINT ["/opt/deepdetect/start-dede.sh", "-host", "0.0.0.0"]
VOLUME ["/data"]
EXPOSE 8080
2 changes: 1 addition & 1 deletion docker/gpu_tensorrt.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,6 @@ RUN /opt/deepdetect/get_models.sh
RUN /opt/deepdetect/check-dede-deps.sh

WORKDIR /opt/deepdetect/build/main
CMD /opt/deepdetect/start-dede.sh -host 0.0.0.0
ENTRYPOINT ["/opt/deepdetect/start-dede.sh", "-host", "0.0.0.0"]
VOLUME ["/data"]
EXPOSE 8080
1 change: 1 addition & 0 deletions docker/start-dede.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
set -x
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/deepdetect/build/lib"
exec /opt/deepdetect/build/main/dede "$@"
10 changes: 10 additions & 0 deletions docs/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ docker run -d -p 8080:8080 -v /path/to/volume:/opt/deepdetect jolibrain/deepdete
```
where `/path/to/volume` is the path to your local volume that you'd like to attach to `/opt/deepdetect`. This is useful for sharing / saving models, etc...

### Access documentation with custom API endpoint

Given a running server on 0.0.0.0:8080, the documentation is served at `http://hostname:8080/swagger/ui`.

If dede is behind a custom API endpoint, e.g. `hostname:port/api/deepdetect`, swagger links won't work correctly. You can get around this by specifying the api path when you run the docker:
```bash
docker run -d -p 8080:8080 jolibrain/deepdetect_cpu -swagger_api_prefix api/deepdetect/
```
You can now serve DeepDetect on `hostname:port/api/deepdetect/` and expect the links in swagger documentation to work correctly.

## Building docker images

See https://github.com/jolibrain/deepdetect/tree/master/docker/README.md
12 changes: 12 additions & 0 deletions src/http/swagger_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
#define HTTP_SWAGGERCOMPONENT_HPP

#include <iostream>
#include <gflags/gflags.h>

#include "oatpp-swagger/Model.hpp"
#include "oatpp-swagger/Resources.hpp"
#include "oatpp-swagger/ControllerPaths.hpp"
#include "oatpp/core/macro/component.hpp"

#include "dd_config.h"

DECLARE_string(swagger_api_prefix);

class SwaggerComponent
{
public:
Expand All @@ -54,6 +58,14 @@ class SwaggerComponent
return builder.build();
}());

OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::ControllerPaths>,
controllerPaths)
([] {
auto paths = std::make_shared<oatpp::swagger::ControllerPaths>();
paths->apiJson = FLAGS_swagger_api_prefix + paths->apiJson;
return paths;
}());

/**
* Swagger-Ui Resources (<oatpp-examples>/lib/oatpp-swagger/res)
*/
Expand Down
49 changes: 49 additions & 0 deletions src/http/swagger_controller.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* DeepDetect
* Copyright (c) 2023 Jolibrain SASU
* Author: Louis Jean <louis.jean@jolibrain.com>
*
* This file is part of deepdetect.
*
* deepdetect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* deepdetect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
*/

#include "oatpp-swagger/Controller.hpp"

namespace dd
{
class DedeSwaggerController : public oatpp::swagger::Controller
{
public:
DedeSwaggerController(
const std::shared_ptr<oatpp::data::mapping::ObjectMapper>
&objectMapper,
const oatpp::Object<oatpp::swagger::oas3::Document> &document,
const std::shared_ptr<oatpp::swagger::Resources> &resources,
const oatpp::swagger::ControllerPaths &paths)
: oatpp::swagger::Controller(objectMapper, document, resources, paths)
{
}

public:
#include OATPP_CODEGEN_BEGIN(ApiController)

ENDPOINT("GET", "api-docs/oas-3.0.0.json", api2)
{
return api();
}

#include OATPP_CODEGEN_END(ApiController)
};
}
26 changes: 23 additions & 3 deletions src/oatppjsonapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/core/macro/component.hpp"
#ifdef USE_OATPP_SWAGGER
#include "oatpp-swagger/Controller.hpp"
#include "http/swagger_controller.hpp"

DEFINE_string(
swagger_api_prefix, "",
"server api prefix in documentation. The prefix should not start "
"with \"/\" and should end with \"/\", e.g. `api/private/`.");
#endif

#include "utils/oatpp.hpp"
Expand Down Expand Up @@ -276,6 +281,15 @@ namespace dd
return response;
}

void OatppJsonAPI::addPrefixToEndpoints(
oatpp::web::server::api::Endpoints &endpoints, const std::string &prefix)
{
for (auto &endpoint : endpoints.list)
{
endpoint->info()->path = prefix + endpoint->info()->path;
}
}

OatppJsonAPI::Response_ptr
OatppJsonAPI::response_bad_request_400(const std::string &msg) const
{
Expand Down Expand Up @@ -349,10 +363,16 @@ namespace dd
// Initialize swagger
oatpp::swagger::Generator::Endpoints docEndpoints;
docEndpoints.append(dedeController->getEndpoints());
if (!FLAGS_swagger_api_prefix.empty())
{
addPrefixToEndpoints(docEndpoints, FLAGS_swagger_api_prefix);
}

OATPP_COMPONENT(std::shared_ptr<oatpp::swagger::DocumentInfo>,
documentInfo);
OATPP_COMPONENT(std::shared_ptr<oatpp::swagger::Resources>, resources);
OATPP_COMPONENT(std::shared_ptr<oatpp::swagger::ControllerPaths>,
swaggerPaths);

std::shared_ptr<oatpp::swagger::Generator::Config> generatorConfig;
try
Expand All @@ -372,8 +392,8 @@ namespace dd
auto swaggerMapper = dd::oatpp_utils::createDDMapper();
swaggerMapper->getSerializer()->getConfig()->includeNullFields = false;
swaggerMapper->getDeserializer()->getConfig()->allowUnknownFields = false;
auto swaggerController = std::make_shared<oatpp::swagger::Controller>(
swaggerMapper, document, resources);
auto swaggerController = std::make_shared<dd::DedeSwaggerController>(
swaggerMapper, document, resources, *swaggerPaths);
router->addController(swaggerController);
#endif

Expand Down
3 changes: 3 additions & 0 deletions src/oatppjsonapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ namespace dd
const uint32_t &dd_code = 0,
const std::string &dd_msg = "") const;

void addPrefixToEndpoints(oatpp::web::server::api::Endpoints &endpoints,
const std::string &prefix);

// Oatpp responses
Response_ptr response_bad_request_400(const std::string &msg = "") const;
Response_ptr response_not_found_404() const;
Expand Down

0 comments on commit 4fe0df7

Please sign in to comment.