From bb02cfa772310deed57cc4162c362a04b78a6d1d Mon Sep 17 00:00:00 2001 From: JesusPerez Date: Wed, 1 Sep 2021 19:31:00 +0100 Subject: [PATCH] chore: add reqenv --- reqenv/.gitignore | 10 ++++ reqenv/Cargo.toml | 56 ++++++++++++++++++++++ reqenv/README.md | 7 +++ reqenv/TODO.md | 9 ++++ reqenv/src/lib.rs | 119 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+) create mode 100644 reqenv/.gitignore create mode 100644 reqenv/Cargo.toml create mode 100644 reqenv/README.md create mode 100644 reqenv/TODO.md create mode 100644 reqenv/src/lib.rs diff --git a/reqenv/.gitignore b/reqenv/.gitignore new file mode 100644 index 0000000..c3a29c2 --- /dev/null +++ b/reqenv/.gitignore @@ -0,0 +1,10 @@ +/target +target +Cargo.lock +.cache +.temp +.env +*.log +.DS_Store +logs +tmp diff --git a/reqenv/Cargo.toml b/reqenv/Cargo.toml new file mode 100644 index 0000000..79f6921 --- /dev/null +++ b/reqenv/Cargo.toml @@ -0,0 +1,56 @@ +[package] +name = "reqenv" +version = "0.1.0" +authors = ["JesusPerez "] +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_errors = { version = "0.1.0", path = "../../defs/app_errors" } +reqtasks = { version = "0.1.0", path = "../reqtasks" } + +[dev-dependencies] +pretty_env_logger = "0.4" +tracing-subscriber = "0.2.15" +tracing-log = "0.1" +serde_derive = "1.0.125" +handlebars = "4.1.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" diff --git a/reqenv/README.md b/reqenv/README.md new file mode 100644 index 0000000..f4d9c73 --- /dev/null +++ b/reqenv/README.md @@ -0,0 +1,7 @@ +# ReqTasks handlers library + +Handlers to manage request using [warp](https://github.com/seanmonstar/warp) + +It use one struct **ReqTasks** + +It can be extended or included in other types like reqenv \ No newline at end of file diff --git a/reqenv/TODO.md b/reqenv/TODO.md new file mode 100644 index 0000000..467dd88 --- /dev/null +++ b/reqenv/TODO.md @@ -0,0 +1,9 @@ +# ReqEnv handlers library + +- [ ] Tests + +- [ ] Manage tokens and certification + +- [ ] Dynamic load profiles, modules, etc + +- [ ] Track sessions, devices, etc. [server timing](https://w3c.github.io/server-timing/) diff --git a/reqenv/src/lib.rs b/reqenv/src/lib.rs new file mode 100644 index 0000000..1601763 --- /dev/null +++ b/reqenv/src/lib.rs @@ -0,0 +1,119 @@ +// +/*! Zterton +*/ +// Copyright 2021, Jesús Pérez Lorenzo +// +//use std::collections::HashMap; +use std::fmt; +//use std::str::from_utf8; +//use tera::Tera; + +use warp::{ + http::{method::Method, HeaderMap, HeaderValue}, +// Filter, +}; + +use reqtasks::ReqTasks; +use app_env::{ + appenv::AppEnv, + config::Config, + module::Module, + AppStore, + // AppData, +}; +use app_auth::{ + AuthStore, + UserCtx, + LoginRequest, + // BEARER_PREFIX, + // AuthError, +}; + +/// `ReqEnv` includes ReqTasks as core type +/// it is a kind of wrapping type +/// to declare: +/// - auth methods locally +/// - other attributes +/// - other request tasks methods +/// +#[derive(Clone)] +pub struct ReqEnv { + pub req: ReqTasks, + +} + +impl fmt::Display for ReqEnv { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} {} {}", &self.req.path, &self.req.origin, &self.req.key_module) + } +} + +impl ReqEnv { + pub fn new( + app_db: AppStore, + auth_store: AuthStore, + header: HeaderMap, + method: Method, + path: &str, + origin: &str, + key_module: &str + ) -> Self { + let app_data = app_db.app_data.read(); + // let auth_store: &'a AuthStore = &AuthStore { + // users: auth_db.users.clone(), + // sessions: auth_db.sessions.clone(), + // enforcer: auth_db.enforcer.clone(), + // }; + Self { + req: ReqTasks { + app_data: app_data.to_owned(), + auth_store: auth_store.to_owned(), + header, + method, + path: format!("{}{}",key_module,path).to_string(), + origin: format!("{}{}",key_module,origin).to_string(), + key_module: key_module.to_string(), + }, + } + } + /// Get `AppEnv` + #[must_use] + pub fn env(&self) -> AppEnv { + self.req.env() + } + /// Get Tera + #[must_use] + pub fn tera(&self) -> tera::Tera { + self.req.tera() + } + /// Get Context (ctx) + #[must_use] + pub fn ctx(&self) -> tera::Context { + self.req.ctx() + } + /// Get `AppEnv` Config + #[must_use] + pub fn config(&self) -> Config { + self.req.config() + } + #[must_use] + pub fn module(&self) -> Module { + self.req.module() + } + #[must_use] + pub fn lang(&self) -> String { + self.req.lang() + } + #[allow(clippy::missing_errors_doc)] + pub fn token_from_header(&self) -> anyhow::Result { + self.req.token_from_header() + } + #[allow(clippy::missing_errors_doc)] + pub async fn token_session(&self, login: &LoginRequest) -> anyhow::Result { + self.req.token_session(login).await + } + #[allow(clippy::missing_errors_doc)] + pub async fn user_authentication(&self) -> anyhow::Result { + self.req.user_authentication().await + } +} \ No newline at end of file