recipes/api/src/server.rs

32 lines
870 B
Rust

use std::sync::Arc;
use std::thread;
use crate::domain;
use crate::repo::ingredient::StaticIngredientRepo;
pub fn start() {
let server = Arc::new(tiny_http::Server::http("0.0.0.0:33333").unwrap());
println!("Server listening on port 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 repo = StaticIngredientRepo {};
let ingredients = domain::ingredient::fetch_list::execute(&repo);
let data = serde_json::to_string(&ingredients).unwrap();
let response = tiny_http::Response::from_string(data);
let _ = rq.respond(response);
}
}));
}
for h in handles {
h.join().unwrap();
}
}