move ApiConfig into extra file
This commit is contained in:
parent
9bc17e8dcd
commit
16b48b81c5
|
@ -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<String, PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ApiConfig {
|
||||||
|
|
||||||
|
pub fn new<B: Into<PathBuf>>(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<S, P>(&mut self, alias: S, path: P)
|
||||||
|
where S: Into<String>,
|
||||||
|
P: Into<PathBuf>,
|
||||||
|
{
|
||||||
|
self.aliases.insert(alias.into(), path.into());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,7 @@
|
||||||
|
use crate::json_schema::*;
|
||||||
|
use crate::api_info::*;
|
||||||
|
use crate::api_config::*;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::{PathBuf};
|
use std::path::{PathBuf};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -5,8 +9,6 @@ use std::sync::Arc;
|
||||||
use failure::*;
|
use failure::*;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
use crate::json_schema::*;
|
|
||||||
use crate::api_info::*;
|
|
||||||
|
|
||||||
|
|
||||||
use futures::future::{self, Either};
|
use futures::future::{self, Either};
|
||||||
|
@ -23,64 +25,6 @@ use hyper::service::{Service, NewService};
|
||||||
use hyper::rt::{Future, Stream};
|
use hyper::rt::{Future, Stream};
|
||||||
use hyper::header;
|
use hyper::header;
|
||||||
|
|
||||||
|
|
||||||
pub struct ApiConfig {
|
|
||||||
basedir: PathBuf,
|
|
||||||
router: &'static MethodInfo,
|
|
||||||
aliases: HashMap<String, PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ApiConfig {
|
|
||||||
|
|
||||||
pub fn new<B: Into<PathBuf>>(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<S, P>(&mut self, alias: S, path: P)
|
|
||||||
where S: Into<String>,
|
|
||||||
P: Into<PathBuf>,
|
|
||||||
{
|
|
||||||
self.aliases.insert(alias.into(), path.into());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type BoxFut = Box<Future<Item = Response<Body>, Error = failure::Error> + Send>;
|
type BoxFut = Box<Future<Item = Response<Body>, Error = failure::Error> + Send>;
|
||||||
|
|
||||||
macro_rules! error_response {
|
macro_rules! error_response {
|
||||||
|
|
|
@ -6,6 +6,8 @@ pub mod json_schema;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod api_info;
|
pub mod api_info;
|
||||||
|
|
||||||
|
pub mod api_config;
|
||||||
|
|
||||||
pub mod api_server;
|
pub mod api_server;
|
||||||
|
|
||||||
pub mod api3;
|
pub mod api3;
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
|
//use apitest::json_schema::*;
|
||||||
|
use apitest::api_info::*;
|
||||||
|
use apitest::api_config::*;
|
||||||
|
use apitest::api_server::*;
|
||||||
|
|
||||||
//use failure::*;
|
//use failure::*;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
//use apitest::json_schema::*;
|
|
||||||
use apitest::api_info::*;
|
|
||||||
use apitest::api_server::*;
|
|
||||||
|
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue