Implemented request parsing to struct example

This commit is contained in:
someone 2024-04-18 10:21:03 +02:00
parent ec63deb6eb
commit d6b206f494
3 changed files with 29 additions and 7 deletions

2
Cargo.lock generated
View file

@ -807,6 +807,8 @@ dependencies = [
"axum", "axum",
"env_logger", "env_logger",
"log", "log",
"serde",
"serde_json",
"tokio", "tokio",
"utoipa", "utoipa",
] ]

View file

@ -14,4 +14,6 @@ log = { workspace = true }
env_logger = { workspace = true } env_logger = { workspace = true }
tokio = { workspace = true, features=["full"] } tokio = { workspace = true, features=["full"] }
axum = { workspace = true } axum = { workspace = true }
utoipa = { version = "4", features = ["axum_extras"] } utoipa = { version = "4", features = ["axum_extras"] }
serde = "1.0.197"
serde_json = "1.0.1"

View file

@ -1,6 +1,8 @@
use axum::{extract::Request, routing::{get, post}, Router}; use axum::{extract::Request, routing::{get, post}, Router};
use log; use log;
use std::env; use std::env;
use serde::Deserialize;
use serde_json;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -12,6 +14,7 @@ async fn main() {
.route("/", get(root)) .route("/", get(root))
.route("/v1/secret/data/foo", post(foo)) .route("/v1/secret/data/foo", post(foo))
.route_service("/v1/secret/data/bar", post(bar)) .route_service("/v1/secret/data/bar", post(bar))
.route("/v1/kw_mount_path/data/foo", post(baz))
.fallback(fallback); .fallback(fallback);
@ -20,21 +23,36 @@ async fn main() {
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
#[derive(Deserialize,Debug)]
struct CreateSecret {
password1: String,
password2: String,
}
/// Routing handler for path "/v1/kw_mount_path/data/foo"
/// expects payload as JSON, prints payload into struct
async fn baz(body: String) -> String{
let mut body_json = serde_json::from_str::<serde_json::Value>(body.as_str()).unwrap();
let secret: CreateSecret = CreateSecret{password1: body_json["data"]["password1"].take().to_string(), password2: body_json["data"]["password2"].take().to_string()};
log::debug!("Pass1: {}, Pass2: {}", secret.password1, secret.password2);
String::from("RoutingTest baz successful")
}
/// Test function foo for routing /// Test function foo for routing
async fn foo(req: Request) -> String { /// Returns body of request
log::debug!("`{:?}`", req); async fn foo(body: String) -> String {
String::from("RoutingTest foo successful") log::debug!("{:?}", body.as_str());
String::from("RoutingTest foo successful")
} }
/// Test function bar for routing /// Test function bar for routing
async fn bar(req: Request)-> String { async fn bar(req: Request)-> String {
log::debug!("`{:?}`", req); log::debug!("{:?}", req);
String::from("RoutingTest bar successful") String::from("RoutingTest bar successful")
} }
async fn fallback(req: Request)-> String { async fn fallback(req: Request)-> String {
log::debug!("`{:?}`", req); log::debug!("{:?}", req);
String::from("Fallback triggered") String::from("Fallback triggered")
} }