Added crow log, rename curl class / functions

main
Brett 2023-08-14 22:55:43 -04:00
parent af47396334
commit 862766de71
4 changed files with 80 additions and 28 deletions

View File

@ -8,21 +8,31 @@
#include <string> #include <string>
#include <curl/curl.h> #include <curl/curl.h>
namespace cs { namespace cs
{
void init(); namespace requests
void cleanup(); {
void init();
class request { void cleanup();
}
class request
{
private: private:
CURL* handler = nullptr; CURL* handler = nullptr;
struct curl_slist* headers = nullptr; struct curl_slist* headers = nullptr;
public: public:
request(); request();
void setAuthHeader(const std::string& header); void setAuthHeader(const std::string& header);
void get(const std::string& domain); void get(const std::string& domain);
void post(const std::string& domain); void post(const std::string& domain);
~easyrequest_get();
~request();
}; };
} }

@ -1 +1 @@
Subproject commit eba9ecb9e525c954e956ae7e25504654af9d8eaf Subproject commit a3f67571460d23fe819a3a3a572344e7030fba4a

View File

@ -21,7 +21,7 @@ namespace cs {
responses[site] = response; responses[site] = response;
} }
void init() void requests::init()
{ {
auto code = curl_global_init(CURL_GLOBAL_ALL); auto code = curl_global_init(CURL_GLOBAL_ALL);
if (code) if (code)
@ -31,23 +31,23 @@ namespace cs {
} }
} }
void cleanup() void requests::cleanup()
{ {
curl_global_cleanup(); curl_global_cleanup();
} }
easy_get::easy_get() request::request()
{ {
handler = curl_easy_init(); handler = curl_easy_init();
} }
easy_get::~easy_get() request::~request()
{ {
curl_slist_free_all(headers); curl_slist_free_all(headers);
curl_easy_cleanup(handler); curl_easy_cleanup(handler);
} }
void easy_get::setAuthHeader(const std::string& header) void request::setAuthHeader(const std::string& header)
{ {
curl_slist_free_all(headers); curl_slist_free_all(headers);
headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
@ -56,7 +56,7 @@ namespace cs {
curl_easy_setopt(handler, CURLOPT_HTTPHEADER, headers); 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_URL, domain.c_str());
curl_easy_setopt(handler, CURLOPT_WRITEDATA, 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)); BLT_ERROR("CURL failed to send request '%s'. Error '%s'", domain.c_str(), curl_easy_strerror(err));
} }
} }
void request::post(const std::string& domain)
{
}
} }

View File

@ -10,8 +10,39 @@
#include <crowsite/requests/curl.h> #include <crowsite/requests/curl.h>
#include <blt/parse/argparse.h> #include <blt/parse/argparse.h>
int main(int argc, const char** argv) { class BLT_CrowLogger : public crow::ILogHandler
blt::logging::setLogOutputFormat("\033[94m[${{FULL_TIME}}]${{RC}} ${{LF}}[${{LOG_LEVEL}}]${{RC}} \033[35m(${{FILE}}:${{LINE}})${{RC}} ${{CNR}}${{STR}}${{RC}}\n"); {
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(); cs::requests::init();
blt::arg_parse parser; blt::arg_parse parser;
@ -70,12 +101,14 @@ int main(int argc, const char** argv) {
// //
// return 0; // return 0;
cs::requests::easy_get get; cs::request get;
get.setAuthHeader("MediaBrowser Client=Crowsite, Device=YourMom, Token=" + blt::arg_parse::get<std::string>(args["token"])); get.setAuthHeader("MediaBrowser Client=Crowsite, Device=YourMom, Token=" + blt::arg_parse::get<std::string>(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); BLT_INFO("Starting site %s.", SITE_NAME);
crow::mustache::set_global_base(SITE_FILES_PATH); 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!"); BLT_INFO("Init Crow with compression and logging enabled!");
crow::SimpleApp app; crow::SimpleApp app;
@ -97,12 +130,14 @@ int main(int argc, const char** argv) {
BLT_INFO("Creating routes"); BLT_INFO("Creating routes");
CROW_ROUTE(app, "/favicon.ico")([](crow::response& local_fav_res) { CROW_ROUTE(app, "/favicon.ico")(
local_fav_res.compressed = false; [](crow::response& local_fav_res) {
local_fav_res.set_static_file_info_unsafe(cs::fs::createStaticFilePath("images/favicon.ico")); local_fav_res.compressed = false;
local_fav_res.set_header("content-type", "image/x-icon"); local_fav_res.set_static_file_info_unsafe(cs::fs::createStaticFilePath("images/favicon.ico"));
local_fav_res.end(); local_fav_res.set_header("content-type", "image/x-icon");
}); local_fav_res.end();
}
);
CROW_ROUTE(app, "/<string>")( CROW_ROUTE(app, "/<string>")(
[&](const std::string& name) -> crow::response { [&](const std::string& name) -> crow::response {
@ -128,9 +163,11 @@ int main(int argc, const char** argv) {
} }
); );
CROW_ROUTE(app, "/")([&engine]() { CROW_ROUTE(app, "/")(
return engine.fetch("home.html"); [&engine]() {
}); return engine.fetch("home.html");
}
);
CROW_CATCHALL_ROUTE(app)( CROW_CATCHALL_ROUTE(app)(
[&engine]() { [&engine]() {