chore: add reqenv

This commit is contained in:
Jesús Pérez Lorenzo 2021-09-01 19:31:00 +01:00
parent db96e0284a
commit bb02cfa772
5 changed files with 201 additions and 0 deletions

10
reqenv/.gitignore vendored Normal file
View File

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

56
reqenv/Cargo.toml Normal file
View File

@ -0,0 +1,56 @@
[package]
name = "reqenv"
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_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"

7
reqenv/README.md Normal file
View File

@ -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 <u>reqenv</u>

9
reqenv/TODO.md Normal file
View File

@ -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/)

119
reqenv/src/lib.rs Normal file
View File

@ -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<HeaderValue>,
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<String> {
self.req.token_from_header()
}
#[allow(clippy::missing_errors_doc)]
pub async fn token_session(&self, login: &LoginRequest) -> anyhow::Result<String> {
self.req.token_session(login).await
}
#[allow(clippy::missing_errors_doc)]
pub async fn user_authentication(&self) -> anyhow::Result<UserCtx> {
self.req.user_authentication().await
}
}