api: add all ingredients resolver

This commit is contained in:
Dmitriy Pleshevskiy 2022-05-09 17:52:22 +03:00
parent 988ef52d05
commit 39808b8bda
11 changed files with 99 additions and 26 deletions

20
Cargo.lock generated
View File

@ -6,6 +6,9 @@ version = 3
name = "api"
version = "0.1.0"
dependencies = [
"db",
"serde",
"serde_json",
"tiny_http",
]
@ -123,6 +126,12 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]]
name = "serde"
version = "1.0.137"
@ -143,6 +152,17 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.92"

View File

@ -6,4 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
db = { version = "0.1.0", path = "../db" }
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.81"
tiny_http = "0.11.0"

View File

@ -0,0 +1,6 @@
use super::types;
use crate::repo::ingredient::IngredientRepo;
pub fn execute(repo: impl IngredientRepo) -> Vec<types::Ingredient> {
repo.get_ingredients()
}

View File

@ -0,0 +1,2 @@
pub mod fetch_list;
pub mod types;

View File

@ -0,0 +1,12 @@
#[derive(Debug, Serialize)]
pub struct Ingredient {
key: String,
}
impl From<&db::data::Ingredient> for Ingredient {
fn from(db: &db::data::Ingredient) -> Self {
Self {
key: db.key.to_string(),
}
}
}

1
api/src/domain/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod ingredient;

View File

@ -1,28 +1,12 @@
use std::sync::Arc;
use std::thread;
#![deny(clippy::all)]
#[macro_use]
extern crate serde;
mod domain;
mod repo;
mod server;
fn main() {
start_server();
}
fn start_server() {
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 response = tiny_http::Response::from_string("hello world".to_string());
let _ = rq.respond(response);
}
}));
}
for h in handles {
h.join().unwrap();
}
server::start();
}

View File

@ -0,0 +1,13 @@
use crate::domain::ingredient::types;
pub trait IngredientRepo {
fn get_ingredients(&self) -> Vec<types::Ingredient>;
}
pub struct StaticIngredientRepo {}
impl IngredientRepo for StaticIngredientRepo {
fn get_ingredients(&self) -> Vec<types::Ingredient> {
db::INGREDIENTS.iter().map(From::from).collect::<Vec<_>>()
}
}

1
api/src/repo/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod ingredient;

31
api/src/server.rs Normal file
View File

@ -0,0 +1,31 @@
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();
}
}

View File

@ -1,3 +1,3 @@
mod data;
pub mod data;
pub use data::{INGREDIENTS, RECIPES};