-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19c26e1
commit ea3f08d
Showing
35 changed files
with
295 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
project(ton-http-api-cpp CXX) | ||
|
||
set(USERVER_FEATURE_REDIS TRUE) | ||
set(USERVER_FEATURE_UBOOST_CORO TRUE) | ||
userver_setup_environment() | ||
|
||
set(TON_HTTP_API_CPP_SOURCE | ||
main.cpp | ||
handlers.hpp | ||
handlers.cpp | ||
tonlib_component.cpp | ||
tonlib_component.h | ||
tonlib_worker.cpp | ||
tonlib_worker.h | ||
handler_api_v2.cpp | ||
handler_api_v2.h | ||
) | ||
|
||
add_executable(${PROJECT_NAME} ${TON_HTTP_API_CPP_SOURCE}) | ||
target_include_directories(${PROJECT_NAME} | ||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../external/userver | ||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../external/ton | ||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/.. | ||
) | ||
#target_link_directories(ton-http-api-cpp | ||
# PRIVATE external/userver | ||
# PRIVATE external/ton | ||
#) | ||
|
||
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) | ||
target_link_libraries(${PROJECT_NAME} userver::core tonlib::multiclient) | ||
# target_link_options(ton-http-api-cpp PUBLIC -rdynamic) | ||
|
||
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "handler_api_v2.h" | ||
|
||
#include "tl/tl_json.h" | ||
#include "auto/tl/tonlib_api.h" | ||
#include "auto/tl/tonlib_api_json.h" | ||
#include "td/utils/JsonBuilder.h" | ||
#include "tonlib-multiclient/request.h" | ||
#include "userver/components/component_context.hpp" | ||
|
||
namespace ton_http::handlers { | ||
std::string ApiV2Handler::HandleRequestThrow( | ||
const userver::server::http::HttpRequest& request, userver::server::request::RequestContext& context | ||
) const { | ||
request.GetHttpResponse().SetContentType(userver::http::content_type::kApplicationJson); | ||
auto& ton_api_method = request.GetPathArg("ton_api_method"); | ||
LOG_WARNING() << "Got request " << ton_api_method; | ||
|
||
// call method | ||
td::StringBuilder result_sb; | ||
bool is_ok = false; | ||
if (ton_api_method == "getMasterchainInfo") { | ||
auto resp = tonlib_component_.DoRequest(multiclient::Request<ton::tonlib_api::blocks_getMasterchainInfo>{ | ||
.parameters = {.mode = multiclient::RequestMode::Multiple, .clients_number = 1}, | ||
.request_creator = [] { | ||
return ton::tonlib_api::blocks_getMasterchainInfo(); | ||
}, | ||
}); | ||
if (resp.is_error()) { | ||
result_sb << resp.move_as_error(); | ||
} else { | ||
const auto res = resp.move_as_ok(); | ||
auto str = td::json_encode<td::string>(td::ToJson(res)); | ||
result_sb << str; | ||
is_ok = true; | ||
} | ||
} | ||
else { | ||
result_sb << "not implemented"; | ||
} | ||
|
||
auto result = userver::formats::json::ValueBuilder(); | ||
result["ok"] = is_ok; | ||
result[(is_ok? "result" : "error")] = userver::formats::json::FromString(result_sb.as_cslice().str()); | ||
return userver::formats::json::ToString(result.ExtractValue()); | ||
} | ||
ApiV2Handler::ApiV2Handler(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context) | ||
: HttpHandlerBase(config, context), tonlib_component_(context.FindComponent<ton_http::core::TonlibComponent>()) | ||
{} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
#include "tonlib_component.h" | ||
#include "userver/server/handlers/http_handler_base.hpp" | ||
|
||
namespace ton_http::handlers { | ||
class ApiV2Handler final : public userver::server::handlers::HttpHandlerBase { | ||
public: | ||
static constexpr std::string_view kName = "handler-api-v2"; | ||
using HttpHandlerBase::HttpHandlerBase; | ||
std::string HandleRequestThrow(const userver::server::http::HttpRequest& request, userver::server::request::RequestContext& context) const override; | ||
ApiV2Handler(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context); | ||
private: | ||
ton_http::core::TonlibComponent& tonlib_component_; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "handlers.hpp" | ||
#include "userver/formats/json.hpp" | ||
|
||
namespace ton_http::handlers { | ||
|
||
std::string HelloHandler::HandleRequestThrow(const userver::server::http::HttpRequest &request, userver::server::request::RequestContext &context) const { | ||
request.GetHttpResponse().SetContentType(userver::http::content_type::kApplicationJson); | ||
auto &name = request.GetArg("name"); | ||
|
||
auto result = userver::formats::json::ValueBuilder(); | ||
result["ok"] = true; | ||
result["result"] = "Hello!"; | ||
return userver::formats::json::ToString(result.ExtractValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include "userver/components/component_list.hpp" | ||
#include "userver/server/handlers/http_handler_base.hpp" | ||
#include "userver/server/handlers/http_handler_base.hpp" | ||
|
||
// #include "userver/utest/using_namespace_userver.hpp" | ||
|
||
namespace ton_http::handlers { | ||
|
||
class HelloHandler final : public userver::server::handlers::HttpHandlerBase { | ||
public: | ||
static constexpr std::string_view kName = "handler-hello-sample"; | ||
|
||
using HttpHandlerBase::HttpHandlerBase; | ||
std::string HandleRequestThrow(const userver::server::http::HttpRequest &request, userver::server::request::RequestContext &context) const override; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "handler_api_v2.h" | ||
#include "userver/components/minimal_server_component_list.hpp" | ||
#include "userver/server/handlers/server_monitor.hpp" | ||
#include "userver/utils/daemon_run.hpp" | ||
|
||
#include "handlers.hpp" | ||
#include "tonlib/Logging.h" | ||
#include "tonlib_component.h" | ||
|
||
int main(int argc, char* argv[]) { | ||
tonlib::Logging::set_verbosity_level(3); | ||
|
||
auto component_list = userver::components::MinimalServerComponentList(); | ||
component_list.Append<userver::server::handlers::ServerMonitor>(); | ||
component_list.Append<ton_http::core::TonlibComponent>(); | ||
component_list.Append<ton_http::handlers::ApiV2Handler>(); | ||
return userver::utils::DaemonMain(argc, argv, component_list); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "tonlib_component.h" | ||
#include "userver/components/component.hpp" | ||
#include "userver/components/component_context.hpp" | ||
#include "userver/dynamic_config/storage/component.hpp" | ||
#include "userver/dynamic_config/value.hpp" | ||
#include "userver/logging/log.hpp" | ||
#include "userver/yaml_config/merge_schemas.hpp" | ||
|
||
|
||
namespace ton_http::core { | ||
|
||
TonlibComponent::TonlibComponent( | ||
const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context | ||
) : | ||
userver::components::ComponentBase(config, context), | ||
config_(context.FindComponent<userver::components::DynamicConfig>().GetSource()), | ||
tonlib_(multiclient::MultiClientConfig{ | ||
.global_config_path = config["global_config"].As<std::string>(), | ||
.key_store_root = config["keystore"].As<std::string>(), | ||
.scheduler_threads = config["threads"].As<std::size_t>() | ||
}), | ||
task_processor_(context.GetTaskProcessor(config["task_processor"].As<std::string>())) { | ||
} | ||
const multiclient::MultiClient& TonlibComponent::GetTonlib() const { | ||
return tonlib_; | ||
} | ||
|
||
userver::yaml_config::Schema TonlibComponent::GetStaticConfigSchema() { | ||
return userver::yaml_config::MergeSchemas<userver::components::ComponentBase>(R"( | ||
type: object | ||
description: tonlib component config | ||
additionalProperties: false | ||
properties: | ||
global_config: | ||
type: string | ||
description: path to TON network config | ||
keystore: | ||
type: string | ||
description: path to Tonlib keystore | ||
threads: | ||
type: integer | ||
description: number of Tonlib threads | ||
task_processor: | ||
type: string | ||
description: task processor name | ||
)"); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.