2018-11-01 13:42:27 +00:00
|
|
|
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-10-30 13:06:37 +00:00
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
use futures::future;
|
|
|
|
|
|
|
|
type BoxFut = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send>;
|
2018-10-30 09:04:30 +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;
|
2018-11-09 07:22:54 +00:00
|
|
|
return resp
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
macro_rules! http_error_future {
|
|
|
|
($status:ident, $msg:expr) => {{
|
|
|
|
let mut resp = Response::new(Body::from($msg));
|
|
|
|
*resp.status_mut() = StatusCode::$status;
|
|
|
|
return Box::new(futures::future::ok(resp));
|
2018-11-01 10:30:49 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2018-11-09 07:22:54 +00:00
|
|
|
fn handle_api_request<'a>(
|
2018-11-09 12:48:57 +00:00
|
|
|
info: &'a ApiMethod,
|
|
|
|
parts: Parts,
|
|
|
|
req_body: Body,
|
|
|
|
) -> Box<Future<Item = Response<Body>, Error = hyper::Error> + Send + 'a>
|
2018-11-09 07:22:54 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
let entire_body = req_body.concat2();
|
|
|
|
|
|
|
|
let resp = entire_body.map(move |body| {
|
|
|
|
let bytes = match String::from_utf8(body.to_vec()) { // why copy??
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(err) => http_error!(NOT_FOUND, err.to_string()),
|
|
|
|
};
|
|
|
|
|
2018-11-09 12:48:57 +00:00
|
|
|
println!("GOT BODY {:?}", bytes);
|
2018-11-09 07:22:54 +00:00
|
|
|
|
2018-11-09 12:48:57 +00:00
|
|
|
let mut test_required = true;
|
|
|
|
|
|
|
|
let mut params = json!({});
|
|
|
|
|
|
|
|
let format_error_list = |error_list: Vec<Error>| {
|
|
|
|
error_list.iter().fold(String::from(""), |acc, item| {
|
|
|
|
acc + &item.to_string() + "\n"
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if bytes.len() > 0 {
|
|
|
|
params = match parse_query_string(&bytes, &info.parameters, true) {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(error_list) => http_error!(NOT_FOUND, format_error_list(error_list)),
|
|
|
|
};
|
|
|
|
test_required = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(query_str) = parts.uri.query() {
|
|
|
|
let query_params = match parse_query_string(query_str, &info.parameters, test_required) {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(error_list) => http_error!(NOT_FOUND, format_error_list(error_list)),
|
|
|
|
};
|
|
|
|
|
|
|
|
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-09 12:48:57 +00:00
|
|
|
println!("GOT PARAMS {}", params);
|
|
|
|
|
|
|
|
let res: Value = match (info.handler)(params, info) {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(err) => http_error!(NOT_FOUND, format!("Method returned error '{}'\n", err)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let json_str = res.to_string();
|
|
|
|
|
|
|
|
Response::builder()
|
|
|
|
.status(200)
|
|
|
|
.header("ContentType", "application/json")
|
|
|
|
.body(Body::from(json_str))
|
|
|
|
.unwrap() // fixme: really?
|
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-09 07:22:54 +00:00
|
|
|
http_error_future!(NOT_FOUND, format!("Unsupported format '{}'\n", 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-09 07:22:54 +00:00
|
|
|
_ => http_error_future!(NOT_FOUND, format!("No such method '{} {}'\n", method, path)),
|
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-09 07:22:54 +00:00
|
|
|
|
2018-11-09 12:48:57 +00:00
|
|
|
let res = handle_api_request(api_method, parts, body);
|
2018-11-09 07:22:54 +00:00
|
|
|
return res;
|
|
|
|
|
|
|
|
/*
|
2018-11-01 10:30:49 +00:00
|
|
|
// extract param
|
2018-11-01 14:41:08 +00:00
|
|
|
let param = match query {
|
2018-11-05 14:20:27 +00:00
|
|
|
Some(data) => {
|
2018-11-06 13:18:13 +00:00
|
|
|
match parse_query_string(data, &api_method.parameters, true) {
|
2018-11-05 14:20:27 +00:00
|
|
|
Ok(query) => query,
|
2018-11-06 12:10:10 +00:00
|
|
|
Err(ref error_list) => {
|
2018-11-07 12:58:09 +00:00
|
|
|
let msg = error_list.iter().fold(String::from(""), |acc, item| {
|
|
|
|
acc + &item.to_string() + "\n"
|
|
|
|
});
|
2018-11-09 07:22:54 +00:00
|
|
|
http_error_future!(BAD_REQUEST, msg);
|
2018-11-06 12:10:10 +00:00
|
|
|
}
|
2018-11-05 14:20:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 10:30:49 +00:00
|
|
|
None => json!({}),
|
|
|
|
};
|
|
|
|
|
2018-11-09 07:22:54 +00:00
|
|
|
|
|
|
|
/*if body.is_end_stream() {
|
|
|
|
println!("NO BODY");
|
|
|
|
}*/
|
|
|
|
|
2018-11-07 10:06:37 +00:00
|
|
|
match (api_method.handler)(param, &api_method) {
|
2018-11-01 14:41:08 +00:00
|
|
|
Ok(res) => {
|
|
|
|
let json_str = res.to_string();
|
|
|
|
return Response::new(Body::from(json_str));
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2018-11-09 07:22:54 +00:00
|
|
|
http_error_future!(NOT_FOUND, format!("Method returned error '{}'\n", err));
|
2018-11-01 14:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 07:22:54 +00:00
|
|
|
*/
|
2018-11-01 10:30:49 +00:00
|
|
|
} else {
|
2018-11-09 07:22:54 +00:00
|
|
|
http_error_future!(NOT_FOUND, format!("No such path '{}'\n", path));
|
2018-11-01 10:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 09:42:14 +00:00
|
|
|
|
2018-11-09 07:22:54 +00:00
|
|
|
Box::new(future::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 = || {
|
|
|
|
// service_fn_ok converts our function into a `Service`
|
2018-11-09 07:22:54 +00:00
|
|
|
service_fn(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
|
|
|
}
|