onion-wownero-blockchain-ex.../src/page.h

103 lines
2.3 KiB
C
Raw Normal View History

2016-04-08 06:54:04 +00:00
//
// Created by mwo on 8/04/16.
//
#ifndef CROWXMR_PAGE_H
#define CROWXMR_PAGE_H
2016-04-08 09:32:12 +00:00
#include "mstch/mstch.hpp"
#include "../ext/format.h"
2016-04-08 06:54:04 +00:00
#include "monero_headers.h"
2016-04-08 09:32:12 +00:00
#include "MicroCore.h"
2016-04-08 06:54:04 +00:00
#include "tools.h"
2016-04-08 09:32:12 +00:00
#define TMPL_DIR "./templates"
#define TMPL_INDEX TMPL_DIR "/index.html"
#define TMPL_HEADER TMPL_DIR "/header.html"
#define TMPL_FOOTER TMPL_DIR "/footer.html"
2016-04-08 06:54:04 +00:00
#define READ_TMPL(tmpl_path) xmreg::read(tmpl_path)
namespace xmreg {
2016-04-08 09:32:12 +00:00
using namespace cryptonote;
using namespace crypto;
using namespace std;
2016-04-08 06:54:04 +00:00
class page {
2016-04-08 09:32:12 +00:00
MicroCore* mcore;
Blockchain* core_storage;
2016-04-08 06:54:04 +00:00
public:
2016-04-08 09:32:12 +00:00
page(MicroCore* _mcore, Blockchain* _core_storage)
: mcore {_mcore}, core_storage {_core_storage}
{
}
2016-04-08 06:54:04 +00:00
string
index()
{
2016-04-08 09:32:12 +00:00
// get the current blockchain height. Just to check if it reads ok.
uint64_t height = core_storage->get_current_blockchain_height() - 1;
mstch::map context {
{"height", fmt::format("{:d}", height)},
{"blocks", mstch::array()}
};
size_t no_of_last_blocks {50};
mstch::array& blocks = boost::get<mstch::array>(context["blocks"]);
for (size_t i = height; i > height - no_of_last_blocks; --i)
{
block blk;
mcore->get_block_by_height(i, blk);
crypto::hash blk_hash = core_storage->get_block_id_by_height(i);
blocks.push_back(mstch::map {
{"height" , to_string(i)},
{"timestamp" , xmreg::timestamp_to_str(blk.timestamp)},
{"hash" , fmt::format("{:s}", blk_hash)},
{"notx" , fmt::format("{:d}", blk.tx_hashes.size())}
});
}
2016-04-08 06:54:04 +00:00
std::string view = READ_TMPL(TMPL_INDEX);
2016-04-08 09:32:12 +00:00
string full_page = get_full_page(view);
return mstch::render(view, context);
2016-04-08 06:54:04 +00:00
}
private:
2016-04-08 09:32:12 +00:00
string
get_full_page(string& middle)
{
return READ_TMPL(TMPL_HEADER)
+ middle
+ READ_TMPL(TMPL_FOOTER);
}
2016-04-08 06:54:04 +00:00
};
}
#endif //CROWXMR_PAGE_H