repo
This commit is contained in:
parent
62e0e86546
commit
fcc54cc0c9
|
@ -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<String>,
|
||||
}
|
||||
|
||||
|
||||
pub fn serve() -> rouille::Response {
|
||||
pub fn serve(repo: Arc<dyn Repository>, 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::<Request>(req) {
|
||||
Ok(_) => rouille::Response::json(&Response{
|
||||
message: "Got json".to_string()
|
||||
}),
|
||||
_ => return rouille::Response::from(rouille::Response::empty_400())
|
||||
}
|
||||
}
|
|
@ -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<dyn Repository>) {
|
||||
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())
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Reference in New Issue