Added blocktime generation

Also refactored the "base" template stuff - really should've done that in
a separate commit
This commit is contained in:
2024-07-15 20:46:36 +01:00
parent eb08b348b1
commit 846a9b94c7
5 changed files with 79 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
#[macro_use] extern crate rocket;
use rocket_dyn_templates::{context, Template};
use blockchain_time::{BlockchainInfo, BlockchainSupported, update_all_blockheights};
use blockchain_time::{BlockchainInfo, BlockchainSupported, update_all_blockheights, time_to_blockheight_specific_chain};
use chrono::prelude::*;
#[get("/")]
fn index() -> Template {
@@ -22,9 +23,34 @@ fn update_blockchains() -> Template {
Template::render("update_complete", context!{})
}
#[get("/to_blockchain_time?<input_datetime>")]
fn to_blockchain_time(input_datetime: &str) -> Template {
let dt = NaiveDateTime::parse_from_str(input_datetime, "%FT%R");
let dt = match dt {
Ok(dt) => {
dt.and_utc()
},
Err(e) => {
println!("error: {}", e);
panic!();
}
};
let blocktimes = vec!{
("BSV".to_string(), time_to_blockheight_specific_chain(dt, BlockchainSupported::BSV)),
("BTC".to_string(), time_to_blockheight_specific_chain(dt, BlockchainSupported::BTC)),
("XMR".to_string(), time_to_blockheight_specific_chain(dt, BlockchainSupported::XMR)),
("ETH".to_string(), time_to_blockheight_specific_chain(dt, BlockchainSupported::ETH)),
};
let context = context!{
blocktimes
};
println!("{:?}", context);
Template::render("blockchain_time_results", context)
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, update_blockchains])
// .mount("/update_blockchains", routes![update_blockchains])
rocket::build().mount("/", routes![index, update_blockchains, to_blockchain_time])
.attach(Template::fairing())
}