From 964fd201c6f351267127e4d8417a7895c0d54cef Mon Sep 17 00:00:00 2001 From: Hepatica Date: Fri, 16 Aug 2024 01:14:47 +0200 Subject: [PATCH] Introduce register rest methosd --- SpCloudMain/Service/DiscordService.cpp | 82 +++++++++++++++++++++++++- SpCloudMain/Service/MongoDbService.cpp | 25 +++++--- SpCloudMain/SpCloudMain.cpp | 40 +++++++++++++ 3 files changed, 138 insertions(+), 9 deletions(-) diff --git a/SpCloudMain/Service/DiscordService.cpp b/SpCloudMain/Service/DiscordService.cpp index 960fbde..159eadd 100644 --- a/SpCloudMain/Service/DiscordService.cpp +++ b/SpCloudMain/Service/DiscordService.cpp @@ -1,12 +1,90 @@ -// ReSharper disable CppClangTidyBugproneSuspiciousInclude +// ReSharper disable CppClangTidyBugproneSuspiciousInclude + +#include +#include "../httplib.h" +#include class DiscordService { + +private: + httplib::Client client_; + public: - DiscordService() + DiscordService() : client_(httplib::Client("https://discord.com/api")) { + } + std::string get_discord_id(std::string auth_code) + { + std::string auth_code_processed = extract_code(auth_code); + + //std::string body = "client_id=1273414933874479185&" + // "client_secret=S_vG4frjlxWoi8mic_GlcxUO0aWxXwRJ&" + // "grant_type=authorization_code&" + // "code=" + auth_code_processed + "&" + // "redirect_uri=https://www.sp-donate.ru/pay/Hepatir"; + + //httplib::Headers headers = { + // {"Content-Type", "application/x-www-form-urlencoded"} + //}; + + //httplib::Client client("discord.com/api"); + ////// Выполняем простой GET-запрос на http://httpbin.org/get + ////auto res = client.Get("/get"); + //auto res = client.Post("/oauth2/token", headers, body, "application/x-www-form-urlencoded"); + ////auto res = client_.Post("/oauth2/token", headers, body, "application/x-www-form-urlencoded"); + + //if (res && res->status == 200) { + // return res->body; + //} + //else { + // return "Error: ";//Todo write handling this + //} + + + std::string command = "curl --location https://discord.com/api/oauth2/token " + "--header \"Content-Type: application/x-www-form-urlencoded\" " + "--data-urlencode \"client_id=1273414933874479185\" " + "--data-urlencode \"client_secret=S_vG4frjIxWoi8mic_GlcxUO0aWxXwRJ\" " + "--data-urlencode \"grant_type=authorization_code\" " + "--data-urlencode \"code=" + auth_code_processed + "\" " + "--data-urlencode \"redirect_uri=https://www.sp-donate.ru/pay/Hepatir\""; + + auto future = std::async(std::launch::async, &DiscordService::execute_command, this, command); + return future.get(); + } + + std::string execute_command(const std::string& command) { + std::array buffer; + std::string result; + std::shared_ptr pipe(popen(command.c_str(), "r"), pclose); + if (!pipe) throw std::runtime_error("popen() failed!"); + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { + result += buffer.data(); + } + return result; + } + + std::string extract_code(const std::string& json_str) { + std::string key = "\"code\":"; + size_t start = json_str.find(key); + + if (start != std::string::npos) { + start += key.length(); + // Пропускаем любые пробелы или символы ':' + while (start < json_str.length() && (json_str[start] == ' ' || json_str[start] == '\"' || json_str[start] == ':')) { + start++; + } + + // Найти конец строки + size_t end = json_str.find('\"', start); + if (end != std::string::npos) { + return json_str.substr(start, end - start); + } + } + return ""; } }; diff --git a/SpCloudMain/Service/MongoDbService.cpp b/SpCloudMain/Service/MongoDbService.cpp index a689989..9b37d90 100644 --- a/SpCloudMain/Service/MongoDbService.cpp +++ b/SpCloudMain/Service/MongoDbService.cpp @@ -1,8 +1,8 @@ -//// ReSharper disable CppClangTidyBugproneSuspiciousInclude -// -//#include -//#include -//#include +// ReSharper disable CppClangTidyBugproneSuspiciousInclude +//#include /* +#include + +//#include "../src/mongocxx/"s class MongoDbService { @@ -10,7 +10,18 @@ class MongoDbService public: MongoDbService() { - - + //mongocxx::instance inst{}; + //const auto uri = mongocxx::uri{ "mongodb+srv://loker:@spcloudcluster.xwpnw.mongodb.net/?retryWrites=true&w=majority&appName=SpCloudCluster" }; + //// Set the version of the Stable API on the client + //mongocxx::options::client client_options; + //const auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1 }; + //client_options.server_api_opts(api); + //// Setup the connection and get a handle on the "admin" database. + //mongocxx::client conn{ uri, client_options }; + //mongocxx::database db = conn["admin"]; + //// Ping the database. + //const auto ping_cmd = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("ping", 1)); + //db.run_command(ping_cmd.view()); + //std::cout << "Pinged your deployment. You successfully connected to MongoDB!" << std::endl; } }; diff --git a/SpCloudMain/SpCloudMain.cpp b/SpCloudMain/SpCloudMain.cpp index 5fe8f1d..7920fe9 100644 --- a/SpCloudMain/SpCloudMain.cpp +++ b/SpCloudMain/SpCloudMain.cpp @@ -6,6 +6,8 @@ #include "SpCloudMain.h" #include "httplib.h" #include "Controllers/PublishController.cpp" +#include "Service/DiscordService.cpp" + //#include "Service/AuthorizationService.cpp" //#include "Service/FileProcessingService.cpp" using namespace std; @@ -50,5 +52,43 @@ int main() res.set_content("App is running on address ????", "text/plain");//Todo add app address showing }); + DiscordService discord_service; + + svr.Post("/register", [&](const httplib::Request& req, httplib::Response& res) + { + + + string discord_id = discord_service.get_discord_id(req.body); + + res.set_content(discord_id, "text/plain"); + + /*logger.log(INFO, "Start publish from main"); + + publish_controller.process_publish(req, res);*/ + + }); + + + /*httplib::Client cli("https://discord.com/api/oauth2/token"); + + std::string jsonData = R"({ + "dataSource": "Cluster0", + "database": "myDatabase", + "collection": "items", + "document": { + "name": "Item Name", + "value": "Item Value" + } + })"; + + auto res = cli.Post("/app/data-abcde/endpoint/data/v1/action/insertOne", jsonData, "application/json"); + + if (res && res->status == 200) { + std::cout << "Success: " << res->body << std::endl; + } + else { + std::cerr << "Error: " << res.error() << std::endl; + }*/ + svr.listen("0.0.0.0", 8081); }