more structs, funcs, mounts, routes

master
lalanza808 5 years ago
parent 7addf838ae
commit 43f8de2de7

@ -2,7 +2,8 @@
pub struct RPCPayload { pub struct RPCPayload {
pub jsonrpc: String, pub jsonrpc: String,
pub id: String, pub id: String,
pub method: String pub method: String,
pub params: Option<RPCParams>
} }
impl Default for RPCPayload { impl Default for RPCPayload {
@ -10,7 +11,23 @@ impl Default for RPCPayload {
RPCPayload { RPCPayload {
jsonrpc: "2.0".to_string(), jsonrpc: "2.0".to_string(),
id: "0".to_string(), id: "0".to_string(),
method: "get_info".to_string() method: "get_info".to_string(),
params: None,
}
}
}
#[derive(Serialize, Deserialize)]
pub struct RPCParams {
pub hash: Option<String>,
pub txs_hashes: Option<Vec<String>>
}
impl Default for RPCParams {
fn default() -> RPCParams {
RPCParams {
hash: None,
txs_hashes: None
} }
} }
} }
@ -95,3 +112,24 @@ pub struct BlockHeader {
pub reward: u64, pub reward: u64,
pub timestamp: u64 pub timestamp: u64
} }
#[derive(Serialize, Deserialize)]
pub struct GetTransactions {
pub credits: u32,
pub status: String,
pub top_hash: String,
pub txs: Vec<GetTransactionsTxs>
}
#[derive(Serialize, Deserialize)]
pub struct GetTransactionsTxs {
pub block_height: u32,
pub block_timestamp: u64,
pub double_spend_seen: bool,
pub in_pool: bool,
pub output_indices: Vec<u32>,
pub prunable_as_hex: String,
pub prunable_hash: String,
pub pruned_as_hex: String,
pub tx_hash: String
}

@ -5,52 +5,83 @@
extern crate reqwest; extern crate reqwest;
mod data_types; mod data_types;
use rocket_contrib::json::{Json, JsonValue}; use rocket_contrib::json::{Json, JsonValue};
use reqwest::blocking::RequestBuilder;
use std::env; use std::env;
use data_types::*; use data_types::*;
fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder {
fn issue_rpc(method: &str) -> reqwest::blocking::RequestBuilder {
let uri = env::var("DAEMON_URI").unwrap();
let http_client = reqwest::blocking::Client::new(); let http_client = reqwest::blocking::Client::new();
let url = format!(
"{}/json_rpc",
env::var("DAEMON_URI").unwrap()
);
let post_data = RPCPayload { let post_data = RPCPayload {
method: method.to_string(), method: method.to_string(),
params: params,
..Default::default() ..Default::default()
}; };
return http_client.post(&uri).json(&post_data); http_client.post(&url).json(&post_data)
} }
#[catch(404)] fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder {
fn not_found() -> JsonValue { let http_client = reqwest::blocking::Client::new();
json!({ let url = format!(
"status": "error", "{}/{}",
"reason": "Resource was not found." env::var("DAEMON_URI").unwrap(),
}) &method
);
http_client.post(&url).json(&params)
} }
#[get("/transaction/<hash>")] // /block
fn get_transaction(hash: String) -> Json<BlockHeader> {
let res: BlockByHeaderHash = issue_rpc(&"get_block_header_by_hash") #[get("/hash/<block_hash>")]
.send() fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
.unwrap().json().unwrap(); let params = RPCParams {
hash: Some(block_hash),
..Default::default()
};
let res: BlockByHeaderHash = issue_rpc(&"get_block_header_by_hash", Some(params))
.send().unwrap().json().unwrap();
Json(res.result.block_header) Json(res.result.block_header)
} }
#[get("/tx/<tx_hash>")]
fn get_block_header_by_transaction_hash(tx_hash: String) -> Json<GetTransactions> {
let params: JsonValue = json!({"txs_hashes": [&tx_hash]});
let res: GetTransactions = issue_raw_rpc(&"get_transactions", params)
.send().unwrap().json().unwrap();
Json(res)
}
// /
#[get("/info")] #[get("/info")]
fn get_daemon_info() -> Json<GetInfoResult> { fn get_daemon_info() -> Json<GetInfoResult> {
let res: GetInfo = issue_rpc(&"get_info") let res: GetInfo = issue_rpc(&"get_info", None)
.send() .send().unwrap().json().unwrap();
.unwrap().json().unwrap();
Json(res.result) Json(res.result)
} }
#[catch(404)]
fn not_found() -> JsonValue {
json!({
"status": "error",
"reason": "Resource was not found."
})
}
fn main() { fn main() {
let env_url = env::var("DAEMON_URI"); let env_url = env::var("DAEMON_URI");
match env_url { match env_url {
Ok(_) => { Ok(_) => {
rocket::ignite() rocket::ignite()
.mount("/", routes![ .mount("/", routes![
get_daemon_info, get_transaction get_daemon_info
])
.mount("/block", routes![
get_block_header_by_block_hash,
get_block_header_by_transaction_hash
]) ])
.register(catchers![not_found]) .register(catchers![not_found])
.launch(); .launch();

Loading…
Cancel
Save