]>
Commit | Line | Data |
---|---|---|
763220ce | 1 | extern crate apitest; |
d8d978eb | 2 | |
504b3597 | 3 | //use failure::*; |
d11f14f7 | 4 | |
28e47cea | 5 | use std::collections::HashMap; |
504b3597 | 6 | use lazy_static::lazy_static; |
28e47cea | 7 | |
504b3597 | 8 | //use apitest::json_schema::*; |
886e5ce8 DM |
9 | use apitest::api_info::*; |
10 | ||
504b3597 | 11 | //use serde_derive::{Serialize, Deserialize}; |
22f0adf2 DM |
12 | use serde_json::{json, Value}; |
13 | ||
28e47cea DM |
14 | use url::form_urlencoded; |
15 | ||
28e47cea | 16 | use hyper::{Method, Body, Request, Response, Server, StatusCode}; |
886e5ce8 DM |
17 | use hyper::rt::Future; |
18 | use hyper::service::service_fn_ok; | |
d8d978eb | 19 | |
28e47cea DM |
20 | macro_rules! http_error { |
21 | ($status:ident, $msg:expr) => {{ | |
22 | let mut resp = Response::new(Body::from($msg)); | |
23 | *resp.status_mut() = StatusCode::$status; | |
24 | return resp; | |
25 | }} | |
26 | } | |
27 | ||
28 | fn parse_query(query: &str) -> Value { | |
29 | ||
30 | println!("PARSE QUERY {}", query); | |
886e5ce8 | 31 | |
28e47cea DM |
32 | // fixme: what about repeated parameters (arrays?) |
33 | let mut raw_param = HashMap::new(); | |
34 | for (k, v) in form_urlencoded::parse(query.as_bytes()) { | |
35 | println!("QUERY PARAM {} value {}", k, v); | |
36 | raw_param.insert(k, v); | |
37 | } | |
38 | println!("QUERY HASH {:?}", raw_param); | |
39 | ||
40 | return json!(null); | |
41 | } | |
42 | ||
43 | fn handle_request(req: Request<Body>) -> Response<Body> { | |
886e5ce8 DM |
44 | |
45 | let method = req.method(); | |
46 | let path = req.uri().path(); | |
28e47cea DM |
47 | let query = req.uri().query(); |
48 | let components: Vec<&str> = path.split('/').filter(|x| !x.is_empty()).collect(); | |
49 | let comp_len = components.len(); | |
886e5ce8 DM |
50 | |
51 | println!("REQUEST {} {}", method, path); | |
28e47cea DM |
52 | println!("COMPO {:?}", components); |
53 | ||
54 | if comp_len >= 1 && components[0] == "api3" { | |
55 | println!("GOT API REQUEST"); | |
56 | if comp_len >= 2 { | |
57 | let format = components[1]; | |
58 | if format != "json" { | |
59 | http_error!(NOT_FOUND, format!("Unsupported format '{}'\n", format)) | |
60 | } | |
61 | ||
18671ca5 | 62 | if let Some(info) = API_ROOT.find_method(&components[2..]) { |
28e47cea DM |
63 | println!("FOUND INFO"); |
64 | let api_method_opt = match method { | |
65 | &Method::GET => info.get, | |
66 | &Method::PUT => info.put, | |
67 | &Method::POST => info.post, | |
68 | &Method::DELETE => info.delete, | |
69 | _ => None, | |
70 | }; | |
bfcba4fd | 71 | let api_method = match api_method_opt { |
28e47cea DM |
72 | Some(m) => m, |
73 | _ => http_error!(NOT_FOUND, format!("No such method '{} {}'\n", method, path)), | |
74 | }; | |
75 | ||
76 | // handle auth | |
77 | ||
78 | // extract param | |
bfcba4fd | 79 | let param = match query { |
28e47cea DM |
80 | Some(data) => parse_query(data), |
81 | None => json!({}), | |
82 | }; | |
83 | ||
bfcba4fd DM |
84 | match (api_method.handler)(param) { |
85 | Ok(res) => { | |
86 | let json_str = res.to_string(); | |
87 | return Response::new(Body::from(json_str)); | |
88 | } | |
89 | Err(err) => { | |
90 | http_error!(NOT_FOUND, format!("Method returned error '{}'\n", err)); | |
91 | } | |
92 | } | |
93 | ||
28e47cea DM |
94 | } else { |
95 | http_error!(NOT_FOUND, format!("No such path '{}'\n", path)); | |
96 | } | |
97 | } | |
98 | } | |
886e5ce8 | 99 | |
28e47cea | 100 | Response::new(Body::from("RETURN WEB GUI\n")) |
886e5ce8 DM |
101 | } |
102 | ||
504b3597 DM |
103 | lazy_static!{ |
104 | static ref API_ROOT: MethodInfo = apitest::api3::get_api_definition(); | |
105 | } | |
106 | ||
d8d978eb DM |
107 | fn main() { |
108 | println!("Fast Static Type Definitions 1"); | |
109 | ||
886e5ce8 DM |
110 | let addr = ([127, 0, 0, 1], 8007).into(); |
111 | ||
112 | let new_svc = || { | |
113 | // service_fn_ok converts our function into a `Service` | |
28e47cea | 114 | service_fn_ok(handle_request) |
886e5ce8 DM |
115 | }; |
116 | ||
117 | let server = Server::bind(&addr) | |
118 | .serve(new_svc) | |
119 | .map_err(|e| eprintln!("server error: {}", e)); | |
120 | ||
121 | // Run this server for... forever! | |
122 | hyper::rt::run(server); | |
d8d978eb | 123 | } |