diff options
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 81 |
1 files changed, 43 insertions, 38 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 09cd2a5..e45ebcd 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -335,11 +335,7 @@ pub async fn authenticate_user( | |||
335 | }) | 335 | }) |
336 | .unwrap(); | 336 | .unwrap(); |
337 | 337 | ||
338 | fs::write( | 338 | write_guy_file(&db.config.name, &new_user.user_id, &user_at_rest_json); |
339 | format!("users/{}/{}.guy", db.config.name, new_user.user_id), | ||
340 | user_at_rest_json, | ||
341 | ) | ||
342 | .unwrap(); | ||
343 | 339 | ||
344 | let mut userlist = db.users.write(); | 340 | let mut userlist = db.users.write(); |
345 | userlist.insert(fingerprint.clone(), new_user); | 341 | userlist.insert(fingerprint.clone(), new_user); |
@@ -621,34 +617,30 @@ pub async fn propose_block( | |||
621 | } | 617 | } |
622 | 618 | ||
623 | // just update everyone's .guy file | 619 | // just update everyone's .guy file |
624 | for (fp, guy) in users_store.iter() { | 620 | for (fp, user) in users_store.iter() { |
625 | if !guy.is_bot { | 621 | if !user.is_bot { |
626 | let user_at_rest_json = serde_json::to_string(&UserAtRest { | 622 | let user_at_rest_json = serde_json::to_string(&UserAtRest { |
627 | fingerprint: fp.clone(), | 623 | fingerprint: fp.clone(), |
628 | user: User { | 624 | user: User { |
629 | user_id: guy.user_id.clone(), | 625 | user_id: user.user_id.clone(), |
630 | public_key: guy.public_key.clone(), | 626 | public_key: user.public_key.clone(), |
631 | balance: guy.balance, | 627 | balance: user.balance, |
632 | is_bot: false, | 628 | is_bot: false, |
633 | }, | 629 | }, |
634 | }) | 630 | }) |
635 | .unwrap(); | 631 | .unwrap(); |
636 | fs::write(format!("users/{}.guy", guy.user_id), user_at_rest_json).unwrap(); | 632 | write_guy_file(&db.config.name, &user.user_id, &user_at_rest_json); |
637 | } | 633 | } |
638 | } | 634 | } |
639 | } | 635 | } |
640 | 636 | ||
641 | let block_json = serde_json::to_string(&new_block).unwrap(); | 637 | let block_json = serde_json::to_string(&new_block).unwrap(); |
642 | 638 | ||
643 | fs::write( | 639 | write_block( |
644 | format!( | 640 | &db.config.name, |
645 | "blocks/{}/{}.block", | 641 | new_block.timestamp.timestamp(), |
646 | db.config.name, | 642 | &block_json, |
647 | new_block.timestamp.timestamp() | 643 | ); |
648 | ), | ||
649 | block_json, | ||
650 | ) | ||
651 | .unwrap(); | ||
652 | 644 | ||
653 | { | 645 | { |
654 | let mut blockchain = db.blockchain.write(); | 646 | let mut blockchain = db.blockchain.write(); |
@@ -928,13 +920,31 @@ pub async fn propose_transaction( | |||
928 | /// GET /block | 920 | /// GET /block |
929 | /// Returns the last block's JSON | 921 | /// Returns the last block's JSON |
930 | /// Cannot fail | 922 | /// Cannot fail |
931 | /// Mostly around for debug purposes | ||
932 | pub async fn list_blocks(db: Db) -> Result<impl warp::Reply, Infallible> { | 923 | pub async fn list_blocks(db: Db) -> Result<impl warp::Reply, Infallible> { |
933 | let block = db.blockchain.read(); | 924 | let block = db.blockchain.read(); |
934 | 925 | ||
935 | Ok(reply::with_status(reply::json(&*block), StatusCode::OK)) | 926 | Ok(reply::with_status(reply::json(&*block), StatusCode::OK)) |
936 | } | 927 | } |
937 | 928 | ||
929 | /// GET /user | ||
930 | /// Returns an HTML file with the current standing of users | ||
931 | pub async fn user_list_handler(db: Db) -> Result<impl warp::Reply, warp::Rejection> { | ||
932 | let users = db.users.read(); | ||
933 | let mut sane_users = Vec::new(); | ||
934 | |||
935 | for (fingerprint, user) in users.iter() { | ||
936 | sane_users.push(DisplayUsers { | ||
937 | fingerprint: fingerprint.clone(), | ||
938 | balance: user.balance, | ||
939 | is_bot: user.is_bot, | ||
940 | }); | ||
941 | } | ||
942 | |||
943 | let template = UserTemplate { users: &sane_users }; | ||
944 | let res = template.render().unwrap(); | ||
945 | Ok(warp::reply::html(res)) | ||
946 | } | ||
947 | |||
938 | /// Handles the JWT Authorization | 948 | /// Handles the JWT Authorization |
939 | /// | 949 | /// |
940 | /// *[`jwt_token`]: The raw JWT token, "Bearer aaa.bbb.ccc" | 950 | /// *[`jwt_token`]: The raw JWT token, "Bearer aaa.bbb.ccc" |
@@ -986,6 +996,18 @@ fn authorize_proposer(jwt_token: &str, user_pem: &str) -> Result<TokenData<Claim | |||
986 | Ok(token_payload) | 996 | Ok(token_payload) |
987 | } | 997 | } |
988 | 998 | ||
999 | fn write_guy_file(network_name: &str, guy: &MetuId, content: &str) { | ||
1000 | fs::write(format!("users/{}/{}.guy", network_name, guy), content).unwrap(); | ||
1001 | } | ||
1002 | |||
1003 | fn write_block(network_name: &str, timestamp: i64, block: &str) { | ||
1004 | fs::write( | ||
1005 | format!("blocks/{}/{}.block", network_name, timestamp), | ||
1006 | block, | ||
1007 | ) | ||
1008 | .unwrap(); | ||
1009 | } | ||
1010 | |||
989 | fn calculate_transaction_id(source: &str, target: &str) -> String { | 1011 | fn calculate_transaction_id(source: &str, target: &str) -> String { |
990 | let long_fingerprint = format!("{}{}{}", source, target, Utc::now()); | 1012 | let long_fingerprint = format!("{}{}{}", source, target, Utc::now()); |
991 | let id = format!("{:x}", Sha256::digest(long_fingerprint.as_bytes())); | 1013 | let id = format!("{:x}", Sha256::digest(long_fingerprint.as_bytes())); |
@@ -1004,23 +1026,6 @@ struct DisplayUsers { | |||
1004 | is_bot: bool, | 1026 | is_bot: bool, |
1005 | } | 1027 | } |
1006 | 1028 | ||
1007 | pub async fn user_list_handler(db: Db) -> Result<impl warp::Reply, warp::Rejection> { | ||
1008 | let users = db.users.read(); | ||
1009 | let mut sane_users = Vec::new(); | ||
1010 | |||
1011 | for (fingerprint, user) in users.iter() { | ||
1012 | sane_users.push(DisplayUsers { | ||
1013 | fingerprint: fingerprint.clone(), | ||
1014 | balance: user.balance, | ||
1015 | is_bot: user.is_bot, | ||
1016 | }); | ||
1017 | } | ||
1018 | |||
1019 | let template = UserTemplate { users: &sane_users }; | ||
1020 | let res = template.render().unwrap(); | ||
1021 | Ok(warp::reply::html(res)) | ||
1022 | } | ||
1023 | |||
1024 | fn has_unique_elements<T>(iter: T) -> bool | 1029 | fn has_unique_elements<T>(iter: T) -> bool |
1025 | where | 1030 | where |
1026 | T: IntoIterator, | 1031 | T: IntoIterator, |