From f5f9916b933359e04702c63d1b2b3630a9a00e9f Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Thu, 4 May 2017 08:43:00 +0800 Subject: [PATCH] myxmr::jsonresponse class added --- main.cpp | 58 ++++++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/main.cpp b/main.cpp index 4e1d2fc..eec2dea 100644 --- a/main.cpp +++ b/main.cpp @@ -13,6 +13,21 @@ using boost::filesystem::path; using namespace std; +namespace myxmr +{ +struct jsonresponse: crow::response +{ + jsonresponse(const nlohmann::json& _body) : crow::response {_body.dump()} + { + add_header("Access-Control-Allow-Origin", "*"); + add_header("Access-Control-Allow-Headers", "Content-Type"); + add_header("Content-Type", "application/json"); + } +}; +} + + + int main(int ac, const char* av[]) { // get command line options @@ -343,11 +358,7 @@ int main(int ac, const char* av[]) { CROW_ROUTE(app, "/api/transaction/") ([&](const crow::request &req, string tx_hash) { - crow::response r{xmrblocks.json_transaction(tx_hash).dump()}; - - r.add_header("Access-Control-Allow-Origin", "*"); - r.add_header("Access-Control-Allow-Headers", "Content-Type"); - r.add_header("Content-Type", "application/json"); + myxmr::jsonresponse r{xmrblocks.json_transaction(tx_hash)}; return r; }); @@ -355,11 +366,7 @@ int main(int ac, const char* av[]) { CROW_ROUTE(app, "/api/block/") ([&](const crow::request &req, string block_no_or_hash) { - crow::response r{xmrblocks.json_block(block_no_or_hash).dump()}; - - r.add_header("Access-Control-Allow-Origin", "*"); - r.add_header("Access-Control-Allow-Headers", "Content-Type"); - r.add_header("Content-Type", "application/json"); + myxmr::jsonresponse r{xmrblocks.json_block(block_no_or_hash)}; return r; }); @@ -374,11 +381,7 @@ int main(int ac, const char* av[]) { string limit = regex_search(req.raw_url, regex {"limit=\\d+"}) ? req.url_params.get("limit") : "25"; - crow::response r{xmrblocks.json_transactions(page, limit).dump()}; - - r.add_header("Access-Control-Allow-Origin", "*"); - r.add_header("Access-Control-Allow-Headers", "Content-Type"); - r.add_header("Content-Type", "application/json"); + myxmr::jsonresponse r{xmrblocks.json_transactions(page, limit)}; return r; }); @@ -386,11 +389,7 @@ int main(int ac, const char* av[]) { CROW_ROUTE(app, "/api/mempool") ([&](const crow::request &req) { - crow::response r{xmrblocks.json_mempool().dump()}; - - r.add_header("Access-Control-Allow-Origin", "*"); - r.add_header("Access-Control-Allow-Headers", "Content-Type"); - r.add_header("Content-Type", "application/json"); + myxmr::jsonresponse r{xmrblocks.json_mempool()}; return r; }); @@ -398,11 +397,7 @@ int main(int ac, const char* av[]) { CROW_ROUTE(app, "/api/search/") ([&](const crow::request &req, string search_value) { - crow::response r{xmrblocks.json_search(search_value).dump()}; - - r.add_header("Access-Control-Allow-Origin", "*"); - r.add_header("Access-Control-Allow-Headers", "Content-Type"); - r.add_header("Content-Type", "application/json"); + myxmr::jsonresponse r{xmrblocks.json_search(search_value)}; return r; }); @@ -421,21 +416,18 @@ int main(int ac, const char* av[]) { bool tx_prove{false}; - try { + try + { tx_prove = regex_search(req.raw_url, regex {"txprove=[01]"}) ? boost::lexical_cast(req.url_params.get("txprove")) : false; } - catch (const boost::bad_lexical_cast &e) { + catch (const boost::bad_lexical_cast &e) + { cerr << "Cant parse tx_prove as bool. Using default value" << endl; } - crow::response r{xmrblocks.json_outputs( - tx_hash, address, viewkey, tx_prove).dump()}; - - r.add_header("Access-Control-Allow-Origin", "*"); - r.add_header("Access-Control-Allow-Headers", "Content-Type"); - r.add_header("Content-Type", "application/json"); + myxmr::jsonresponse r{xmrblocks.json_outputs(tx_hash, address, viewkey, tx_prove)}; return r; });