recipes/api/src/rest/server.rs

39 lines
1.1 KiB
Rust

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