]>
Commit | Line | Data |
---|---|---|
763220ce | 1 | extern crate apitest; |
d8d978eb | 2 | |
6d77fb40 | 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 | 9 | use apitest::api_info::*; |
6d77fb40 | 10 | use apitest::json_schema::*; |
886e5ce8 | 11 | |
504b3597 | 12 | //use serde_derive::{Serialize, Deserialize}; |
22f0adf2 DM |
13 | use serde_json::{json, Value}; |
14 | ||
28e47cea | 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 | ||
28e47cea DM |
28 | |
29 | fn handle_request(req: Request<Body>) -> Response<Body> { | |
886e5ce8 DM |
30 | |
31 | let method = req.method(); | |
32 | let path = req.uri().path(); | |
28e47cea DM |
33 | let query = req.uri().query(); |
34 | let components: Vec<&str> = path.split('/').filter(|x| !x.is_empty()).collect(); | |
35 | let comp_len = components.len(); | |
886e5ce8 DM |
36 | |
37 | println!("REQUEST {} {}", method, path); | |
28e47cea DM |
38 | println!("COMPO {:?}", components); |
39 | ||
40 | if comp_len >= 1 && components[0] == "api3" { | |
41 | println!("GOT API REQUEST"); | |
42 | if comp_len >= 2 { | |
43 | let format = components[1]; | |
44 | if format != "json" { | |
45 | http_error!(NOT_FOUND, format!("Unsupported format '{}'\n", format)) | |
46 | } | |
47 | ||
0dde2f04 | 48 | if let Some(info) = ROUTER.find_method(&components[2..]) { |
28e47cea DM |
49 | println!("FOUND INFO"); |
50 | let api_method_opt = match method { | |
0dde2f04 | 51 | &Method::GET => &info.get, |
6d77fb40 DM |
52 | &Method::PUT => &info.put, |
53 | &Method::POST => &info.post, | |
54 | &Method::DELETE => &info.delete, | |
0dde2f04 | 55 | _ => &None, |
28e47cea | 56 | }; |
bfcba4fd | 57 | let api_method = match api_method_opt { |
28e47cea DM |
58 | Some(m) => m, |
59 | _ => http_error!(NOT_FOUND, format!("No such method '{} {}'\n", method, path)), | |
60 | }; | |
61 | ||
62 | // handle auth | |
63 | ||
64 | // extract param | |
bfcba4fd | 65 | let param = match query { |
6d77fb40 | 66 | Some(data) => { |
ab9e6de2 | 67 | match parse_query_string(data, &api_method.parameters, true) { |
6d77fb40 | 68 | Ok(query) => query, |
cfa5886c DM |
69 | Err(ref error_list) => { |
70 | let mut msg = String::from(""); | |
71 | for item in error_list { | |
72 | msg = msg + &item.to_string() + "\n"; | |
73 | } | |
74 | http_error!(BAD_REQUEST, msg); | |
75 | } | |
6d77fb40 DM |
76 | } |
77 | } | |
28e47cea DM |
78 | None => json!({}), |
79 | }; | |
80 | ||
e72677bf | 81 | match (api_method.handler)(param, &api_method) { |
bfcba4fd DM |
82 | Ok(res) => { |
83 | let json_str = res.to_string(); | |
84 | return Response::new(Body::from(json_str)); | |
85 | } | |
86 | Err(err) => { | |
87 | http_error!(NOT_FOUND, format!("Method returned error '{}'\n", err)); | |
88 | } | |
89 | } | |
90 | ||
28e47cea DM |
91 | } else { |
92 | http_error!(NOT_FOUND, format!("No such path '{}'\n", path)); | |
93 | } | |
94 | } | |
95 | } | |
886e5ce8 | 96 | |
28e47cea | 97 | Response::new(Body::from("RETURN WEB GUI\n")) |
886e5ce8 DM |
98 | } |
99 | ||
504b3597 | 100 | lazy_static!{ |
0dde2f04 | 101 | static ref ROUTER: MethodInfo = apitest::api3::router(); |
504b3597 DM |
102 | } |
103 | ||
d8d978eb DM |
104 | fn main() { |
105 | println!("Fast Static Type Definitions 1"); | |
106 | ||
886e5ce8 DM |
107 | let addr = ([127, 0, 0, 1], 8007).into(); |
108 | ||
109 | let new_svc = || { | |
110 | // service_fn_ok converts our function into a `Service` | |
28e47cea | 111 | service_fn_ok(handle_request) |
886e5ce8 DM |
112 | }; |
113 | ||
114 | let server = Server::bind(&addr) | |
115 | .serve(new_svc) | |
116 | .map_err(|e| eprintln!("server error: {}", e)); | |
117 | ||
118 | // Run this server for... forever! | |
119 | hyper::rt::run(server); | |
d8d978eb | 120 | } |