proxmox-backup/src/main.rs

222 lines
5.7 KiB
Rust
Raw Normal View History

2018-10-30 09:40:05 +00:00
extern crate failure;
2018-10-30 13:06:37 +00:00
use failure::*;
2018-10-30 09:04:30 +00:00
2018-11-01 12:05:45 +00:00
use apitest::static_map::StaticMap;
2018-11-01 10:30:49 +00:00
use std::collections::HashMap;
2018-10-31 09:42:14 +00:00
#[macro_use]
extern crate apitest;
use apitest::json_schema::*;
use apitest::api_info::*;
2018-10-30 09:04:30 +00:00
2018-10-30 13:06:37 +00:00
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use serde_json::{json, Value};
2018-11-01 10:30:49 +00:00
extern crate url;
use url::form_urlencoded;
2018-10-31 09:42:14 +00:00
extern crate hyper;
2018-11-01 10:30:49 +00:00
use hyper::{Method, Body, Request, Response, Server, StatusCode};
2018-10-31 09:42:14 +00:00
use hyper::rt::Future;
use hyper::service::service_fn_ok;
2018-10-30 09:04:30 +00:00
2018-11-01 13:16:41 +00:00
static PARAMETERS1: PropertyMap = PropertyMap {
2018-11-01 12:05:45 +00:00
entries: &[
("force", Boolean!{
description => "Test for boolean options."
}),
("text1", ApiString!{
description => "A simple text string.",
min_length => Some(10),
max_length => Some(30)
}),
("count", Integer!{
description => "A counter for everything.",
minimum => Some(0),
maximum => Some(10)
}),
("myarray1", Array!{
description => "Test Array of simple integers.",
items => &PVE_VMID
}),
("myarray2", Jss::Array(JssArray {
description: "Test Array of simple integers.",
optional: Some(false),
items: &Object!{description => "Empty Object."},
})),
("myobject", Object!{
description => "TEST Object.",
2018-11-01 13:16:41 +00:00
properties => &PropertyMap {
2018-11-01 12:05:45 +00:00
entries: &[
("vmid", Jss::Reference { reference: &PVE_VMID}),
("loop", Integer!{
description => "Totally useless thing.",
optional => Some(false)
})
]
2018-10-30 09:04:30 +00:00
}
2018-11-01 12:05:45 +00:00
}),
("emptyobject", Object!{description => "Empty Object."}),
]
2018-10-30 09:04:30 +00:00
};
2018-10-30 13:06:37 +00:00
#[derive(Serialize, Deserialize)]
struct Myparam {
test: bool,
}
fn test_api_handler(param: Value) -> Result<Value, Error> {
println!("This is a test {}", param);
// let force: Option<bool> = Some(false);
//if let Some(force) = param.force {
//}
2018-11-01 13:19:02 +00:00
let _force = param["force"].as_bool()
2018-10-30 13:06:37 +00:00
.ok_or_else(|| format_err!("meine fehlermeldung"))?;
2018-11-01 13:19:02 +00:00
if let Some(_force) = param["force"].as_bool() {
2018-10-30 13:06:37 +00:00
}
2018-11-01 13:19:02 +00:00
let _tmp: Myparam = serde_json::from_value(param)?;
2018-10-30 13:06:37 +00:00
Ok(json!(null))
}
static TEST_API_METHOD: ApiMethod = ApiMethod {
description: "This is a simple test.",
2018-11-01 13:16:41 +00:00
properties: &PropertyMap {
2018-11-01 12:05:45 +00:00
entries: &[
("force", Boolean!{
optional => Some(true),
description => "Test for boolean options."
})
]
2018-10-30 13:06:37 +00:00
},
2018-11-01 12:57:53 +00:00
returns: &Jss::Null,
2018-10-30 13:06:37 +00:00
handler: test_api_handler,
};
static API3_NODES: MethodInfo = MethodInfo {
get: Some(&TEST_API_METHOD),
2018-10-31 09:42:14 +00:00
..METHOD_INFO_DEFAULTS
2018-10-30 13:06:37 +00:00
};
2018-11-01 10:30:49 +00:00
static API_ROOT: MethodInfo = MethodInfo {
2018-10-30 13:06:37 +00:00
get: Some(&TEST_API_METHOD),
2018-11-01 13:16:41 +00:00
subdirs: Some(&SubdirMap {
2018-11-01 12:05:45 +00:00
entries: &[
("nodes", &API3_NODES),
]
}),
2018-10-31 09:42:14 +00:00
..METHOD_INFO_DEFAULTS
2018-10-30 13:06:37 +00:00
};
2018-11-01 10:30:49 +00:00
macro_rules! http_error {
($status:ident, $msg:expr) => {{
let mut resp = Response::new(Body::from($msg));
*resp.status_mut() = StatusCode::$status;
return resp;
}}
}
fn parse_query(query: &str) -> Value {
println!("PARSE QUERY {}", query);
2018-10-31 09:42:14 +00:00
2018-11-01 10:30:49 +00:00
// fixme: what about repeated parameters (arrays?)
let mut raw_param = HashMap::new();
for (k, v) in form_urlencoded::parse(query.as_bytes()) {
println!("QUERY PARAM {} value {}", k, v);
raw_param.insert(k, v);
}
println!("QUERY HASH {:?}", raw_param);
return json!(null);
}
fn handle_request(req: Request<Body>) -> Response<Body> {
2018-10-31 09:42:14 +00:00
let method = req.method();
let path = req.uri().path();
2018-11-01 10:30:49 +00:00
let query = req.uri().query();
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" {
http_error!(NOT_FOUND, format!("Unsupported format '{}'\n", format))
}
if let Some(info) = find_method_info(&API_ROOT, &components[2..]) {
println!("FOUND INFO");
let api_method_opt = match method {
&Method::GET => info.get,
&Method::PUT => info.put,
&Method::POST => info.post,
&Method::DELETE => info.delete,
_ => None,
};
2018-11-01 13:19:02 +00:00
let _api_method = match api_method_opt {
2018-11-01 10:30:49 +00:00
Some(m) => m,
_ => http_error!(NOT_FOUND, format!("No such method '{} {}'\n", method, path)),
};
// handle auth
// extract param
2018-11-01 13:19:02 +00:00
let _param = match query {
2018-11-01 10:30:49 +00:00
Some(data) => parse_query(data),
None => json!({}),
};
} else {
http_error!(NOT_FOUND, format!("No such path '{}'\n", path));
}
}
}
2018-10-31 09:42:14 +00:00
2018-11-01 10:30:49 +00:00
Response::new(Body::from("RETURN WEB GUI\n"))
2018-10-31 09:42:14 +00:00
}
2018-10-30 09:04:30 +00:00
fn main() {
println!("Fast Static Type Definitions 1");
2018-11-01 12:05:45 +00:00
for (k, v) in PARAMETERS1.entries {
2018-10-30 09:04:30 +00:00
println!("Parameter: {} Value: {:?}", k, v);
}
2018-10-30 13:06:37 +00:00
2018-10-31 09:42:14 +00:00
let addr = ([127, 0, 0, 1], 8007).into();
let new_svc = || {
// service_fn_ok converts our function into a `Service`
2018-11-01 10:30:49 +00:00
service_fn_ok(handle_request)
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
}