From 862766de719b9580451aa3e07d678ff1205d5eed Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 14 Aug 2023 22:55:43 -0400 Subject: [PATCH] Added crow log, rename curl class / functions --- include/crowsite/requests/curl.h | 24 ++++++++---- libs/BLT | 2 +- src/crowsite/requests/curl.cpp | 17 ++++++--- src/main.cpp | 65 +++++++++++++++++++++++++------- 4 files changed, 80 insertions(+), 28 deletions(-) diff --git a/include/crowsite/requests/curl.h b/include/crowsite/requests/curl.h index 11d6a04..f97884e 100644 --- a/include/crowsite/requests/curl.h +++ b/include/crowsite/requests/curl.h @@ -8,23 +8,33 @@ #include #include -namespace cs { - - void init(); - void cleanup(); +namespace cs +{ - class request { + namespace requests + { + void init(); + + void cleanup(); + } + + class request + { private: CURL* handler = nullptr; struct curl_slist* headers = nullptr; public: request(); + void setAuthHeader(const std::string& header); + void get(const std::string& domain); + void post(const std::string& domain); - ~easyrequest_get(); + + ~request(); }; - + } #endif //CROWSITE_CURL_H diff --git a/libs/BLT b/libs/BLT index eba9ecb..a3f6757 160000 --- a/libs/BLT +++ b/libs/BLT @@ -1 +1 @@ -Subproject commit eba9ecb9e525c954e956ae7e25504654af9d8eaf +Subproject commit a3f67571460d23fe819a3a3a572344e7030fba4a diff --git a/src/crowsite/requests/curl.cpp b/src/crowsite/requests/curl.cpp index 42e0bba..448cf1b 100644 --- a/src/crowsite/requests/curl.cpp +++ b/src/crowsite/requests/curl.cpp @@ -21,7 +21,7 @@ namespace cs { responses[site] = response; } - void init() + void requests::init() { auto code = curl_global_init(CURL_GLOBAL_ALL); if (code) @@ -31,23 +31,23 @@ namespace cs { } } - void cleanup() + void requests::cleanup() { curl_global_cleanup(); } - easy_get::easy_get() + request::request() { handler = curl_easy_init(); } - easy_get::~easy_get() + request::~request() { curl_slist_free_all(headers); curl_easy_cleanup(handler); } - void easy_get::setAuthHeader(const std::string& header) + void request::setAuthHeader(const std::string& header) { curl_slist_free_all(headers); headers = curl_slist_append(headers, "Content-Type: application/json"); @@ -56,7 +56,7 @@ namespace cs { curl_easy_setopt(handler, CURLOPT_HTTPHEADER, headers); } - void easy_get::request(const std::string& domain) + void request::get(const std::string& domain) { curl_easy_setopt(handler, CURLOPT_URL, domain.c_str()); curl_easy_setopt(handler, CURLOPT_WRITEDATA, domain.c_str()); @@ -68,4 +68,9 @@ namespace cs { BLT_ERROR("CURL failed to send request '%s'. Error '%s'", domain.c_str(), curl_easy_strerror(err)); } } + + void request::post(const std::string& domain) + { + + } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e07830c..4edb600 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,8 +10,39 @@ #include #include -int main(int argc, const char** argv) { - blt::logging::setLogOutputFormat("\033[94m[${{FULL_TIME}}]${{RC}} ${{LF}}[${{LOG_LEVEL}}]${{RC}} \033[35m(${{FILE}}:${{LINE}})${{RC}} ${{CNR}}${{STR}}${{RC}}\n"); +class BLT_CrowLogger : public crow::ILogHandler +{ + public: + void log(std::string message, crow::LogLevel crow_level) final + { + blt::logging::log_level blt_level; + switch (crow_level){ + case crow::LogLevel::DEBUG: + blt_level = blt::logging::log_level::DEBUG; + break; + case crow::LogLevel::INFO: + blt_level = blt::logging::log_level::INFO; + break; + case crow::LogLevel::WARNING: + blt_level = blt::logging::log_level::WARN; + break; + case crow::LogLevel::ERROR: + blt_level = blt::logging::log_level::ERROR; + break; + case crow::LogLevel::CRITICAL: + blt_level = blt::logging::log_level::FATAL; + break; + } + BLT_LOG("Crow: %s", blt_level, message.c_str()); + } +}; + + +int main(int argc, const char** argv) +{ + blt::logging::setLogOutputFormat( + "\033[94m[${{FULL_TIME}}]${{RC}} ${{LF}}[${{LOG_LEVEL}}]${{RC}} \033[35m(${{FILE}}:${{LINE}})${{RC}} ${{CNR}}${{STR}}${{RC}}\n" + ); cs::requests::init(); blt::arg_parse parser; @@ -70,12 +101,14 @@ int main(int argc, const char** argv) { // // return 0; - cs::requests::easy_get get; + cs::request get; get.setAuthHeader("MediaBrowser Client=Crowsite, Device=YourMom, Token=" + blt::arg_parse::get(args["token"])); - get.request("https://media.tpgc.me/Auth/Keys"); - + get.get("https://media.tpgc.me/Auth/Keys"); + BLT_INFO("Starting site %s.", SITE_NAME); crow::mustache::set_global_base(SITE_FILES_PATH); + static BLT_CrowLogger bltCrowLogger{}; + crow::logger::setHandler(&bltCrowLogger); BLT_INFO("Init Crow with compression and logging enabled!"); crow::SimpleApp app; @@ -97,12 +130,14 @@ int main(int argc, const char** argv) { BLT_INFO("Creating routes"); - CROW_ROUTE(app, "/favicon.ico")([](crow::response& local_fav_res) { - local_fav_res.compressed = false; - local_fav_res.set_static_file_info_unsafe(cs::fs::createStaticFilePath("images/favicon.ico")); - local_fav_res.set_header("content-type", "image/x-icon"); - local_fav_res.end(); - }); + CROW_ROUTE(app, "/favicon.ico")( + [](crow::response& local_fav_res) { + local_fav_res.compressed = false; + local_fav_res.set_static_file_info_unsafe(cs::fs::createStaticFilePath("images/favicon.ico")); + local_fav_res.set_header("content-type", "image/x-icon"); + local_fav_res.end(); + } + ); CROW_ROUTE(app, "/")( [&](const std::string& name) -> crow::response { @@ -128,9 +163,11 @@ int main(int argc, const char** argv) { } ); - CROW_ROUTE(app, "/")([&engine]() { - return engine.fetch("home.html"); - }); + CROW_ROUTE(app, "/")( + [&engine]() { + return engine.fetch("home.html"); + } + ); CROW_CATCHALL_ROUTE(app)( [&engine]() {