diff options
author | Yigit Sever | 2021-04-13 04:05:44 +0300 |
---|---|---|
committer | Yigit Sever | 2021-04-13 04:05:49 +0300 |
commit | b06cbd69fc2a7544f6f62c20cbdfb30bda194101 (patch) | |
tree | e639c13328c3c0083f3f343fb5633290ec2bf29c /src/custom_filters.rs | |
parent | 42cad1e409727ae8e1fd9f9d1c0b520e9c0565f8 (diff) | |
download | gradecoin-b06cbd69fc2a7544f6f62c20cbdfb30bda194101.tar.gz gradecoin-b06cbd69fc2a7544f6f62c20cbdfb30bda194101.tar.bz2 gradecoin-b06cbd69fc2a7544f6f62c20cbdfb30bda194101.zip |
Housekeeping
Moved tests out of routes.rs into their own file
Learned how to use lib.rs, now we have cargo doc support as well
Diffstat (limited to 'src/custom_filters.rs')
-rw-r--r-- | src/custom_filters.rs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/custom_filters.rs b/src/custom_filters.rs index f93f572..dfdae04 100644 --- a/src/custom_filters.rs +++ b/src/custom_filters.rs | |||
@@ -1,30 +1,38 @@ | |||
1 | use gradecoin::schema::{AuthRequest, Block, Db, Transaction}; | 1 | /// Functions that extracts Structs to be used in warp routines |
2 | use crate::schema::{AuthRequest, Block, Db, Transaction}; | ||
2 | use std::convert::Infallible; | 3 | use std::convert::Infallible; |
3 | use warp::{Filter, Rejection}; | 4 | use warp::{Filter, Rejection}; |
4 | 5 | ||
5 | // Database context for routes | 6 | /// Wraps the database to be used in warp routes |
6 | pub fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = Infallible> + Clone { | 7 | pub fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = Infallible> + Clone { |
7 | warp::any().map(move || db.clone()) | 8 | warp::any().map(move || db.clone()) |
8 | } | 9 | } |
9 | 10 | ||
10 | // Accept only json encoded User body and reject big payloads | 11 | /// Extracts an `AuthRequest` JSON body from the request |
12 | /// Accepts only JSON encoded `AuthRequest` body and rejects big payloads | ||
13 | /// | ||
11 | // TODO: find a good limit for this, (=e2482057; 8 char String + rsa pem) <11-04-21, yigit> // | 14 | // TODO: find a good limit for this, (=e2482057; 8 char String + rsa pem) <11-04-21, yigit> // |
12 | pub fn auth_request_json_body() -> impl Filter<Extract = (AuthRequest,), Error = Rejection> + Clone | 15 | pub fn auth_request_json_body() -> impl Filter<Extract = (AuthRequest,), Error = Rejection> + Clone |
13 | { | 16 | { |
14 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 17 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) |
15 | } | 18 | } |
16 | 19 | ||
17 | // Accept only json encoded Transaction body and reject big payloads | 20 | /// Extracts an `Transaction` JSON body from the request |
21 | /// Accepts only JSON encoded `Transaction` body and rejects big payloads | ||
18 | // TODO: find a good limit for this <11-04-21, yigit> // | 22 | // TODO: find a good limit for this <11-04-21, yigit> // |
19 | pub fn transaction_json_body() -> impl Filter<Extract = (Transaction,), Error = Rejection> + Clone { | 23 | pub fn transaction_json_body() -> impl Filter<Extract = (Transaction,), Error = Rejection> + Clone { |
20 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 24 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) |
21 | } | 25 | } |
22 | 26 | ||
27 | /// Extracts the value of the `Authorization` header field, hopefully a valid JWT | ||
28 | /// Used in Authorization for `Block` and `Transaction` proposals | ||
29 | /// Rejects the request if the Authorization header does not exist | ||
23 | pub fn auth_header() -> impl Filter<Extract = (String,), Error = Rejection> + Clone { | 30 | pub fn auth_header() -> impl Filter<Extract = (String,), Error = Rejection> + Clone { |
24 | warp::header::header::<String>("Authorization") | 31 | warp::header::header::<String>("Authorization") |
25 | } | 32 | } |
26 | 33 | ||
27 | // Accept only json encoded Block body and reject big payloads | 34 | /// Extracts an `Block` JSON body from the request |
35 | /// Accepts only JSON encoded `Block` body and rejects big payloads | ||
28 | // TODO: find a good limit for this <11-04-21, yigit> // | 36 | // TODO: find a good limit for this <11-04-21, yigit> // |
29 | pub fn block_json_body() -> impl Filter<Extract = (Block,), Error = Rejection> + Clone { | 37 | pub fn block_json_body() -> impl Filter<Extract = (Block,), Error = Rejection> + Clone { |
30 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) | 38 | warp::body::content_length_limit(1024 * 32).and(warp::body::json()) |