From 73ed903effa596cf3aaac180e614c81be8bd2b1c Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Wed, 13 Mar 2019 08:55:05 +0800 Subject: [PATCH] add htmlresponse to other endpoints --- main.cpp | 123 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 45 deletions(-) diff --git a/main.cpp b/main.cpp index da87608..93fca21 100644 --- a/main.cpp +++ b/main.cpp @@ -17,7 +17,7 @@ using namespace std; namespace myxmr { -struct htmlresponse: crow::response +struct htmlresponse: public crow::response { htmlresponse(string&& _body) : crow::response {std::move(_body)} @@ -26,7 +26,7 @@ struct htmlresponse: crow::response } }; -struct jsonresponse: crow::response +struct jsonresponse: public crow::response { jsonresponse(const nlohmann::json& _body) : crow::response {_body.dump()} @@ -191,12 +191,13 @@ main(int ac, const char* av[]) try { - mempool_info_timeout = boost::lexical_cast(*mempool_info_timeout_opt); + mempool_info_timeout = boost::lexical_cast( + *mempool_info_timeout_opt); } catch (boost::bad_lexical_cast &e) { - cout << "Cant cast " << (*mempool_info_timeout_opt) <<" into numbers. Using default values." - << endl; + cout << "Cant cast " << (*mempool_info_timeout_opt) + <<" into numbers. Using default values.\n"; } uint64_t mempool_refresh_time {10}; @@ -300,60 +301,68 @@ main(int ac, const char* av[]) CROW_ROUTE(app, "/") ([&]() { - //return crow::response(xmrblocks.index2()); return myxmr::htmlresponse(xmrblocks.index2()); }); CROW_ROUTE(app, "/page/") ([&](size_t page_no) { - return xmrblocks.index2(page_no); + return myxmr::htmlresponse(xmrblocks.index2(page_no)); }); CROW_ROUTE(app, "/block/") ([&](size_t block_height) { - return crow::response(xmrblocks.show_block(block_height)); + return myxmr::htmlresponse(xmrblocks.show_block(block_height)); }); CROW_ROUTE(app, "/block/") ([&](string block_hash) { - return crow::response(xmrblocks.show_block(remove_bad_chars(block_hash))); + return myxmr::htmlresponse( + xmrblocks.show_block(remove_bad_chars(block_hash))); }); CROW_ROUTE(app, "/tx/") ([&](string tx_hash) { - return crow::response(xmrblocks.show_tx(remove_bad_chars(tx_hash))); + return myxmr::htmlresponse( + xmrblocks.show_tx(remove_bad_chars(tx_hash))); }); if (enable_as_hex) { CROW_ROUTE(app, "/txhex/") ([&](string tx_hash) { - return crow::response(xmrblocks.show_tx_hex(remove_bad_chars(tx_hash))); + return crow::response( + xmrblocks.show_tx_hex(remove_bad_chars(tx_hash))); }); CROW_ROUTE(app, "/ringmembershex/") ([&](string tx_hash) { - return crow::response(xmrblocks.show_ringmembers_hex(remove_bad_chars(tx_hash))); + return crow::response( + xmrblocks.show_ringmembers_hex(remove_bad_chars(tx_hash))); }); CROW_ROUTE(app, "/blockhex/") ([&](size_t block_height) { - return crow::response(xmrblocks.show_block_hex(block_height, false)); + return crow::response( + xmrblocks.show_block_hex(block_height, false)); }); CROW_ROUTE(app, "/blockhexcomplete/") ([&](size_t block_height) { - return crow::response(xmrblocks.show_block_hex(block_height, true)); + return crow::response( + xmrblocks.show_block_hex(block_height, true)); }); // CROW_ROUTE(app, "/ringmemberstxhex/") // ([&](string tx_hash) { -// return crow::response(xmrblocks.show_ringmemberstx_hex(remove_bad_chars(tx_hash))); +// return crow::response( +// xmrblocks.show_ringmemberstx_hex(remove_bad_chars(tx_hash))); // }); CROW_ROUTE(app, "/ringmemberstxhex/") ([&](string tx_hash) { - return myxmr::jsonresponse {xmrblocks.show_ringmemberstx_jsonhex(remove_bad_chars(tx_hash))}; + return myxmr::jsonresponse { + xmrblocks.show_ringmemberstx_jsonhex( + remove_bad_chars(tx_hash))}; }); } @@ -361,11 +370,13 @@ main(int ac, const char* av[]) CROW_ROUTE(app, "/tx//") ([&](string tx_hash, uint16_t with_ring_signatures) { - return xmrblocks.show_tx(remove_bad_chars(tx_hash), with_ring_signatures); + return myxmr::htmlresponse( + xmrblocks.show_tx(remove_bad_chars(tx_hash), + with_ring_signatures)); }); CROW_ROUTE(app, "/myoutputs").methods("POST"_method) - ([&](const crow::request& req) + ([&](const crow::request& req) -> myxmr::htmlresponse { map post_body @@ -388,9 +399,12 @@ main(int ac, const char* av[]) string domain = get_domain(req); - return xmrblocks.show_my_outputs(tx_hash, xmr_address, + string response = xmrblocks.show_my_outputs( + tx_hash, xmr_address, viewkey, raw_tx_data, domain); + + return myxmr::htmlresponse(std::move(response)); }); CROW_ROUTE(app, "/myoutputs///") @@ -400,15 +414,17 @@ main(int ac, const char* av[]) string domain = get_domain(req); - return xmrblocks.show_my_outputs(remove_bad_chars(tx_hash), + return myxmr::htmlresponse(xmrblocks.show_my_outputs( + remove_bad_chars(tx_hash), remove_bad_chars(xmr_address), remove_bad_chars(viewkey), string {}, - domain); + domain)); }); CROW_ROUTE(app, "/prove").methods("POST"_method) - ([&](const crow::request& req) { + ([&](const crow::request& req) -> myxmr::htmlresponse + { map post_body = xmreg::parse_crow_post_data(req.body); @@ -431,41 +447,45 @@ main(int ac, const char* av[]) string domain = get_domain(req); - return xmrblocks.show_prove(tx_hash, + return myxmr::htmlresponse(xmrblocks.show_prove(tx_hash, xmr_address, tx_prv_key, raw_tx_data, - domain); + domain)); }); CROW_ROUTE(app, "/prove///") ([&](const crow::request& req, string tx_hash, - string xmr_address, string tx_prv_key) { + string xmr_address, string tx_prv_key) + { string domain = get_domain(req); - return xmrblocks.show_prove(remove_bad_chars(tx_hash), + return myxmr::htmlresponse(xmrblocks.show_prove( + remove_bad_chars(tx_hash), remove_bad_chars(xmr_address), remove_bad_chars(tx_prv_key), string {}, - domain); + domain)); }); if (enable_pusher) { CROW_ROUTE(app, "/rawtx") ([&]() { - return xmrblocks.show_rawtx(); + return myxmr::htmlresponse(xmrblocks.show_rawtx()); }); CROW_ROUTE(app, "/checkandpush").methods("POST"_method) - ([&](const crow::request& req) { + ([&](const crow::request& req) -> myxmr::htmlresponse + { map post_body = xmreg::parse_crow_post_data(req.body); - if (post_body.count("rawtxdata") == 0 || post_body.count("action") == 0) + if (post_body.count("rawtxdata") == 0 + || post_body.count("action") == 0) { return string("Raw tx data or action not provided"); } @@ -474,9 +494,11 @@ main(int ac, const char* av[]) string action = remove_bad_chars(post_body["action"]); if (action == "check") - return xmrblocks.show_checkrawtx(raw_tx_data, action); + return myxmr::htmlresponse( + xmrblocks.show_checkrawtx(raw_tx_data, action)); else if (action == "push") - return xmrblocks.show_pushrawtx(raw_tx_data, action); + return myxmr::htmlresponse( + xmrblocks.show_pushrawtx(raw_tx_data, action)); return string("Provided action is neither check nor push"); }); @@ -486,11 +508,12 @@ main(int ac, const char* av[]) { CROW_ROUTE(app, "/rawkeyimgs") ([&]() { - return xmrblocks.show_rawkeyimgs(); + return myxmr::htmlresponse(xmrblocks.show_rawkeyimgs()); }); CROW_ROUTE(app, "/checkrawkeyimgs").methods("POST"_method) - ([&](const crow::request& req) { + ([&](const crow::request& req) -> myxmr::htmlresponse + { map post_body = xmreg::parse_crow_post_data(req.body); @@ -508,7 +531,8 @@ main(int ac, const char* av[]) string raw_data = remove_bad_chars(post_body["rawkeyimgsdata"]); string viewkey = remove_bad_chars(post_body["viewkey"]); - return xmrblocks.show_checkrawkeyimgs(raw_data, viewkey); + return myxmr::htmlresponse( + xmrblocks.show_checkrawkeyimgs(raw_data, viewkey)); }); } @@ -517,11 +541,12 @@ main(int ac, const char* av[]) { CROW_ROUTE(app, "/rawoutputkeys") ([&]() { - return xmrblocks.show_rawoutputkeys(); + return myxmr::htmlresponse(xmrblocks.show_rawoutputkeys()); }); CROW_ROUTE(app, "/checkrawoutputkeys").methods("POST"_method) - ([&](const crow::request& req) { + ([&](const crow::request& req) -> myxmr::htmlresponse + { map post_body = xmreg::parse_crow_post_data(req.body); @@ -540,25 +565,29 @@ main(int ac, const char* av[]) string raw_data = remove_bad_chars(post_body["rawoutputkeysdata"]); string viewkey = remove_bad_chars(post_body["viewkey"]); - return xmrblocks.show_checkcheckrawoutput(raw_data, viewkey); + return myxmr::htmlresponse( + xmrblocks.show_checkcheckrawoutput(raw_data, viewkey)); }); } CROW_ROUTE(app, "/search").methods("GET"_method) ([&](const crow::request& req) { - return xmrblocks.search(remove_bad_chars(string(req.url_params.get("value")))); + return myxmr::htmlresponse( + xmrblocks.search( + remove_bad_chars( + string(req.url_params.get("value"))))); }); CROW_ROUTE(app, "/mempool") ([&]() { - return xmrblocks.mempool(true); + return myxmr::htmlresponse(xmrblocks.mempool(true)); }); // alias to "/mempool" CROW_ROUTE(app, "/txpool") ([&]() { - return xmrblocks.mempool(true); + return myxmr::htmlresponse(xmrblocks.mempool(true)); }); // CROW_ROUTE(app, "/altblocks") @@ -783,13 +812,16 @@ main(int ac, const char* av[]) try { - in_mempool_aswell = regex_search(req.raw_url, regex {"mempool=[01]"}) ? - boost::lexical_cast(req.url_params.get("mempool")) : + in_mempool_aswell = regex_search( + req.raw_url, regex {"mempool=[01]"}) ? + boost::lexical_cast( + req.url_params.get("mempool")) : false; } catch (const boost::bad_lexical_cast &e) { - cerr << "Cant parse tx_prove as bool. Using default value" << endl; + cerr << "Cant parse tx_prove as bool. Using default value" + << endl; } myxmr::jsonresponse r{xmrblocks.json_outputsblocks( @@ -826,7 +858,8 @@ main(int ac, const char* av[]) if (use_ssl) { cout << "Staring in ssl mode" << endl; - app.bindaddr(bindaddr).port(app_port).ssl_file(ssl_crt_file, ssl_key_file) + app.bindaddr(bindaddr).port(app_port).ssl_file( + ssl_crt_file, ssl_key_file) .multithreaded().run(); } else