This commit is contained in:
Jeremy Yin 2023-07-01 17:54:01 +08:00
parent 62e0e86546
commit fcc54cc0c9
4 changed files with 30 additions and 11 deletions

View File

@ -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,
}
pub fn serve() -> rouille::Response {
// rouille::Response::text("Got you!")
rouille::Response::json(&Response{
message: "Got me!".to_string(),
})
#[derive(Deserialize)]
struct Request {
number: u16,
name: String,
types: Vec<String>,
}
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(),
// })
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())
}
}

View File

@ -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())

View File

@ -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);
}

View File

@ -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)]