chore: add app_file_filters

This commit is contained in:
Jesús Pérez Lorenzo 2021-09-01 19:03:16 +01:00
parent 55004c6744
commit c5bfc9df7f
5 changed files with 215 additions and 0 deletions

10
app_file_filters/.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
/target
target
Cargo.lock
.cache
.temp
.env
*.log
.DS_Store
logs
tmp

View File

@ -0,0 +1,56 @@
[package]
name = "app_file_filters"
version = "0.1.0"
authors = ["JesusPerez <jpl@jesusperez.pro>"]
edition = "2018"
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.40"
base64 = "0.13.0"
casbin = "2.0.7"
chrono = "0.4"
dotenv = "0.15.0"
envmnt = "0.9.0"
error-chain = "0.12.4"
glob = "0.3.0"
json = "0.12.4"
once_cell = "1.7.2"
parking_lot = "0.11.1"
rand = "0.8.3"
regex = "1.4.3"
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0.125"
serde_json = "1.0.64"
serde_yaml = "0.8.17"
slab = "0.4.3"
tempfile = "3.2.0"
tera = "1.8.0"
thiserror = "1.0.24"
toml = "0.5.8"
yaml-rust = "0.4"
tokio = { version = "1.5.0", features = ["full"] }
uuid = { version = "0.8", features = ["serde", "v4"] }
url = "2.2.1"
warp = { version = "0.3", features = ["default","websocket","tls","compression"] }
app_tools = { version = "0.1.0", path = "../../utils/app_tools" }
app_env = { version = "0.1.0", path = "../../defs/app_env" }
app_auth = { version = "0.1.0", path = "../../defs/app_auth" }
app_file_handlers = { version = "0.1.0", path = "../../handlers/app_file_handlers" }
[dev-dependencies]
pretty_env_logger = "0.4"
tracing-subscriber = "0.2.15"
tracing-log = "0.1"
serde_derive = "1.0.125"
handlebars = "3.0.0"
tokio = { version = "1.5.0", features = ["macros", "rt-multi-thread"] }
tokio-stream = { version = "0.1.5", features = ["net"] }
listenfd = "0.3"
envmnt = "0.9.0"
[build-dependencies]
envmnt = "0.9.0"

View File

@ -0,0 +1,3 @@
# App auth filters library
Filters as configure routes and webservices for [warp](https://github.com/seanmonstar/warp)

5
app_file_filters/TODO.md Normal file
View File

@ -0,0 +1,5 @@
# App file filters library
- [ ] Conditional **boxed** with cargo build for production
[Warp and compile times](https://rust-classes.com/chapter_6_3.html)

141
app_file_filters/src/lib.rs Normal file
View File

@ -0,0 +1,141 @@
// use std::collections::HashMap;
// use std::convert::Infallible;
// use std::str::from_utf8;
// use std::sync::Arc;
// use tokio::sync::RwLock;
// use casbin::prelude::*;
use warp::{
filters::header::headers_cloned,
filters::method::method,
filters::BoxedFilter,
// filters::path::{full, FullPath},
// http::{header::AUTHORIZATION, method::Method, HeaderMap, HeaderValue},
Filter,// Rejection,
};
use app_env::AppStore;
use app_auth::{
AppAuthDBs,
AuthStore,
Sessions,
// AuthError,
// UserCtx,
// WebResult,
// BEARER_PREFIX,
// custom_reject
};
use app_file_handlers::{upload_handler,uploadin_handler};
#[must_use]
pub fn files(
app_db: AppStore,
auth_db: AuthStore,
//) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
) -> BoxedFilter<(impl warp::Reply,)> {
let app_auth_dbs = AppAuthDBs { app: app_db, auth: auth_db};
upload(app_auth_dbs.clone())
// .or(ui(app_auth_dbs.clone()))
.or(uploadin(app_auth_dbs.clone()))
.boxed()
}
/*
struct FutureReply<F>(F);
impl<F: futures::Future<Output = impl Reply> + std::marker::Send> Reply for FutureReply<F> {
fn into_response(self) -> warp::reply::Response {
futures::executor::block_on(self.0).into_response()
}
}
// https://www.reddit.com/r/rust/comments/hv06wy/how_to_get_boxedfilter_for_warp_multipart_fiilter/
#[must_use]
pub fn uploadin(
db: AppAuthDBs
//) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
//) -> BoxedFilter<(impl warp::Reply,)> {
) -> BoxedFilter<(FutureReply<impl futures::Future<Output = impl Reply>>,)> {
warp::path!("uploadin")
// .and(warp::post())
.and(warp::multipart::form())
.and(warp::body::json())
.and(headers_cloned())
.and(method())
.and(with_dbauth(db))
.map(uploadin_handler)
.map(|fut| FutureReply(fut))
.boxed()
}
*/
// #[must_use]
// pub fn ui(
// db: AppAuthDBs
// //) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
// ) -> BoxedFilter<(impl warp::Reply,)> {
// warp::path!("ui")
// .and(headers_cloned())
// .and(method())
// .and(with_dbauth(db))
// .and_then(ui_handler)
// .boxed()
// }
#[must_use]
pub fn uploadin(
db: AppAuthDBs
//) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
) -> BoxedFilter<(impl warp::Reply,)> {
warp::path!("uploadin")
.and(warp::post())
.and(warp::multipart::form().max_length(5_000_000))
.and(headers_cloned())
.and(method())
.and(with_dbauth(db))
/*
.and_then(|form: warp::multipart::FormData| {
async {
// Collect the fields into (name, value): (String, Vec<u8>)
let part: Result<Vec<(String, Vec<u8>)>, warp::Rejection> = form
.and_then(|part| {
let name = part.name().to_string();
let value = part.stream().try_fold(Vec::new(), |mut vec, data| {
vec.put(data);
async move { Ok(vec) }
});
value.map_ok(move |vec| (name, vec))
})
.try_collect()
.await
.map_err(|e| {
panic!("multipart error: {:?}", e);
});
part
}})
*/
.and_then(uploadin_handler)
.boxed()
}
#[must_use]
pub fn upload(
db: AppAuthDBs
//) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
) -> BoxedFilter<(impl warp::Reply,)> {
warp::path!("upload")
.and(headers_cloned())
.and(method())
.and(with_dbauth(db))
.and_then(upload_handler)
.boxed()
}
#[must_use]
pub fn with_dbauth(db: AppAuthDBs) -> impl Filter<Extract = (AppAuthDBs,), Error = std::convert::Infallible> + Clone {
warp::any().map(move || db.clone())
}
#[must_use]
pub fn with_sessions(sessions: Sessions) -> impl Filter<Extract = (Sessions,), Error = std::convert::Infallible> + Clone {
warp::any().map(move || sessions.clone())
}