proxmox-backup/src/api3.rs

95 lines
2.4 KiB
Rust
Raw Normal View History

2018-11-03 09:42:48 +00:00
use failure::*;
use std::collections::HashMap;
use crate::json_schema::*;
use crate::api_info::*;
use serde_json::{json, Value};
2018-11-10 11:06:39 +00:00
use futures::future::*;
use tokio::prelude::*;
use tokio::fs::File;
use tokio::io;
use tokio_codec;
2018-11-10 11:06:39 +00:00
use hyper::{Method, Body, Request, Response, Server, StatusCode};
2018-11-03 09:42:48 +00:00
2018-11-10 11:06:39 +00:00
fn test_sync_api_handler(param: Value, info: &ApiMethod) -> Result<Value, Error> {
2018-11-03 09:42:48 +00:00
println!("This is a test {}", param);
// let force: Option<bool> = Some(false);
//if let Some(force) = param.force {
//}
let _force = param["force"].as_bool()
.ok_or_else(|| format_err!("missing parameter 'force'"))?;
if let Some(_force) = param["force"].as_bool() {
}
Ok(json!(null))
}
2018-11-10 11:06:39 +00:00
fn test_async_api_handler(
param: Value,
info: &ApiMethod
) -> Box<Future<Item = Response<Body>, Error = Error> + Send> {
println!("This is a test {}", param);
let task = lazy(|| {
println!("A LAZY TASK");
let dump_file = File::open("/etc/network/interfaces")
.and_then(|file| {
let file = std::io::BufReader::new(file);
let mut linenr = 1;
tokio::io::lines(file).for_each(move |line| {
println!("LINE {}: {}", linenr, line);
linenr += 1;
ok(())
})
}).map_err(|err| ());
//tokio::spawn(dump_file);
let dump_file2 = File::open("/etc/network/interfaces")
.and_then(|file| {
tokio_codec::FramedRead::new(file, tokio_codec::BytesCodec::new()).for_each(|data| {
println!("DATA {:?}", data);
ok(())
})
}).map_err(|err| ()); // fixme: log error
tokio::spawn(dump_file2);
2018-11-10 11:06:39 +00:00
let mut resp = Response::new(Body::from("A LAZY TASKs RESPONSE"));
*resp.status_mut() = StatusCode::OK;
ok(resp)
});
Box::new(task)
}
2018-11-03 09:42:48 +00:00
2018-11-03 14:10:21 +00:00
pub fn router() -> MethodInfo {
2018-11-03 09:42:48 +00:00
2018-11-03 14:10:21 +00:00
let route = MethodInfo::new()
.get(ApiMethod {
2018-11-10 11:06:39 +00:00
handler: test_sync_api_handler,
async_handler: test_async_api_handler,
2018-11-03 14:10:21 +00:00
description: "This is a simple test.",
parameters: parameter!{
force => Boolean!{
2018-11-06 12:10:10 +00:00
optional => true,
2018-11-03 14:10:21 +00:00
description => "Test for boolean options."
}
},
returns: Jss::Null,
});
2018-11-03 09:42:48 +00:00
2018-11-03 14:10:21 +00:00
route
2018-11-03 09:42:48 +00:00
}