2018-11-15 16:07:55 +00:00
|
|
|
use failure::*;
|
|
|
|
|
2019-02-17 09:16:33 +00:00
|
|
|
use crate::api_schema::*;
|
2018-11-15 16:07:55 +00:00
|
|
|
use serde_json::{Value};
|
|
|
|
use std::collections::HashMap;
|
2018-12-06 09:41:57 +00:00
|
|
|
use std::sync::Arc;
|
2019-01-31 12:22:30 +00:00
|
|
|
use std::fmt;
|
2018-11-15 16:07:55 +00:00
|
|
|
|
2019-01-31 12:22:30 +00:00
|
|
|
use hyper::{Body, Response, StatusCode};
|
2019-01-14 11:26:04 +00:00
|
|
|
use hyper::rt::Future;
|
2019-01-17 11:43:29 +00:00
|
|
|
use hyper::http::request::Parts;
|
2019-01-14 11:26:04 +00:00
|
|
|
|
2019-04-16 08:36:04 +00:00
|
|
|
use super::api_handler::*;
|
|
|
|
|
2019-01-14 11:26:04 +00:00
|
|
|
pub type BoxFut = Box<Future<Item = Response<Body>, Error = failure::Error> + Send>;
|
|
|
|
|
2019-02-17 09:30:41 +00:00
|
|
|
/// Abstract Interface for API methods to interact with the environment
|
2019-01-26 13:50:37 +00:00
|
|
|
pub trait RpcEnvironment {
|
|
|
|
|
2019-02-17 09:30:41 +00:00
|
|
|
/// Use this to pass additional result data. It is up to the environment
|
|
|
|
/// how the data is used.
|
2019-01-26 13:50:37 +00:00
|
|
|
fn set_result_attrib(&mut self, name: &str, value: Value);
|
|
|
|
|
2019-02-17 09:30:41 +00:00
|
|
|
/// Query additional result data.
|
2019-01-26 13:50:37 +00:00
|
|
|
fn get_result_attrib(&self, name: &str) -> Option<&Value>;
|
2019-01-27 09:33:42 +00:00
|
|
|
|
2019-02-17 09:30:41 +00:00
|
|
|
/// The environment type
|
2019-01-27 09:33:42 +00:00
|
|
|
fn env_type(&self) -> RpcEnvironmentType;
|
2019-01-27 09:42:45 +00:00
|
|
|
|
2019-02-17 09:30:41 +00:00
|
|
|
/// Set user name
|
2019-01-27 09:42:45 +00:00
|
|
|
fn set_user(&mut self, user: Option<String>);
|
|
|
|
|
2019-02-17 09:30:41 +00:00
|
|
|
/// Get user name
|
2019-01-27 09:42:45 +00:00
|
|
|
fn get_user(&self) -> Option<String>;
|
2019-01-27 09:33:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 10:59:18 +00:00
|
|
|
|
|
|
|
/// Environment Type
|
|
|
|
///
|
|
|
|
/// We use this to enumerate the different environment types. Some methods
|
|
|
|
/// needs to do different things when started from the command line interface,
|
|
|
|
/// or when executed from a privileged server running as root.
|
2019-01-28 16:30:39 +00:00
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
2019-01-27 09:33:42 +00:00
|
|
|
pub enum RpcEnvironmentType {
|
2019-02-17 10:59:18 +00:00
|
|
|
/// Command started from command line
|
2019-01-27 09:33:42 +00:00
|
|
|
CLI,
|
2019-02-17 10:59:18 +00:00
|
|
|
/// Access from public accessible server
|
2019-01-27 09:33:42 +00:00
|
|
|
PUBLIC,
|
2019-02-17 10:59:18 +00:00
|
|
|
/// Access from privileged server (run as root)
|
|
|
|
PRIVILEGED,
|
2019-01-26 13:50:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 12:22:30 +00:00
|
|
|
#[derive(Debug, Fail)]
|
|
|
|
pub struct HttpError {
|
|
|
|
pub code: StatusCode,
|
|
|
|
pub message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpError {
|
|
|
|
pub fn new(code: StatusCode, message: String) -> Self {
|
|
|
|
HttpError { code, message }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for HttpError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-02-17 16:18:44 +00:00
|
|
|
write!(f, "{}", self.message)
|
2019-01-31 12:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! http_err {
|
|
|
|
($status:ident, $msg:expr) => {{
|
|
|
|
Error::from(HttpError::new(StatusCode::$status, $msg))
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-04-16 08:36:04 +00:00
|
|
|
type ApiAsyncHandlerFn = Box<
|
|
|
|
dyn Fn(Parts, Body, Value, &ApiAsyncMethod, &mut dyn RpcEnvironment) -> Result<BoxFut, Error>
|
|
|
|
+ Send + Sync + 'static
|
|
|
|
>;
|
2019-01-14 11:26:04 +00:00
|
|
|
|
2019-02-01 09:38:07 +00:00
|
|
|
/// This struct defines synchronous API call which returns the restulkt as json `Value`
|
2018-11-15 16:07:55 +00:00
|
|
|
pub struct ApiMethod {
|
2019-02-01 08:54:56 +00:00
|
|
|
/// The protected flag indicates that the provides function should be forwarded
|
|
|
|
/// to the deaemon running in priviledged mode.
|
2019-01-28 16:18:42 +00:00
|
|
|
pub protected: bool,
|
2019-02-01 08:54:56 +00:00
|
|
|
/// This flag indicates that the provided method may change the local timezone, so the server
|
|
|
|
/// should do a tzset afterwards
|
|
|
|
pub reload_timezone: bool,
|
2019-02-01 09:38:07 +00:00
|
|
|
/// Parameter type Schema
|
2018-11-18 07:46:26 +00:00
|
|
|
pub parameters: ObjectSchema,
|
2019-02-01 09:38:07 +00:00
|
|
|
/// Return type Schema
|
2018-12-06 09:41:57 +00:00
|
|
|
pub returns: Arc<Schema>,
|
2019-02-01 09:38:07 +00:00
|
|
|
/// Handler function
|
2019-04-16 08:36:04 +00:00
|
|
|
pub handler: Option<ApiHandlerFn>,
|
2018-12-06 09:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiMethod {
|
|
|
|
|
2019-04-16 08:36:04 +00:00
|
|
|
pub fn new<F, Args, R, MetaArgs>(func: F, parameters: ObjectSchema) -> Self
|
|
|
|
where
|
|
|
|
F: WrapApiHandler<Args, R, MetaArgs>,
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
parameters,
|
|
|
|
handler: Some(func.wrap()),
|
|
|
|
returns: Arc::new(Schema::Null),
|
|
|
|
protected: false,
|
|
|
|
reload_timezone: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_dummy(parameters: ObjectSchema) -> Self {
|
2018-12-06 09:23:45 +00:00
|
|
|
Self {
|
|
|
|
parameters,
|
2019-04-16 08:36:04 +00:00
|
|
|
handler: None,
|
2018-12-06 09:41:57 +00:00
|
|
|
returns: Arc::new(Schema::Null),
|
2019-01-28 16:18:42 +00:00
|
|
|
protected: false,
|
2019-02-01 08:54:56 +00:00
|
|
|
reload_timezone: false,
|
2018-12-06 09:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 09:41:57 +00:00
|
|
|
pub fn returns<S: Into<Arc<Schema>>>(mut self, schema: S) -> Self {
|
|
|
|
|
|
|
|
self.returns = schema.into();
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2019-01-28 16:18:42 +00:00
|
|
|
|
|
|
|
pub fn protected(mut self, protected: bool) -> Self {
|
|
|
|
|
|
|
|
self.protected = protected;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2019-02-01 08:54:56 +00:00
|
|
|
|
|
|
|
pub fn reload_timezone(mut self, reload_timezone: bool) -> Self {
|
|
|
|
|
|
|
|
self.reload_timezone = reload_timezone;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 15:42:43 +00:00
|
|
|
pub struct ApiAsyncMethod {
|
2019-01-14 11:26:04 +00:00
|
|
|
pub parameters: ObjectSchema,
|
|
|
|
pub returns: Arc<Schema>,
|
2019-01-19 15:42:43 +00:00
|
|
|
pub handler: ApiAsyncHandlerFn,
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 15:42:43 +00:00
|
|
|
impl ApiAsyncMethod {
|
2019-01-14 11:26:04 +00:00
|
|
|
|
2019-04-16 08:36:04 +00:00
|
|
|
pub fn new<F>(handler: F, parameters: ObjectSchema) -> Self
|
|
|
|
where
|
|
|
|
F: Fn(Parts, Body, Value, &ApiAsyncMethod, &mut dyn RpcEnvironment) -> Result<BoxFut, Error>
|
|
|
|
+ Send + Sync + 'static,
|
|
|
|
{
|
2019-01-14 11:26:04 +00:00
|
|
|
Self {
|
|
|
|
parameters,
|
2019-04-16 08:36:04 +00:00
|
|
|
handler: Box::new(handler),
|
2019-01-14 11:26:04 +00:00
|
|
|
returns: Arc::new(Schema::Null),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn returns<S: Into<Arc<Schema>>>(mut self, schema: S) -> Self {
|
2018-12-06 09:41:57 +00:00
|
|
|
|
2019-01-14 11:26:04 +00:00
|
|
|
self.returns = schema.into();
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2018-11-15 16:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SubRoute {
|
|
|
|
None,
|
|
|
|
Hash(HashMap<String, Router>),
|
|
|
|
MatchAll { router: Box<Router>, param_name: String },
|
|
|
|
}
|
|
|
|
|
2019-01-14 11:26:04 +00:00
|
|
|
pub enum MethodDefinition {
|
|
|
|
None,
|
|
|
|
Simple(ApiMethod),
|
2019-01-19 15:42:43 +00:00
|
|
|
Async(ApiAsyncMethod),
|
2019-01-14 11:26:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 16:07:55 +00:00
|
|
|
pub struct Router {
|
2019-01-14 11:26:04 +00:00
|
|
|
pub get: MethodDefinition,
|
|
|
|
pub put: MethodDefinition,
|
|
|
|
pub post: MethodDefinition,
|
|
|
|
pub delete: MethodDefinition,
|
2018-11-15 16:07:55 +00:00
|
|
|
pub subroute: SubRoute,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Router {
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2019-01-14 11:26:04 +00:00
|
|
|
get: MethodDefinition::None,
|
|
|
|
put: MethodDefinition::None,
|
|
|
|
post: MethodDefinition::None,
|
|
|
|
delete: MethodDefinition::None,
|
2018-11-15 16:07:55 +00:00
|
|
|
subroute: SubRoute::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 09:23:45 +00:00
|
|
|
pub fn subdir<S: Into<String>>(mut self, subdir: S, router: Router) -> Self {
|
|
|
|
if let SubRoute::None = self.subroute {
|
|
|
|
self.subroute = SubRoute::Hash(HashMap::new());
|
|
|
|
}
|
|
|
|
match self.subroute {
|
|
|
|
SubRoute::Hash(ref mut map) => {
|
|
|
|
map.insert(subdir.into(), router);
|
|
|
|
}
|
|
|
|
_ => panic!("unexpected subroute type"),
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:07:55 +00:00
|
|
|
pub fn subdirs(mut self, map: HashMap<String, Router>) -> Self {
|
|
|
|
self.subroute = SubRoute::Hash(map);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-12-06 09:23:45 +00:00
|
|
|
pub fn match_all<S: Into<String>>(mut self, param_name: S, router: Router) -> Self {
|
|
|
|
if let SubRoute::None = self.subroute {
|
|
|
|
self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: param_name.into() };
|
|
|
|
} else {
|
|
|
|
panic!("unexpected subroute type");
|
|
|
|
}
|
2018-11-15 16:07:55 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(mut self, m: ApiMethod) -> Self {
|
2019-01-14 11:26:04 +00:00
|
|
|
self.get = MethodDefinition::Simple(m);
|
2018-11-15 16:07:55 +00:00
|
|
|
self
|
|
|
|
}
|
2018-12-06 09:23:45 +00:00
|
|
|
|
2018-11-16 07:24:10 +00:00
|
|
|
pub fn put(mut self, m: ApiMethod) -> Self {
|
2019-01-14 11:26:04 +00:00
|
|
|
self.put = MethodDefinition::Simple(m);
|
2018-11-16 07:24:10 +00:00
|
|
|
self
|
|
|
|
}
|
2018-12-06 09:23:45 +00:00
|
|
|
|
2018-11-16 07:24:10 +00:00
|
|
|
pub fn post(mut self, m: ApiMethod) -> Self {
|
2019-01-14 11:26:04 +00:00
|
|
|
self.post = MethodDefinition::Simple(m);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:42:43 +00:00
|
|
|
pub fn upload(mut self, m: ApiAsyncMethod) -> Self {
|
|
|
|
self.post = MethodDefinition::Async(m);
|
2018-11-16 07:24:10 +00:00
|
|
|
self
|
|
|
|
}
|
2018-12-06 09:23:45 +00:00
|
|
|
|
2019-01-19 15:42:43 +00:00
|
|
|
pub fn download(mut self, m: ApiAsyncMethod) -> Self {
|
|
|
|
self.get = MethodDefinition::Async(m);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-06 09:18:06 +00:00
|
|
|
pub fn upgrade(mut self, m: ApiAsyncMethod) -> Self {
|
|
|
|
self.get = MethodDefinition::Async(m);
|
|
|
|
self
|
|
|
|
}
|
2019-01-19 15:42:43 +00:00
|
|
|
|
2018-11-16 07:24:10 +00:00
|
|
|
pub fn delete(mut self, m: ApiMethod) -> Self {
|
2019-01-14 11:26:04 +00:00
|
|
|
self.delete = MethodDefinition::Simple(m);
|
2018-11-16 07:24:10 +00:00
|
|
|
self
|
|
|
|
}
|
2018-11-15 16:07:55 +00:00
|
|
|
|
2018-11-16 08:15:33 +00:00
|
|
|
pub fn find_route(&self, components: &[&str], uri_param: &mut HashMap<String, String>) -> Option<&Router> {
|
2018-11-15 16:07:55 +00:00
|
|
|
|
|
|
|
if components.len() == 0 { return Some(self); };
|
|
|
|
|
|
|
|
let (dir, rest) = (components[0], &components[1..]);
|
|
|
|
|
|
|
|
match self.subroute {
|
|
|
|
SubRoute::None => {},
|
|
|
|
SubRoute::Hash(ref dirmap) => {
|
|
|
|
if let Some(ref router) = dirmap.get(dir) {
|
|
|
|
println!("FOUND SUBDIR {}", dir);
|
2018-11-16 08:15:33 +00:00
|
|
|
return router.find_route(rest, uri_param);
|
2018-11-15 16:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SubRoute::MatchAll { ref router, ref param_name } => {
|
|
|
|
println!("URI PARAM {} = {}", param_name, dir); // fixme: store somewhere
|
2018-11-16 08:15:33 +00:00
|
|
|
uri_param.insert(param_name.clone(), dir.into());
|
|
|
|
return router.find_route(rest, uri_param);
|
2018-11-15 16:07:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|