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()); let ctx = rest::Context::from(url.query_params()); let _ = match url.path_segments() { ["api", "ingredients"] => { let res = rest::ctrl::ingredient::fetch_list(&ctx, &url); rq.respond(res) } ["api", "ingredients", key] => { let res = rest::ctrl::ingredient::fetch_by_key(&ctx, &url, key); rq.respond(res) } ["api", "recipes"] => { let res = rest::ctrl::recipe::fetch_list(&ctx, &url); rq.respond(res) } _ => rq.respond(Response::from_string("Not found")), }; } })); } for h in handles { h.join().unwrap(); } }