From c9f15a203d862fc248630bf55d80b9e23046cc92 Mon Sep 17 00:00:00 2001 From: NamH Date: Tue, 17 Dec 2024 13:49:36 +0700 Subject: [PATCH] fix: swagger getting configuration from config file (#1803) --- docs/static/openapi/cortex.json | 8 ++++---- engine/controllers/swagger.cc | 31 +++++++++-------------------- engine/controllers/swagger.h | 35 +++++++++++++++++++++++++++++---- engine/main.cc | 4 ++++ 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/docs/static/openapi/cortex.json b/docs/static/openapi/cortex.json index 4b238d2e4..9134e89e6 100644 --- a/docs/static/openapi/cortex.json +++ b/docs/static/openapi/cortex.json @@ -3092,7 +3092,7 @@ "items": { "type": "string" }, - "example": ["http://localhost:39281", "https://cortex.so"] + "example": ["http://127.0.0.1:39281", "https://cortex.so"] }, "cors": { "type": "boolean", @@ -3139,7 +3139,7 @@ }, "example": { "allowed_origins": [ - "http://localhost:39281", + "http://127.0.0.1:39281", "https://cortex.so" ], "cors": false, @@ -3180,7 +3180,7 @@ "type": "string" }, "description": "List of allowed origins.", - "example": ["http://localhost:39281", "https://cortex.so"] + "example": ["http://127.0.0.1:39281", "https://cortex.so"] }, "proxy_username": { "type": "string", @@ -3249,7 +3249,7 @@ "type": "string" }, "example": [ - "http://localhost:39281", + "http://127.0.0.1:39281", "https://cortex.so" ] }, diff --git a/engine/controllers/swagger.cc b/engine/controllers/swagger.cc index 96a6c3837..abb80b94e 100644 --- a/engine/controllers/swagger.cc +++ b/engine/controllers/swagger.cc @@ -2,30 +2,17 @@ #include "cortex_openapi.h" #include "utils/cortex_utils.h" -constexpr auto ScalarUi = R"( - - - - Cortex API Reference - - - - - - - - - -)"; - -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"] = "http://" + host_ + ":" + port_; + Json::Value resp_data(Json::arrayValue); + resp_data.append(server_url); + + root["servers"] = resp_data; return root; } @@ -41,7 +28,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..61db1cc6e 100644 --- a/engine/controllers/swagger.h +++ b/engine/controllers/swagger.h @@ -5,13 +5,38 @@ using namespace drogon; -class SwaggerController : public drogon::HttpController { +class SwaggerController + : public drogon::HttpController { + + constexpr static auto ScalarUi = R"( + + + + Cortex API Reference + + + + + + + + + +)"; + 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 +46,8 @@ class SwaggerController : public drogon::HttpController { std::function&& callback) const; private: - static const std::string swaggerUIHTML; - static Json::Value generateOpenAPISpec(); -}; \ No newline at end of file + std::string host_; + std::string port_; + + 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);