Skip to content

Commit

Permalink
fix: swagger getting configuration from config file (#1803)
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai authored Dec 17, 2024
1 parent 52acbfa commit c9f15a2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
8 changes: 4 additions & 4 deletions docs/static/openapi/cortex.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -3139,7 +3139,7 @@
},
"example": {
"allowed_origins": [
"http://localhost:39281",
"http://127.0.0.1:39281",
"https://cortex.so"
],
"cors": false,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -3249,7 +3249,7 @@
"type": "string"
},
"example": [
"http://localhost:39281",
"http://127.0.0.1:39281",
"https://cortex.so"
]
},
Expand Down
31 changes: 9 additions & 22 deletions engine/controllers/swagger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,17 @@
#include "cortex_openapi.h"
#include "utils/cortex_utils.h"

constexpr auto ScalarUi = R"(
<!doctype html>
<html>
<head>
<title>Cortex API Reference</title>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1" />
</head>
<body>
<!-- Need a Custom Header? Check out this example https://codepen.io/scalarorg/pen/VwOXqam -->
<script
id="api-reference"
data-url="/openapi.json"></script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>
)";

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;
}

Expand All @@ -41,7 +28,7 @@ void SwaggerController::serveSwaggerUI(
void SwaggerController::serveOpenAPISpec(
const drogon::HttpRequestPtr& req,
std::function<void(const drogon::HttpResponsePtr&)>&& callback) const {
Json::Value spec = generateOpenAPISpec();
auto spec = GenerateOpenApiSpec();
auto resp = cortex_utils::CreateCortexHttpJsonResponse(spec);
callback(resp);
}
35 changes: 31 additions & 4 deletions engine/controllers/swagger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,38 @@

using namespace drogon;

class SwaggerController : public drogon::HttpController<SwaggerController> {
class SwaggerController
: public drogon::HttpController<SwaggerController, false> {

constexpr static auto ScalarUi = R"(
<!doctype html>
<html>
<head>
<title>Cortex API Reference</title>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1" />
</head>
<body>
<!-- Need a Custom Header? Check out this example https://codepen.io/scalarorg/pen/VwOXqam -->
<script
id="api-reference"
data-url="/openapi.json"></script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>
)";

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<void(const drogon::HttpResponsePtr&)>&& callback) const;
Expand All @@ -21,6 +46,8 @@ class SwaggerController : public drogon::HttpController<SwaggerController> {
std::function<void(const drogon::HttpResponsePtr&)>&& callback) const;

private:
static const std::string swaggerUIHTML;
static Json::Value generateOpenAPISpec();
};
std::string host_;
std::string port_;

Json::Value GenerateOpenApiSpec() const;
};
4 changes: 4 additions & 0 deletions engine/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -155,6 +156,8 @@ void RunServer(std::optional<int> port, bool ignore_cout) {
file_watcher_srv->start();

// initialize custom controllers
auto swagger_ctl = std::make_shared<SwaggerController>(config.apiServerHost,
config.apiServerPort);
auto file_ctl = std::make_shared<Files>(file_srv, message_srv);
auto assistant_ctl = std::make_shared<Assistants>(assistant_srv);
auto thread_ctl = std::make_shared<Threads>(thread_srv, message_srv);
Expand All @@ -169,6 +172,7 @@ void RunServer(std::optional<int> port, bool ignore_cout) {
std::make_shared<inferences::server>(inference_svc, engine_service);
auto config_ctl = std::make_shared<Configs>(config_service);

drogon::app().registerController(swagger_ctl);
drogon::app().registerController(file_ctl);
drogon::app().registerController(assistant_ctl);
drogon::app().registerController(thread_ctl);
Expand Down

0 comments on commit c9f15a2

Please sign in to comment.