2022-05-12 22:32:51 +03:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::thread;
|
|
|
|
use tiny_http::{Response, Server};
|
|
|
|
|
|
|
|
use crate::rest;
|
|
|
|
use crate::rest::types::Url;
|
|
|
|
|
|
|
|
pub fn start() {
|
|
|
|
let server = Arc::new(Server::http("0.0.0.0:33333").unwrap());
|
|
|
|
println!("Server listening on http://localhost:33333");
|
|
|
|
|
|
|
|
let mut handles = Vec::with_capacity(4);
|
|
|
|
|
|
|
|
for _ in 0..4 {
|
|
|
|
let server = server.clone();
|
|
|
|
|
|
|
|
handles.push(thread::spawn(move || {
|
|
|
|
for rq in server.incoming_requests() {
|
|
|
|
let url = Url::parse(rq.url());
|
2022-05-15 15:50:09 +03:00
|
|
|
let ctx = rest::Context::from(url.query_params());
|
|
|
|
let _ = match url.path_segments() {
|
2022-05-12 22:32:51 +03:00
|
|
|
["api", "ingredients"] => {
|
2022-05-15 15:50:09 +03:00
|
|
|
let res = rest::ctrl::ingredient::fetch_list(&ctx, &url);
|
2022-05-12 22:32:51 +03:00
|
|
|
rq.respond(res)
|
|
|
|
}
|
2022-05-13 19:33:08 +03:00
|
|
|
["api", "ingredients", key] => {
|
2022-05-15 15:50:09 +03:00
|
|
|
let res = rest::ctrl::ingredient::fetch_by_key(&ctx, &url, key);
|
2022-05-13 19:33:08 +03:00
|
|
|
rq.respond(res)
|
|
|
|
}
|
2022-05-14 00:45:31 +03:00
|
|
|
["api", "recipes"] => {
|
2022-05-15 15:50:09 +03:00
|
|
|
let res = rest::ctrl::recipe::fetch_list(&ctx, &url);
|
2022-05-14 00:45:31 +03:00
|
|
|
rq.respond(res)
|
|
|
|
}
|
2022-05-12 22:32:51 +03:00
|
|
|
_ => rq.respond(Response::from_string("Not found")),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
for h in handles {
|
|
|
|
h.join().unwrap();
|
|
|
|
}
|
|
|
|
}
|