51 lines
1.2 KiB
Rust
51 lines
1.2 KiB
Rust
use std::sync::Arc;
|
|
use rouille::router;
|
|
use crate::repo::pokemon::Repository;
|
|
|
|
mod health;
|
|
mod create_pokemon;
|
|
mod fetch_all_pokemons;
|
|
|
|
|
|
pub fn serve(url: &str, repo: Arc<dyn Repository>) {
|
|
rouille::start_server(url, move |req| {
|
|
router!(req,
|
|
(POST)(/health) => {
|
|
health::serve()
|
|
},
|
|
(POST)(/) => {
|
|
create_pokemon::serve(repo.clone(), req)
|
|
},
|
|
(GET)(/) => {
|
|
fetch_all_pokemons::serve(repo.clone())
|
|
},
|
|
_ => {
|
|
rouille::Response::from(rouille::Response::empty_404())
|
|
}
|
|
)
|
|
});
|
|
}
|
|
|
|
enum Status {
|
|
BadRequest,
|
|
NotFound,
|
|
Conflict,
|
|
InternalServerError,
|
|
}
|
|
|
|
impl From<Status> for rouille::Response {
|
|
fn from(value: Status) -> Self {
|
|
let status_code = match value {
|
|
Status::BadRequest => 400,
|
|
Status::NotFound => 404,
|
|
Status::Conflict => 409,
|
|
Status::InternalServerError => 500,
|
|
};
|
|
Self {
|
|
status_code,
|
|
headers: vec![],
|
|
data: rouille::ResponseBody::empty(),
|
|
upgrade: None,
|
|
}
|
|
}
|
|
} |