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

View File

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

View File

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