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)]
|
#[derive(Serialize)]
|
||||||
struct Response{
|
struct Response{
|
||||||
message: String,
|
message: String,
|
||||||
}
|
}
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Request {
|
||||||
|
number: u16,
|
||||||
|
name: String,
|
||||||
|
types: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn serve(repo: Arc<dyn Repository>, req: &rouille::Request) -> rouille::Response {
|
||||||
pub fn serve() -> rouille::Response {
|
|
||||||
// rouille::Response::text("Got you!")
|
// rouille::Response::text("Got you!")
|
||||||
rouille::Response::json(&Response{
|
// rouille::Response::json(&Response{
|
||||||
message: "Got me!".to_string(),
|
// 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 rouille::router;
|
||||||
|
use crate::repo::pokemon::Repository;
|
||||||
|
|
||||||
mod health;
|
mod health;
|
||||||
|
|
||||||
|
|
||||||
pub fn serve(url: &str) {
|
pub fn serve(url: &str, repo: Arc<dyn Repository>) {
|
||||||
rouille::start_server(url, move |req| {
|
rouille::start_server(url, move |req| {
|
||||||
router!(req,
|
router!(req,
|
||||||
(GET)(/health) => {
|
(POST)(/health) => {
|
||||||
health::serve()
|
health::serve(repo.clone(), req)
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
rouille::Response::from(rouille::Response::empty_404())
|
rouille::Response::from(rouille::Response::empty_404())
|
||||||
|
|
|
@ -7,6 +7,10 @@ mod api;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rouille;
|
extern crate rouille;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use crate::repo::pokemon::InMemoryRepository;
|
||||||
|
|
||||||
fn main() {
|
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};
|
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;
|
fn insert(&mut self, number: PokemonNumber, name: PokemonName, types: PokemonTypes) -> Insert;
|
||||||
}
|
}
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
Loading…
Reference in New Issue