From 7f91412ddf3895214a8b56ac520ea7a60c0d24a1 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 15 Apr 2021 00:32:28 +0300 Subject: [WIP] Add template support --- src/handlers.rs | 19 ++++++++++++++++++- src/routes.rs | 6 ++++-- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/handlers.rs b/src/handlers.rs index a8c9947..b3a6fa8 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,5 +1,6 @@ -/// API handlers, the ends of each filter chain use aes::Aes128; +/// API handlers, the ends of each filter chain +use askama::Template; use base64; use blake2::{Blake2s, Digest}; use block_modes::block_padding::Pkcs7; @@ -599,3 +600,19 @@ fn authorize_proposer(jwt_token: String, user_pem: &String) -> Result { + title: &'a str, + body: &'a str, +} + +pub async fn welcome_handler() -> Result { + let template = WelcomeTemplate { + title: "Welcome", + body: "To The Bookstore!", + }; + let res = template.render().unwrap(); + Ok(warp::reply::html(res)) +} diff --git a/src/routes.rs b/src/routes.rs index 280de35..f8d9605 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -7,7 +7,10 @@ use crate::schema::Db; /// Every route combined pub fn consensus_routes(db: Db) -> impl Filter + Clone { - transaction_list(db.clone()) + let welcome_route = warp::path::end().and_then(handlers::welcome_handler); + + welcome_route + .or(transaction_list(db.clone())) .or(register_user(db.clone())) .or(auth_transaction_propose(db.clone())) .or(auth_block_propose(db.clone())) @@ -60,4 +63,3 @@ pub fn auth_block_propose(db: Db) -> impl Filter impl Filter + Clone { - let welcome_route = warp::path::end().and_then(handlers::welcome_handler); + // Remember when we wanted to implement templating + // Why would we? Just put a staic webpage under /public (next to Cargo.toml) and place it and + // the end of the filter chain - welcome_route - .or(transaction_list(db.clone())) + // Fully fledged website support, phew! + let static_route = warp::any().and(warp::fs::dir("public")); + + transaction_list(db.clone()) .or(register_user(db.clone())) .or(auth_transaction_propose(db.clone())) .or(auth_block_propose(db.clone())) .or(block_list(db.clone())) + .or(static_route) } /// POST /register warp route -- cgit v1.2.3-70-g09d2 From ca9146db3670cac5a82a3b89b998eee12ff27b44 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 15 Apr 2021 03:40:20 +0300 Subject: shed unused function --- src/handlers.rs | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'src') diff --git a/src/handlers.rs b/src/handlers.rs index b3a6fa8..7135190 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -194,17 +194,6 @@ pub async fn authenticate_user( Ok(warp::reply::with_status(res_json, StatusCode::CREATED)) } -// fn shed_pem_header_footer(maybe_key: String) -> Result, String> { -// let der_encoded = maybe_key -// .lines() -// .filter(|line| !line.starts_with("-")) -// .fold(String::new(), |mut data, line| { -// data.push_str(&line); -// data -// }); -// Ok(base64::decode(&der_encoded).expect("failed to decode base64 content")) -// } - /// GET /transaction /// Returns JSON array of transactions /// Cannot fail -- cgit v1.2.3-70-g09d2 From 2b866db08bd20985a570c4f0f292aaecfe1ea052 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 15 Apr 2021 00:32:28 +0300 Subject: Add template support We are now serving static HTML alongside Gradecoin REST --- Cargo.lock | 136 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/handlers.rs | 19 ++++++- src/routes.rs | 6 ++- templates/css.html | 8 +++ templates/footer.html | 2 + templates/header.html | 11 ++++ templates/menu.html | 5 ++ templates/welcome.html | 8 +++ 9 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 templates/css.html create mode 100644 templates/footer.html create mode 100644 templates/header.html create mode 100644 templates/menu.html create mode 100644 templates/welcome.html (limited to 'src') diff --git a/Cargo.lock b/Cargo.lock index b548774..649c923 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,58 @@ dependencies = [ "memchr", ] +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + +[[package]] +name = "askama" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d298738b6e47e1034e560e5afe63aa488fea34e25ec11b855a76f0d7b8e73134" +dependencies = [ + "askama_derive", + "askama_escape", + "askama_shared", +] + +[[package]] +name = "askama_derive" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2925c4c290382f9d2fa3d1c1b6a63fa1427099721ecca4749b154cc9c25522" +dependencies = [ + "askama_shared", + "proc-macro2", + "syn", +] + +[[package]] +name = "askama_escape" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90c108c1a94380c89d2215d0ac54ce09796823cca0fd91b299cfff3b33e346fb" + +[[package]] +name = "askama_shared" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2582b77e0f3c506ec4838a25fa8a5f97b9bed72bb6d3d272ea1c031d8bd373bc" +dependencies = [ + "askama_escape", + "humansize", + "nom", + "num-traits", + "percent-encoding", + "proc-macro2", + "quote", + "serde", + "syn", + "toml", +] + [[package]] name = "atty" version = "0.2.14" @@ -81,6 +133,18 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "bitvec" +version = "0.19.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8942c8d352ae1838c9dda0b0ca2ab657696ef2232a20147cf1b30ae1a9cb4321" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + [[package]] name = "blake2" version = "0.9.1" @@ -277,6 +341,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +[[package]] +name = "funty" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" + [[package]] name = "futures" version = "0.3.14" @@ -376,6 +446,7 @@ name = "gradecoin" version = "0.1.0" dependencies = [ "aes", + "askama", "base64 0.13.0", "blake2", "block-modes", @@ -495,6 +566,12 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" +[[package]] +name = "humansize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e" + [[package]] name = "humantime" version = "1.3.0" @@ -615,6 +692,19 @@ dependencies = [ "spin", ] +[[package]] +name = "lexical-core" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21f866863575d0e1d654fbeeabdc927292fdf862873dc3c96c6f753357e13374" +dependencies = [ + "arrayvec", + "bitflags", + "cfg-if 1.0.0", + "ryu", + "static_assertions", +] + [[package]] name = "libc" version = "0.2.93" @@ -744,6 +834,19 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "nom" +version = "6.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7413f999671bd4745a7b624bd370a569fb6bc574b23c83a3c5ed2e453f3d5e2" +dependencies = [ + "bitvec", + "funty", + "lexical-core", + "memchr", + "version_check", +] + [[package]] name = "num-bigint" version = "0.2.6" @@ -967,6 +1070,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" + [[package]] name = "rand" version = "0.7.3" @@ -1278,6 +1387,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "subtle" version = "2.4.0" @@ -1307,6 +1422,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tempfile" version = "3.2.0" @@ -1431,6 +1552,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "tower-service" version = "0.3.1" @@ -1744,6 +1874,12 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "wyz" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" + [[package]] name = "zeroize" version = "1.2.0" diff --git a/Cargo.toml b/Cargo.toml index b5733b2..b505ba4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ base64 = "0.13.0" sha2 = "0.9.3" block-modes = "0.7.0" aes = "0.6.0" +askama = "0.10.5" [dev-dependencies] serde_test = "1.0.117" diff --git a/src/handlers.rs b/src/handlers.rs index a8c9947..b3a6fa8 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,5 +1,6 @@ -/// API handlers, the ends of each filter chain use aes::Aes128; +/// API handlers, the ends of each filter chain +use askama::Template; use base64; use blake2::{Blake2s, Digest}; use block_modes::block_padding::Pkcs7; @@ -599,3 +600,19 @@ fn authorize_proposer(jwt_token: String, user_pem: &String) -> Result { + title: &'a str, + body: &'a str, +} + +pub async fn welcome_handler() -> Result { + let template = WelcomeTemplate { + title: "Welcome", + body: "To The Bookstore!", + }; + let res = template.render().unwrap(); + Ok(warp::reply::html(res)) +} diff --git a/src/routes.rs b/src/routes.rs index 280de35..f8d9605 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -7,7 +7,10 @@ use crate::schema::Db; /// Every route combined pub fn consensus_routes(db: Db) -> impl Filter + Clone { - transaction_list(db.clone()) + let welcome_route = warp::path::end().and_then(handlers::welcome_handler); + + welcome_route + .or(transaction_list(db.clone())) .or(register_user(db.clone())) .or(auth_transaction_propose(db.clone())) .or(auth_block_propose(db.clone())) @@ -60,4 +63,3 @@ pub fn auth_block_propose(db: Db) -> impl Filter + td, th { + padding: 8px; + } + th { + text-align: left; + } + diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..2ab5c0d --- /dev/null +++ b/templates/footer.html @@ -0,0 +1,2 @@ + + diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..fffbefe --- /dev/null +++ b/templates/header.html @@ -0,0 +1,11 @@ + + + Bookstore + {% include "css.html" %} + + +
+

Bookstore

+
+ {% include "menu.html" %} +
diff --git a/templates/menu.html b/templates/menu.html new file mode 100644 index 0000000..0f4e85f --- /dev/null +++ b/templates/menu.html @@ -0,0 +1,5 @@ + diff --git a/templates/welcome.html b/templates/welcome.html new file mode 100644 index 0000000..ca60f33 --- /dev/null +++ b/templates/welcome.html @@ -0,0 +1,8 @@ +{% include "header.html" %} +
+

{{title}}

+
+ {{body}} +
+
+{% include "footer.html" %} -- cgit v1.2.3-70-g09d2 From 57d833094c4e5f54d2a8ad8816595eea7ddc21de Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 15 Apr 2021 13:10:18 +0300 Subject: Remove unused code --- src/error.rs | 38 -------------------------------------- src/lib.rs | 1 - 2 files changed, 39 deletions(-) delete mode 100644 src/error.rs (limited to 'src') diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index 7339a06..0000000 --- a/src/error.rs +++ /dev/null @@ -1,38 +0,0 @@ -use log::warn; -use serde::Serialize; -use std::convert::Infallible; -use warp::{http::StatusCode, Rejection, Reply}; - -#[derive(Serialize)] -struct ErrorResponse { - message: String, -} - -pub async fn handle_rejection(err: Rejection) -> std::result::Result { - let code; - let message; - - if err.is_not_found() { - code = StatusCode::NOT_FOUND; - message = "Requested resource is not found"; - } else if let Some(_) = err.find::() { - code = StatusCode::BAD_REQUEST; - message = "Error: JSON body is not formatted correctly, check your payload"; - } else if let Some(_) = err.find::() { - code = StatusCode::METHOD_NOT_ALLOWED; - message = "Error: Authorization header missing, cannot authorize"; - } else if let Some(_) = err.find::() { - code = StatusCode::METHOD_NOT_ALLOWED; - message = "Error: method not allowed on this endpoint"; - } else { - warn!("unhandled error: {:?}", err); - code = StatusCode::INTERNAL_SERVER_ERROR; - message = "Internal Server Error"; - } - - let json = warp::reply::json(&ErrorResponse { - message: message.to_owned(), - }); - - Ok(warp::reply::with_status(json, code)) -} diff --git a/src/lib.rs b/src/lib.rs index 7a24f9f..82fb51f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,6 @@ //! `Authorization`: The request header should have Bearer JWT.Token signed with Student Public Key pub mod custom_filters; -pub mod error; pub mod handlers; pub mod routes; pub mod schema; -- cgit v1.2.3-70-g09d2 From 77b99f7d3a8747f562f2b8f1e8df551aafea1b28 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 15 Apr 2021 13:35:06 +0300 Subject: Remove lorems and inpsumses --- site/content/block_docs.md | 27 ++++++++++-- site/content/register_docs.md | 51 +++++++++++----------- site/content/transaction_docs.md | 23 ++++++++-- site/public/block-docs/index.html | 49 +++++++++++++++++++-- site/public/register-docs/index.html | 75 ++++++++++++--------------------- site/public/search_index.en.js | 2 +- site/public/transaction-docs/index.html | 47 ++++++++++++++++++--- src/handlers.rs | 2 +- src/schema.rs | 2 +- 9 files changed, 183 insertions(+), 95 deletions(-) (limited to 'src') diff --git a/site/content/block_docs.md b/site/content/block_docs.md index 26803bd..4227f26 100644 --- a/site/content/block_docs.md +++ b/site/content/block_docs.md @@ -4,8 +4,27 @@ description = "Block Documentation" weight = 2 +++ -Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod -tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At -vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd -ubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. +A block that was proposed to commit Transactions in `transaction_list` to the +ledger with a nonce that made `hash` valid; 6 zeroes at the left hand side of the +hash (24 bytes). +We are _mining_ using [blake2s](https://www.blake2.net/) algorithm, which produces 256 bit hashes. Hash/second is roughly 20x10^3 on my machine, a new block can be mined in around 4-6 minutes. + +# Requests + +## GET +A HTTP `GET` request to [/block](/block) endpoint will return the latest mined block. + +## POST + +A HTTP `POST` request with Authorization using JWT will allow you to propose your own blocks. + +# Fields +``` +transaction_list: [array of Fingerprints] +nonce: unsigned 32-bit integer +timestamp: ISO 8601 T