move Config into its own module
cargo-fmt
This commit is contained in:
parent
0856b7b6b2
commit
88c2e99aa8
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ simple-auth
|
||||
|
||||
tests/python/__pycache__
|
||||
tests/bash/response.txt
|
||||
tests/data/*.ini
|
||||
|
||||
131
src/config/config.rs
Normal file
131
src/config/config.rs
Normal file
@ -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<Ini> for Config {
|
||||
type Error = String;
|
||||
fn try_from(config: Ini) -> Result<Self, Self::Error> {
|
||||
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());
|
||||
}
|
||||
3
src/config/mod.rs
Normal file
3
src/config/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
mod config;
|
||||
|
||||
pub use config::Config;
|
||||
@ -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;
|
||||
|
||||
@ -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<HTTPResponse> = Pin<Box<dyn Future<Output = HTTPResponse>>>;
|
||||
type Handler = fn(HTTPRequest, Config) -> FuturePinned<HTTPResponse>;
|
||||
|
||||
#[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<Ini> for Config {
|
||||
type Error = String;
|
||||
fn try_from(config: Ini) -> Result<Self, Self::Error> {
|
||||
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<HTTPResponse> {
|
||||
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());
|
||||
}
|
||||
|
||||
@ -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)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user