Skip to content

Commit

Permalink
fix: swagger getting configuration from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Dec 17, 2024
1 parent 52acbfa commit c6523e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
11 changes: 9 additions & 2 deletions engine/controllers/swagger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ constexpr auto ScalarUi = R"(
</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"] = host_ + ":" + port_;
Json::Value resp_data(Json::arrayValue);
resp_data.append(server_url);

root["servers"] = resp_data;
return root;
}

Expand All @@ -41,7 +48,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);
}
15 changes: 12 additions & 3 deletions engine/controllers/swagger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

using namespace drogon;

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

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 +26,10 @@ class SwaggerController : public drogon::HttpController<SwaggerController> {
std::function<void(const drogon::HttpResponsePtr&)>&& callback) const;

private:
std::string host_;
std::string port_;

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

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 c6523e9

Please sign in to comment.