summaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud.bailly@iohk.io>2024-10-03 09:45:45 +0200
committerArnaud Bailly <arnaud.bailly@iohk.io>2024-10-03 09:45:45 +0200
commit5d68c51f98faea7a4b16c25741d5a637305687be (patch)
tree0084f3e8dd6db6b7fa1fd24e979dd517e1bd9ff7 /rust/src
parentcd3c54945d8bff57b121a2f834d165314e0f0738 (diff)
downloadlambda-nantes-5d68c51f98faea7a4b16c25741d5a637305687be.tar.gz
Introduce web app using actix-web
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/web.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/rust/src/web.rs b/rust/src/web.rs
new file mode 100644
index 0000000..94ed282
--- /dev/null
+++ b/rust/src/web.rs
@@ -0,0 +1,52 @@
+use std::{
+ env::args,
+ io::{stdin, stdout, IsTerminal},
+};
+
+use lambda::io::{batch_eval, eval_file, repl};
+
+use actix_web::{
+ get, http::header::ContentType, post, web, App, HttpResponse, HttpServer, Responder,
+};
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Serialize, Deserialize)]
+struct Registration {
+ url: String,
+}
+
+#[post("/register")]
+async fn register(registration: web::Json<Registration>) -> impl Responder {
+ HttpResponse::Ok()
+ .content_type(ContentType::json())
+ .body(serde_json::to_string(&registration).unwrap())
+}
+
+#[actix_web::main]
+async fn main() -> std::io::Result<()> {
+ HttpServer::new(|| App::new().service(register))
+ .bind(("127.0.0.1", 8080))?
+ .run()
+ .await
+}
+
+#[cfg(test)]
+mod app_tests {
+ use actix_web::{http::header::ContentType, test, App};
+
+ use super::*;
+
+ #[actix_web::test]
+ async fn post_registration_echoes_back_registration_data() {
+ let app = test::init_service(App::new().service(register)).await;
+ let req = test::TestRequest::post()
+ .uri("/register")
+ .set_json(Registration {
+ url: "http://192.168.1.1".to_string(),
+ })
+ .insert_header(ContentType::json())
+ .to_request();
+ let resp = test::call_service(&app, req).await;
+ assert!(resp.status().is_success());
+ }
+}