From c6523e93f635b45f9d623eede31fba8b32e5948d Mon Sep 17 00:00:00 2001 From: James Date: Tue, 17 Dec 2024 13:10:01 +0700 Subject: [PATCH] fix: swagger getting configuration from config file --- engine/controllers/swagger.cc | 11 +++++++++-- engine/controllers/swagger.h | 15 ++++++++++++--- engine/main.cc | 4 ++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/engine/controllers/swagger.cc b/engine/controllers/swagger.cc index 96a6c3837..386ae3574 100644 --- a/engine/controllers/swagger.cc +++ b/engine/controllers/swagger.cc @@ -22,10 +22,17 @@ constexpr auto ScalarUi = R"( )"; -Json::Value SwaggerController::generateOpenAPISpec() { +Json::Value SwaggerController::GenerateOpenApiSpec() const { Json::Value root; Json::Reader reader; reader.parse(CortexOpenApi::GetOpenApiJson(), root); + + Json::Value server_url; + server_url["url"] = host_ + ":" + port_; + Json::Value resp_data(Json::arrayValue); + resp_data.append(server_url); + + root["servers"] = resp_data; return root; } @@ -41,7 +48,7 @@ void SwaggerController::serveSwaggerUI( void SwaggerController::serveOpenAPISpec( const drogon::HttpRequestPtr& req, std::function&& callback) const { - Json::Value spec = generateOpenAPISpec(); + auto spec = GenerateOpenApiSpec(); auto resp = cortex_utils::CreateCortexHttpJsonResponse(spec); callback(resp); } diff --git a/engine/controllers/swagger.h b/engine/controllers/swagger.h index 4099bc447..32fe6777a 100644 --- a/engine/controllers/swagger.h +++ b/engine/controllers/swagger.h @@ -5,13 +5,18 @@ using namespace drogon; -class SwaggerController : public drogon::HttpController { +class SwaggerController + : public drogon::HttpController { + public: METHOD_LIST_BEGIN ADD_METHOD_TO(SwaggerController::serveSwaggerUI, "/", Get); ADD_METHOD_TO(SwaggerController::serveOpenAPISpec, "/openapi.json", Get); METHOD_LIST_END + explicit SwaggerController(const std::string& host, const std::string& port) + : host_{host}, port_{port} {}; + void serveSwaggerUI( const drogon::HttpRequestPtr& req, std::function&& callback) const; @@ -21,6 +26,10 @@ class SwaggerController : public drogon::HttpController { std::function&& callback) const; private: + std::string host_; + std::string port_; + static const std::string swaggerUIHTML; - static Json::Value generateOpenAPISpec(); -}; \ No newline at end of file + + Json::Value GenerateOpenApiSpec() const; +}; diff --git a/engine/main.cc b/engine/main.cc index 8ca5ffd1f..b79859ef3 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -11,6 +11,7 @@ #include "controllers/models.h" #include "controllers/process_manager.h" #include "controllers/server.h" +#include "controllers/swagger.h" #include "controllers/threads.h" #include "database/database.h" #include "migrations/migration_manager.h" @@ -155,6 +156,8 @@ void RunServer(std::optional port, bool ignore_cout) { file_watcher_srv->start(); // initialize custom controllers + auto swagger_ctl = std::make_shared(config.apiServerHost, + config.apiServerPort); auto file_ctl = std::make_shared(file_srv, message_srv); auto assistant_ctl = std::make_shared(assistant_srv); auto thread_ctl = std::make_shared(thread_srv, message_srv); @@ -169,6 +172,7 @@ void RunServer(std::optional port, bool ignore_cout) { std::make_shared(inference_svc, engine_service); auto config_ctl = std::make_shared(config_service); + drogon::app().registerController(swagger_ctl); drogon::app().registerController(file_ctl); drogon::app().registerController(assistant_ctl); drogon::app().registerController(thread_ctl);