From 16b48b81c500e08c9d5e8dbf82810aba3f218813 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 15 Nov 2018 10:14:08 +0100 Subject: [PATCH] move ApiConfig into extra file --- src/api_config.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++ src/api_server.rs | 64 +++-------------------------------------------- src/lib.rs | 2 ++ src/main.rs | 8 +++--- 4 files changed, 75 insertions(+), 63 deletions(-) create mode 100644 src/api_config.rs diff --git a/src/api_config.rs b/src/api_config.rs new file mode 100644 index 00000000..3d2f1154 --- /dev/null +++ b/src/api_config.rs @@ -0,0 +1,64 @@ +use crate::api_info::*; +use crate::json_schema::*; + +use std::collections::HashMap; +use std::path::{PathBuf}; + +use hyper::Method; + +pub struct ApiConfig { + basedir: PathBuf, + router: &'static MethodInfo, + aliases: HashMap, +} + +impl ApiConfig { + + pub fn new>(basedir: B, router: &'static MethodInfo) -> Self { + Self { + basedir: basedir.into(), + router: router, + aliases: HashMap::new(), + } + } + + pub fn find_method(&self, components: &[&str], method: Method) -> Option<&'static ApiMethod> { + + if let Some(info) = self.router.find_route(components) { + println!("FOUND INFO"); + let opt_api_method = match method { + Method::GET => &info.get, + Method::PUT => &info.put, + Method::POST => &info.post, + Method::DELETE => &info.delete, + _ => &None, + }; + if let Some(api_method) = opt_api_method { + return Some(&api_method); + } + } + None + } + + pub fn find_alias(&self, components: &[&str]) -> PathBuf { + + let mut prefix = String::new(); + let mut filename = self.basedir.clone(); + let comp_len = components.len(); + if comp_len >= 1 { + prefix.push_str(components[0]); + if let Some(subdir) = self.aliases.get(&prefix) { + filename.push(subdir); + for i in 1..comp_len { filename.push(components[i]) } + } + } + filename + } + + pub fn add_alias(&mut self, alias: S, path: P) + where S: Into, + P: Into, + { + self.aliases.insert(alias.into(), path.into()); + } +} diff --git a/src/api_server.rs b/src/api_server.rs index 26558995..7e5746fa 100644 --- a/src/api_server.rs +++ b/src/api_server.rs @@ -1,3 +1,7 @@ +use crate::json_schema::*; +use crate::api_info::*; +use crate::api_config::*; + use std::collections::HashMap; use std::path::{PathBuf}; use std::sync::Arc; @@ -5,8 +9,6 @@ use std::sync::Arc; use failure::*; use serde_json::{json, Value}; -use crate::json_schema::*; -use crate::api_info::*; use futures::future::{self, Either}; @@ -23,64 +25,6 @@ use hyper::service::{Service, NewService}; use hyper::rt::{Future, Stream}; use hyper::header; - -pub struct ApiConfig { - basedir: PathBuf, - router: &'static MethodInfo, - aliases: HashMap, -} - -impl ApiConfig { - - pub fn new>(basedir: B, router: &'static MethodInfo) -> Self { - Self { - basedir: basedir.into(), - router: router, - aliases: HashMap::new(), - } - } - - pub fn find_method(&self, components: &[&str], method: Method) -> Option<&'static ApiMethod> { - - if let Some(info) = self.router.find_route(components) { - println!("FOUND INFO"); - let opt_api_method = match method { - Method::GET => &info.get, - Method::PUT => &info.put, - Method::POST => &info.post, - Method::DELETE => &info.delete, - _ => &None, - }; - if let Some(api_method) = opt_api_method { - return Some(&api_method); - } - } - None - } - - pub fn find_alias(&self, components: &[&str]) -> PathBuf { - - let mut prefix = String::new(); - let mut filename = self.basedir.clone(); - let comp_len = components.len(); - if comp_len >= 1 { - prefix.push_str(components[0]); - if let Some(subdir) = self.aliases.get(&prefix) { - filename.push(subdir); - for i in 1..comp_len { filename.push(components[i]) } - } - } - filename - } - - pub fn add_alias(&mut self, alias: S, path: P) - where S: Into, - P: Into, - { - self.aliases.insert(alias.into(), path.into()); - } -} - type BoxFut = Box, Error = failure::Error> + Send>; macro_rules! error_response { diff --git a/src/lib.rs b/src/lib.rs index 1310d49a..61f3e893 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,8 @@ pub mod json_schema; #[macro_use] pub mod api_info; +pub mod api_config; + pub mod api_server; pub mod api3; diff --git a/src/main.rs b/src/main.rs index 70439ff3..9315c844 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ +//use apitest::json_schema::*; +use apitest::api_info::*; +use apitest::api_config::*; +use apitest::api_server::*; + //use failure::*; use lazy_static::lazy_static; -//use apitest::json_schema::*; -use apitest::api_info::*; -use apitest::api_server::*; use futures::future::Future;