From 677e46e341b4dac4f831a90958e620130785f293 Mon Sep 17 00:00:00 2001 From: nekobit Date: Wed, 4 Jan 2023 03:35:48 +0000 Subject: [PATCH] Add some debug information FossilOrigin-Name: 9f05b05915fb5c06d571c4d9f03574f7a983399481405665d73436e025c38f9e --- CMakeLists.txt | 2 +- src/http/microhttpd_server.cpp | 6 +++++ src/main.cpp | 34 ++++++++++++++++++++++----- src/protocol/masto-api/CMakeLists.txt | 4 +++- src/protocol/masto-api/instance.cpp | 8 ++++--- src/protocol/masto-api/instance.h | 2 +- src/protocol/masto-api/mastoapi.cpp | 9 +++++-- 7 files changed, 51 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 09488c0..64426d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,7 +78,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug") else() # Some asserts are in the code... add_definitions(-DNDEBUG) - add_compile_options(-Wall -Wno-used-function -Wno-reorder -O2) + add_compile_options(-Wall -Wno-unused-function -Wno-reorder -O2) endif() set(WORMHOLE_INCLUDE_DIRS diff --git a/src/http/microhttpd_server.cpp b/src/http/microhttpd_server.cpp index 66ba5cd..507887b 100644 --- a/src/http/microhttpd_server.cpp +++ b/src/http/microhttpd_server.cpp @@ -149,6 +149,12 @@ namespace MHD_RESPMEM_MUST_COPY); MHD_add_response_header(response, "Content-Type", MIME::mime_to_str(resp->mimetype).data()); + + // Useful for testing with frontends +#ifndef NDEBUG + MHD_add_response_header(response, "Access-Control-Allow-Origin", "*"); +#endif + // HTTP::Code works for MHD already since it's just int's that represent the HTTP response codes ret = MHD_queue_response(conn, resp->code, response); diff --git a/src/main.cpp b/src/main.cpp index d89f852..ff89b2c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,7 +65,8 @@ void init_routes(const std::unique_ptr& server) #endif // MODULE_FCGI } -int start_wormhole() +int +start_wormhole() { using namespace std::string_literals; @@ -147,10 +148,9 @@ int start_wormhole() return EXIT_SUCCESS; } -int main(int argc, char** argv) +inline void +load_configs() { - int res = EXIT_SUCCESS; - // Initialize config // Try XDG config, then HOME std::filesystem::path xdgpath; @@ -177,9 +177,31 @@ int main(int argc, char** argv) if (!found) { std::cerr << " !! Couldn't find a config file for Wormhole, " - "this is required to function." << std::endl; - return EXIT_FAILURE; + "this is required to function. Possible paths include: \n"; + for (auto path: pths) + { + std::cerr << " - " << path.generic_string() << "\n"; + } + std::cerr << std::endl; + exit(EXIT_FAILURE); } +} + + +int +main(int argc, char** argv) +{ + int res = EXIT_SUCCESS; + + load_configs(); + + // Notify user if in debug mode +#ifndef NDEBUG + std::cout << " !! Notice: You compiled with " RED " DEBUG MODE " RESET " which " + "means that some debug features are enabled, like full CORS request access." + << std::endl; + +#endif // Start wormhole as is, no arguments needed if (argc <= 1) diff --git a/src/protocol/masto-api/CMakeLists.txt b/src/protocol/masto-api/CMakeLists.txt index 7211cb6..1191b9f 100644 --- a/src/protocol/masto-api/CMakeLists.txt +++ b/src/protocol/masto-api/CMakeLists.txt @@ -1,4 +1,6 @@ add_library(wormhole_masto_api_module - mastoapi.cpp apps.cpp) + mastoapi.cpp + instance.cpp + apps.cpp) include_directories(wormhole_masto_api_module ${WORMHOLE_INCLUDE_DIRS}) target_link_libraries(wormhole_masto_api_module wormhole_lib) diff --git a/src/protocol/masto-api/instance.cpp b/src/protocol/masto-api/instance.cpp index 1a09b8b..286464f 100644 --- a/src/protocol/masto-api/instance.cpp +++ b/src/protocol/masto-api/instance.cpp @@ -6,6 +6,8 @@ #include "instance.h" #include +#include "http/response.h" +#include "jsonhelper.h" #include "route_args.h" #include "http/mime.h" @@ -20,19 +22,19 @@ Route::MastoAPI::get_instance(std::any& args, rjson::Document root(rjson::kObjectType); rjson::Document::AllocatorType& a = root.GetAllocator(); - root.AddMember("website", rjson::Value()) + root.AddMember("name", "test", a); return HTTP::Response{ rjson::to_string(root), HTTP::MIME::JSON }; } void -Protocol::MastoAPI::init_instance_info(HTTP::Server* server) +Protocol::MastoAPI::init_masto_instance(HTTP::Server* server) { server->add_route({ HTTP::Request{ HTTP::Request::Type::GET, "/api/v1/instance", - HTTP::MIME::Mimetypes + HTTP::MIME::Mimetypes::JSON, }, Route::MastoAPI::get_instance }); diff --git a/src/protocol/masto-api/instance.h b/src/protocol/masto-api/instance.h index 173984c..59d8987 100644 --- a/src/protocol/masto-api/instance.h +++ b/src/protocol/masto-api/instance.h @@ -24,7 +24,7 @@ namespace Protocol namespace MastoAPI { void - init_instance_info(HTTP::Server* server); + init_masto_instance(HTTP::Server* server); } } #endif // INSTANCE_H diff --git a/src/protocol/masto-api/mastoapi.cpp b/src/protocol/masto-api/mastoapi.cpp index 6e12714..5a7dcd0 100644 --- a/src/protocol/masto-api/mastoapi.cpp +++ b/src/protocol/masto-api/mastoapi.cpp @@ -14,12 +14,15 @@ #include "http/httpserver.h" #include "http/error_response.h" #include "http/request.h" -#include "apps.h" #include "types/user.h" #include "logger.h" #include "jsonhelper.h" #include "route_args.h" +// Routes to be loaded here +#include "apps.h" +#include "instance.h" + using namespace Protocol; using namespace std::string_literals; @@ -33,9 +36,11 @@ Route::MastoAPI::test(std::any& args, return HTTP::Response{ "{\"test\":\"succeeded1\"}", HTTP::MIME::JSON }; } -void MastoAPI::init_masto_api(HTTP::Server* server) +void +MastoAPI::init_masto_api(HTTP::Server* server) { server->add_route({{HTTP::Request::Type::GET, "/api/v1/wormhole_test"}, Route::MastoAPI::test}); init_masto_apps(server); + init_masto_instance(server); }