diff --git a/src/api/health.rs b/src/api/health.rs index 808f772..833a992 100644 --- a/src/api/health.rs +++ b/src/api/health.rs @@ -1,14 +1,27 @@ -use serde::Serialize; +use std::sync::Arc; +use serde::{Deserialize, Serialize}; +use crate::repo::pokemon::Repository; #[derive(Serialize)] struct Response{ message: String, } +#[derive(Deserialize)] +struct Request { + number: u16, + name: String, + types: Vec, +} - -pub fn serve() -> rouille::Response { +pub fn serve(repo: Arc, req: &rouille::Request) -> rouille::Response { // rouille::Response::text("Got you!") - rouille::Response::json(&Response{ - message: "Got me!".to_string(), - }) + // rouille::Response::json(&Response{ + // message: "Got me!".to_string(), + // }) + match rouille::input::json_input::(req) { + Ok(_) => rouille::Response::json(&Response{ + message: "Got json".to_string() + }), + _ => return rouille::Response::from(rouille::Response::empty_400()) + } } \ No newline at end of file diff --git a/src/api/mod.rs b/src/api/mod.rs index 049c8d8..fa3ecaa 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,13 +1,15 @@ +use std::sync::Arc; use rouille::router; +use crate::repo::pokemon::Repository; mod health; -pub fn serve(url: &str) { +pub fn serve(url: &str, repo: Arc) { rouille::start_server(url, move |req| { router!(req, - (GET)(/health) => { - health::serve() + (POST)(/health) => { + health::serve(repo.clone(), req) }, _ => { rouille::Response::from(rouille::Response::empty_404()) diff --git a/src/main.rs b/src/main.rs index da656a7..134aefa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,10 @@ mod api; #[macro_use] extern crate rouille; +use std::sync::Arc; +use crate::repo::pokemon::InMemoryRepository; + fn main() { - api::serve("localhost:8000"); + let repo = Arc::new(InMemoryRepository::new()); + api::serve("localhost:8000", repo); } diff --git a/src/repo/pokemon.rs b/src/repo/pokemon.rs index 4175636..c9e80ad 100644 --- a/src/repo/pokemon.rs +++ b/src/repo/pokemon.rs @@ -1,6 +1,6 @@ use crate::domain::entity::{Pokemon, PokemonName, PokemonNumber, PokemonTypes}; -pub trait Repository { +pub trait Repository: Send + Sync { fn insert(&mut self, number: PokemonNumber, name: PokemonName, types: PokemonTypes) -> Insert; } #[derive(Debug)]