From 88c2e99aa85d52dae71b24f49a7ed95961d8c421 Mon Sep 17 00:00:00 2001 From: landrigun Date: Thu, 13 Oct 2022 08:54:47 +0000 Subject: [PATCH] move Config into its own module cargo-fmt --- .gitignore | 1 + src/config/config.rs | 131 ++++++++++++++++++++++++++++++++++++++++++ src/config/mod.rs | 3 + src/http/mod.rs | 2 +- src/http/router.rs | 133 +------------------------------------------ src/main.rs | 4 +- 6 files changed, 140 insertions(+), 134 deletions(-) create mode 100644 src/config/config.rs create mode 100644 src/config/mod.rs diff --git a/.gitignore b/.gitignore index ddf9e12..8779a8a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ simple-auth tests/python/__pycache__ tests/bash/response.txt +tests/data/*.ini diff --git a/src/config/config.rs b/src/config/config.rs new file mode 100644 index 0000000..31d0bd8 --- /dev/null +++ b/src/config/config.rs @@ -0,0 +1,131 @@ +//! config module implements all the utilities to properly create and validate a router config + +use configparser::ini::Ini; +use std::str::FromStr; + +#[derive(Clone)] +pub struct Config { + pub jwt_exp_time: u64, + pub jwt_issuer: String, + pub jwt_priv_key: String, + pub jwt_pub_key: String, + pub filestore_path: String, +} + +impl Default for Config { + fn default() -> Self { + Config { + jwt_exp_time: 0, + jwt_issuer: "".to_string(), + jwt_priv_key: "".to_string(), + jwt_pub_key: "".to_string(), + filestore_path: "".to_string(), + } + } +} + +impl TryFrom for Config { + type Error = String; + fn try_from(config: Ini) -> Result { + let exp_time = config + .get("jwt", "expiration_time") + .unwrap_or("".to_string()); + let jwt_exp_time = { + match u64::from_str(&exp_time) { + Ok(v) => v, + Err(e) => { + eprintln!("unable to convert JWT expiration time into u64 err={}", e); + 0 + } + } + }; + let config = Config { + jwt_exp_time, + jwt_issuer: config.get("jwt", "issuer").unwrap_or("".to_string()), + jwt_pub_key: config.get("jwt", "public_key").unwrap_or("".to_string()), + jwt_priv_key: config.get("jwt", "private_key").unwrap_or("".to_string()), + filestore_path: config.get("store", "path").unwrap_or("".to_string()), + }; + + if !config.validate() { + return Err("ini file configuration validation failed".to_string()); + } + + Ok(config) + } +} + +impl Config { + /// validates config ini file + fn validate(&self) -> bool { + if self.jwt_exp_time <= 0 { + eprintln!("invalid config parameter: JWT expiration time is negative or equals to 0"); + return false; + } + + if self.jwt_issuer == "" { + eprintln!("invalid config parameter: JWT issuer is empty"); + return false; + } + + // TODO: check if the file exists and rights are ok + if self.jwt_pub_key == "" { + eprintln!("invalid config parameter: JWT public key file path is empty"); + return false; + } + + // TODO: check if the file exists and rights are ok + if self.jwt_priv_key == "" { + eprintln!("invalid config parameter: JWT private key file path is empty"); + return false; + } + + if self.filestore_path == "" { + eprintln!("invalid config parameter: filestore path is empty"); + return false; + } + + true + } +} + +#[test] +fn test_config() { + use std::env; + + let root_path = env::var("CARGO_MANIFEST_DIR").unwrap(); + + let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "config.ini"); + let mut config = Ini::new(); + let _r = config.load(config_path); + + let router_config = Config::try_from(config); + assert!(router_config.is_ok()); +} + +#[test] +fn test_bad_config() { + use std::env; + + let root_path = env::var("CARGO_MANIFEST_DIR").unwrap(); + + let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "bad_config.ini"); + let mut config = Ini::new(); + let _r = config.load(config_path); + + let router_config = Config::try_from(config); + assert!(router_config.is_err()); +} + +#[test] +fn test_bad_config_path() { + use std::env; + + let root_path = env::var("CARGO_MANIFEST_DIR").unwrap(); + + let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "con.ini"); + let mut config = Ini::new(); + + let result = config.load(config_path); + assert!(result.is_err()); +} diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..43fe76c --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,3 @@ +mod config; + +pub use config::Config; diff --git a/src/http/mod.rs b/src/http/mod.rs index 38d58b0..f9dbf7c 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -6,4 +6,4 @@ pub mod router; pub use request::HTTPRequest; pub use response::{HTTPResponse, HTTPStatusCode}; -pub use router::{Config, ROUTER}; +pub use router::ROUTER; diff --git a/src/http/router.rs b/src/http/router.rs index c242fc7..f2a2089 100644 --- a/src/http/router.rs +++ b/src/http/router.rs @@ -2,105 +2,18 @@ //! it implements all the logic to build an `HTTPResponse` use super::{HTTPRequest, HTTPResponse}; +use crate::config::Config; use crate::stores::FileStore; use crate::stores::Store; -use configparser::ini::Ini; use jwt_simple::prelude::*; use lazy_static::lazy_static; use std::collections::HashMap; use std::future::Future; use std::pin::Pin; -use std::str::FromStr; type FuturePinned = Pin>>; type Handler = fn(HTTPRequest, Config) -> FuturePinned; -#[derive(Clone)] -pub struct Config { - jwt_exp_time: u64, - jwt_issuer: String, - jwt_priv_key: String, - jwt_pub_key: String, - filestore_path: String, -} - -impl Default for Config { - fn default() -> Self { - Config { - jwt_exp_time: 0, - jwt_issuer: "".to_string(), - jwt_priv_key: "".to_string(), - jwt_pub_key: "".to_string(), - filestore_path: "".to_string(), - } - } -} - -impl TryFrom for Config { - type Error = String; - fn try_from(config: Ini) -> Result { - let exp_time = config - .get("jwt", "expiration_time") - .unwrap_or("".to_string()); - let jwt_exp_time = { - match u64::from_str(&exp_time) { - Ok(v) => v, - Err(e) => { - eprintln!("unable to convert JWT expiration time into u64 err={}", e); - 0 - } - } - }; - let config = Config { - jwt_exp_time, - jwt_issuer: config.get("jwt", "issuer").unwrap_or("".to_string()), - jwt_pub_key: config.get("jwt", "public_key").unwrap_or("".to_string()), - jwt_priv_key: config.get("jwt", "private_key").unwrap_or("".to_string()), - filestore_path: config.get("store", "path").unwrap_or("".to_string()), - }; - - if !config.validate() { - return Err("ini file configuration validation failed".to_string()); - } - - Ok(config) - } -} - -impl Config { - /// validates config ini file - fn validate(&self) -> bool { - if self.jwt_exp_time <= 0 { - eprintln!("invalid config parameter: JWT expiration time is negative or equals to 0"); - return false; - } - - if self.jwt_issuer == "" { - eprintln!("invalid config parameter: JWT issuer is empty"); - return false; - } - - // TODO: check if the file exists and rights are ok - if self.jwt_pub_key == "" { - eprintln!("invalid config parameter: JWT public key file path is empty"); - return false; - } - - // TODO: check if the file exists and rights are ok - if self.jwt_priv_key == "" { - eprintln!("invalid config parameter: JWT private key file path is empty"); - return false; - } - - if self.filestore_path == "" { - eprintln!("invalid config parameter: filestore path is empty"); - return false; - } - - true - } -} - fn handle_get(request: HTTPRequest, config: Config) -> FuturePinned { Box::pin(async move { let mut store = FileStore::new(config.filestore_path); @@ -205,47 +118,3 @@ async fn test_route() { response.status_line.get_status_code() ); } - -#[test] -fn test_config() { - use std::env; - - let root_path = env::var("CARGO_MANIFEST_DIR").unwrap(); - - // TODO: path::Path should be better - let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "config.ini"); - let mut config = Ini::new(); - let _r = config.load(config_path); - - let router_config = Config::try_from(config); - assert!(router_config.is_ok()); -} - -#[test] -fn test_bad_config() { - use std::env; - - let root_path = env::var("CARGO_MANIFEST_DIR").unwrap(); - - // TODO: path::Path should be better - let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "bad_config.ini"); - let mut config = Ini::new(); - let _r = config.load(config_path); - - let router_config = Config::try_from(config); - assert!(router_config.is_err()); -} - -#[test] -fn test_bad_config_path() { - use std::env; - - let root_path = env::var("CARGO_MANIFEST_DIR").unwrap(); - - // TODO: path::Path should be better - let config_path = format!("{}/{}/{}/{}", root_path, "tests", "data", "con.ini"); - let mut config = Ini::new(); - - let result = config.load(config_path); - assert!(result.is_err()); -} diff --git a/src/main.rs b/src/main.rs index cb91c62..1fb6aae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod config; mod http; mod stores; @@ -8,7 +9,8 @@ use tokio::{ net::{TcpListener, TcpStream}, }; -use http::{Config, ROUTER}; +use config::Config; +use http::ROUTER; #[derive(Parser)] #[clap(author, version, about, long_about = None)]