Skip to content
Snippets Groups Projects
Commit 835ca668 authored by Michael Hauspie's avatar Michael Hauspie
Browse files

Now listens to all ipv4 adresses

parent 379b1b7b
No related branches found
No related tags found
No related merge requests found
use rocket::{get, launch, post, routes, serde::json::Json}; use rocket::{get, http::uri::Host, launch, post, routes, serde::json::Json};
const INDEX_TEXT: &str = r#"\ const INDEX_TEXT: &str = r#"\
Cette application permet d'executer des calculs simple. Cette application permet d'executer des calculs simple.
...@@ -24,17 +24,25 @@ Exemple: ...@@ -24,17 +24,25 @@ Exemple:
"resultat": 7, "resultat": 7,
} }
Les opérations possibles sont:
- "Plus"
- "Moins"
- "Fois"
- "Diviser"
Pour envoyer une requête avec curl: Pour envoyer une requête avec curl:
curl -X POST http://monurl -H 'Content-Type: application/json' -d '{"Plus", [3, 4]}' curl -X POST http://<host> -H 'Content-Type: application/json' -d '{"Plus": [3, 4]}'
"#; "#;
mod operations; mod operations;
use operations::{Operation, OperationResult}; use operations::{Operation, OperationResult};
#[get("/")] #[get("/")]
fn index() -> &'static str { fn index(host: &Host) -> String {
INDEX_TEXT println!("{host}");
INDEX_TEXT.replace("<host>", &host.to_string())
} }
#[post("/", data = "<operation>")] #[post("/", data = "<operation>")]
...@@ -44,29 +52,34 @@ fn do_calc(operation: Json<Operation>) -> Json<OperationResult> { ...@@ -44,29 +52,34 @@ fn do_calc(operation: Json<Operation>) -> Json<OperationResult> {
#[get("/test")] #[get("/test")]
fn test() -> Json<Operation> { fn test() -> Json<Operation> {
let t = Operation::Plus(1, 3); let t = Operation::Plus(1, 3);
t.into() t.into()
} }
#[launch] #[launch]
fn launch() -> _ { fn launch() -> _ {
rocket::build().mount("/", routes![index, do_calc, test]) let figment = rocket::Config::figment().merge(("address", "0.0.0.0"));
rocket::custom(figment).mount("/", routes![index, do_calc, test])
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use rocket::local::blocking::Client; use super::*;
use rocket::http::Status; use rocket::http::Status;
use rocket::local::blocking::Client;
use rocket::uri; use rocket::uri;
use super::*;
fn build_response<'r>(operation: Operation) -> (Status, OperationResult) { fn build_response<'r>(operation: Operation) -> (Status, OperationResult) {
let client = Client::tracked(launch()).expect("valid rocket instance"); let client = Client::tracked(launch()).expect("valid rocket instance");
let req = client.post(uri!(super::index)).json(&operation); let req = client.post(uri!(super::index)).json(&operation);
let response = req.dispatch(); let response = req.dispatch();
(response.status(), response.into_json::<OperationResult>().unwrap()) (
response.status(),
response.into_json::<OperationResult>().unwrap(),
)
} }
#[test] #[test]
fn index() { fn index() {
let client = Client::tracked(launch()).expect("valid rocket instance"); let client = Client::tracked(launch()).expect("valid rocket instance");
...@@ -82,7 +95,7 @@ mod tests { ...@@ -82,7 +95,7 @@ mod tests {
assert_eq!(status, Status::Ok); assert_eq!(status, Status::Ok);
let expected = OperationResult::new("3 + 8", 11); let expected = OperationResult::new("3 + 8", 11);
assert_eq!(response, expected); assert_eq!(response, expected);
} }
#[test] #[test]
...@@ -91,7 +104,7 @@ mod tests { ...@@ -91,7 +104,7 @@ mod tests {
assert_eq!(status, Status::Ok); assert_eq!(status, Status::Ok);
let expected = OperationResult::new("10 - 20", -10); let expected = OperationResult::new("10 - 20", -10);
assert_eq!(response, expected); assert_eq!(response, expected);
} }
#[test] #[test]
...@@ -100,7 +113,7 @@ mod tests { ...@@ -100,7 +113,7 @@ mod tests {
assert_eq!(status, Status::Ok); assert_eq!(status, Status::Ok);
let expected = OperationResult::new("10 x 20", 200); let expected = OperationResult::new("10 x 20", 200);
assert_eq!(response, expected); assert_eq!(response, expected);
} }
#[test] #[test]
...@@ -109,7 +122,6 @@ mod tests { ...@@ -109,7 +122,6 @@ mod tests {
assert_eq!(status, Status::Ok); assert_eq!(status, Status::Ok);
let expected = OperationResult::new("10 / 20", 0); let expected = OperationResult::new("10 / 20", 0);
assert_eq!(response, expected); assert_eq!(response, expected);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment