proxmox-backup/src/main.rs

233 lines
6.3 KiB
Rust
Raw Normal View History

extern crate apitest;
2018-10-30 09:04:30 +00:00
2018-11-05 14:20:27 +00:00
use failure::*;
2018-11-01 12:05:45 +00:00
2018-11-09 12:48:57 +00:00
//use std::collections::HashMap;
2018-11-03 09:42:48 +00:00
use lazy_static::lazy_static;
2018-11-01 10:30:49 +00:00
2018-11-03 09:42:48 +00:00
//use apitest::json_schema::*;
2018-10-31 09:42:14 +00:00
use apitest::api_info::*;
2018-11-05 14:20:27 +00:00
use apitest::json_schema::*;
2018-10-31 09:42:14 +00:00
2018-11-03 09:42:48 +00:00
//use serde_derive::{Serialize, Deserialize};
2018-11-10 11:06:39 +00:00
use serde_json::{json, Value};
use tokio::prelude::*;
use tokio::timer::Delay;
2018-10-30 13:06:37 +00:00
2018-11-09 12:48:57 +00:00
//use hyper::body::Payload;
2018-11-09 07:22:54 +00:00
use hyper::http::request::Parts;
2018-11-01 10:30:49 +00:00
use hyper::{Method, Body, Request, Response, Server, StatusCode};
2018-11-09 07:22:54 +00:00
use hyper::rt::{Future, Stream};
use hyper::service::service_fn;
2018-11-10 11:06:39 +00:00
use futures::future::*;
use std::time::{Duration, Instant};
2018-11-09 07:22:54 +00:00
2018-11-10 09:00:48 +00:00
type BoxFut = Box<Future<Item = Response<Body>, Error = failure::Error> + Send>;
2018-10-30 09:04:30 +00:00
2018-11-10 09:00:48 +00:00
macro_rules! error_response {
2018-11-01 10:30:49 +00:00
($status:ident, $msg:expr) => {{
let mut resp = Response::new(Body::from($msg));
*resp.status_mut() = StatusCode::$status;
2018-11-10 09:00:48 +00:00
resp
2018-11-09 07:22:54 +00:00
}}
}
2018-11-10 09:00:48 +00:00
2018-11-09 07:22:54 +00:00
macro_rules! http_error_future {
($status:ident, $msg:expr) => {{
2018-11-10 09:00:48 +00:00
let resp = error_response!($status, $msg);
2018-11-10 11:06:39 +00:00
return Box::new(ok(resp));
2018-11-01 10:30:49 +00:00
}}
}
2018-11-10 11:06:39 +00:00
fn get_request_parameters_async<'a>(
2018-11-09 12:48:57 +00:00
info: &'a ApiMethod,
parts: Parts,
req_body: Body,
2018-11-10 11:06:39 +00:00
) -> Box<Future<Item = Value, Error = failure::Error> + Send + 'a>
2018-11-09 07:22:54 +00:00
{
2018-11-10 09:00:48 +00:00
let resp = req_body.concat2()
.map_err(|err| format_err!("Promlems reading request body: {}", err))
.and_then(move |body| {
2018-11-09 07:22:54 +00:00
2018-11-10 09:00:48 +00:00
let bytes = String::from_utf8(body.to_vec())?; // why copy??
2018-11-09 07:22:54 +00:00
2018-11-10 09:00:48 +00:00
println!("GOT BODY {:?}", bytes);
2018-11-09 07:22:54 +00:00
2018-11-10 09:00:48 +00:00
let mut test_required = true;
2018-11-09 07:22:54 +00:00
2018-11-10 09:00:48 +00:00
let mut params = json!({});
2018-11-09 12:48:57 +00:00
2018-11-10 09:00:48 +00:00
if bytes.len() > 0 {
params = parse_query_string(&bytes, &info.parameters, true)?;
test_required = false;
}
2018-11-09 12:48:57 +00:00
2018-11-10 09:00:48 +00:00
if let Some(query_str) = parts.uri.query() {
let query_params = parse_query_string(query_str, &info.parameters, test_required)?;
2018-11-09 12:48:57 +00:00
2018-11-10 09:00:48 +00:00
for (k, v) in query_params.as_object().unwrap() {
params[k] = v.clone(); // fixme: why clone()??
}
2018-11-09 07:22:54 +00:00
}
2018-11-10 11:06:39 +00:00
println!("GOT PARAMS {}", params);
Ok(params)
});
Box::new(resp)
}
fn handle_async_api_request<'a>(
info: &'a ApiMethod,
parts: Parts,
req_body: Body,
) -> Box<Future<Item = Response<Body>, Error = failure::Error> + Send + 'a>
{
let params = get_request_parameters_async(info, parts, req_body);
let resp = params
.and_then(move |params| {
println!("GOT PARAMS {}", params);
/*
let when = Instant::now() + Duration::from_millis(3000);
let task = Delay::new(when).then(|_| {
println!("A LAZY TASK");
ok(())
});
tokio::spawn(task);
*/
(info.async_handler)(params, info)
});
Box::new(resp)
}
fn handle_sync_api_request<'a>(
info: &'a ApiMethod,
parts: Parts,
req_body: Body,
) -> Box<Future<Item = Response<Body>, Error = failure::Error> + Send + 'a>
{
let params = get_request_parameters_async(info, parts, req_body);
let resp = params
.and_then(move |params| {
2018-11-10 09:00:48 +00:00
println!("GOT PARAMS {}", params);
2018-11-10 11:06:39 +00:00
/*
let when = Instant::now() + Duration::from_millis(3000);
let task = Delay::new(when).then(|_| {
println!("A LAZY TASK");
ok(())
});
tokio::spawn(task);
*/
2018-11-10 09:00:48 +00:00
let res = (info.handler)(params, info)?;
2018-11-09 12:48:57 +00:00
2018-11-10 09:00:48 +00:00
Ok(res)
2018-11-09 12:48:57 +00:00
2018-11-10 11:06:39 +00:00
}).then(|result| {
match result {
Ok(ref value) => {
let json_str = value.to_string();
2018-11-09 12:48:57 +00:00
2018-11-10 11:06:39 +00:00
Ok(Response::builder()
.status(StatusCode::OK)
.header("ContentType", "application/json")
.body(Body::from(json_str))?)
}
Err(err) => Ok(error_response!(BAD_REQUEST, err.to_string()))
2018-11-10 09:00:48 +00:00
}
2018-11-10 11:06:39 +00:00
});
2018-11-09 07:22:54 +00:00
Box::new(resp)
}
fn handle_request(req: Request<Body>) -> BoxFut {
let (parts, body) = req.into_parts();
2018-11-01 10:30:49 +00:00
2018-11-09 07:22:54 +00:00
let method = parts.method.clone();
let path = parts.uri.path();
2018-10-31 09:42:14 +00:00
2018-11-01 10:30:49 +00:00
let components: Vec<&str> = path.split('/').filter(|x| !x.is_empty()).collect();
let comp_len = components.len();
2018-10-31 09:42:14 +00:00
println!("REQUEST {} {}", method, path);
2018-11-01 10:30:49 +00:00
println!("COMPO {:?}", components);
if comp_len >= 1 && components[0] == "api3" {
println!("GOT API REQUEST");
if comp_len >= 2 {
let format = components[1];
if format != "json" {
2018-11-10 09:32:25 +00:00
http_error_future!(BAD_REQUEST, format!("Unsupported output format '{}'.", format))
2018-11-01 10:30:49 +00:00
}
2018-11-03 14:10:21 +00:00
if let Some(info) = ROUTER.find_method(&components[2..]) {
2018-11-01 10:30:49 +00:00
println!("FOUND INFO");
let api_method_opt = match method {
2018-11-09 07:22:54 +00:00
Method::GET => &info.get,
Method::PUT => &info.put,
Method::POST => &info.post,
Method::DELETE => &info.delete,
2018-11-03 14:10:21 +00:00
_ => &None,
2018-11-01 10:30:49 +00:00
};
2018-11-01 14:41:08 +00:00
let api_method = match api_method_opt {
2018-11-01 10:30:49 +00:00
Some(m) => m,
2018-11-10 09:32:25 +00:00
_ => http_error_future!(NOT_FOUND, format!("No such method '{}'.", method)),
2018-11-01 10:30:49 +00:00
};
2018-11-07 12:58:09 +00:00
// fixme: handle auth
2018-11-01 10:30:49 +00:00
2018-11-10 11:06:39 +00:00
//return handle_sync_api_request(api_method, parts, body);
return handle_async_api_request(api_method, parts, body);
2018-11-09 07:22:54 +00:00
2018-11-01 10:30:49 +00:00
} else {
2018-11-10 09:32:25 +00:00
http_error_future!(NOT_FOUND, "Path not found.");
2018-11-01 10:30:49 +00:00
}
}
}
2018-10-31 09:42:14 +00:00
2018-11-10 11:06:39 +00:00
Box::new(ok(Response::new(Body::from("RETURN WEB GUI\n"))))
2018-10-31 09:42:14 +00:00
}
2018-11-03 09:42:48 +00:00
lazy_static!{
2018-11-03 14:10:21 +00:00
static ref ROUTER: MethodInfo = apitest::api3::router();
2018-11-03 09:42:48 +00:00
}
2018-10-30 09:04:30 +00:00
fn main() {
println!("Fast Static Type Definitions 1");
2018-10-31 09:42:14 +00:00
let addr = ([127, 0, 0, 1], 8007).into();
let new_svc = || {
2018-11-10 09:00:48 +00:00
service_fn(|req| {
// clumsy way to convert failure::Error to Response
handle_request(req).then(|result| -> Result<Response<Body>, String> {
match result {
Ok(res) => Ok(res),
2018-11-10 09:32:25 +00:00
Err(err) => Ok(error_response!(BAD_REQUEST, err.to_string())),
2018-11-10 09:00:48 +00:00
}
})
})
2018-10-31 09:42:14 +00:00
};
let server = Server::bind(&addr)
.serve(new_svc)
.map_err(|e| eprintln!("server error: {}", e));
// Run this server for... forever!
hyper::rt::run(server);
2018-10-30 09:04:30 +00:00
}