moved body-to-json-conversion to base
This commit is contained in:
parent
f91d396f69
commit
01b4d7f92a
5 changed files with 26 additions and 10 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -192,6 +192,7 @@ name = "base"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
|
@ -893,6 +894,7 @@ name = "server"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"base",
|
||||
"env_logger",
|
||||
"log",
|
||||
"serde",
|
||||
|
|
|
|||
|
|
@ -10,3 +10,4 @@ workspace = true
|
|||
chrono = { version = "0.4.38", features = ["serde"] }
|
||||
serde = { version = "1.0.198", features = ["derive"] }
|
||||
serde_json = "1.0.116"
|
||||
log = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use log::*;
|
||||
#[test]
|
||||
fn print_serialized_test() {
|
||||
let temp_secret = TempSecret {
|
||||
|
|
@ -36,6 +36,7 @@ pub fn create_mock_meta() -> SecretMeta {
|
|||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
|
@ -84,3 +85,15 @@ pub fn serialize_metadata_json(secret: &SecretMeta) -> Result<String, serde_json
|
|||
pub fn deserialize_metadata_struct(raw: &String) -> Result<SecretMeta, serde_json::Error> {
|
||||
serde_json::from_str(&raw)
|
||||
}
|
||||
|
||||
pub fn body_to_json(body: String) -> Value{
|
||||
|
||||
match serde_json::from_str::<serde_json::Value>(body.as_str()){
|
||||
Ok(val ) => {
|
||||
return val},
|
||||
Err(e) => {
|
||||
log::debug!("Faulty result from conversion: {:?}", e);
|
||||
return Into::into("Error converting body") }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,3 +18,4 @@ axum = { workspace = true }
|
|||
utoipa = { version = "4", features = ["axum_extras"] }
|
||||
serde = "1.0.197"
|
||||
serde_json = "1.0.1"
|
||||
base = { path = "../base" }
|
||||
|
|
@ -1,8 +1,5 @@
|
|||
use axum::{
|
||||
extract::Request,
|
||||
http::StatusCode,
|
||||
routing::{get,post, trace},
|
||||
Router,
|
||||
extract::Request, http::StatusCode, routing::{get,post, trace}, Router
|
||||
};
|
||||
use log::*;
|
||||
use std::{env, net::SocketAddr, str::FromStr};
|
||||
|
|
@ -13,13 +10,15 @@ mod identity;
|
|||
mod secrets;
|
||||
mod sys;
|
||||
use serde::Deserialize;
|
||||
use serde_json;
|
||||
use base::body_to_json;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// To be configured via environment variables
|
||||
env::set_var("RUST_LOG", "trace"); // Used to set level of logging -> choose from (highest to lowest): error, warn, info, debug, trace, off
|
||||
env_logger::init();
|
||||
|
||||
|
||||
// Listen on all IPv4 and IPv6 interfaces on port 8200
|
||||
let listen_addr = env::var("LISTEN_ADDR").unwrap_or("[::]:8200".to_string()); // Do not change
|
||||
let listen_addr = SocketAddr::from_str(&listen_addr).expect("Failed to parse LISTEN_ADDR");
|
||||
|
|
@ -31,7 +30,7 @@ async fn main() {
|
|||
.nest("/v1/identity", identity::identity_router())
|
||||
.nest("/v1/sys", sys::sys_router())
|
||||
.nest("/v1", secrets::secrets_router()) // mountable secret backends
|
||||
.route("/v1/kw_mount_path/data/foo", post(baz))
|
||||
.route("/v1/kv-v2/data/foo", post(baz))
|
||||
.fallback(fallback_route_unknown);
|
||||
|
||||
warn!("Listening on: {}", listen_addr.to_string());
|
||||
|
|
@ -49,7 +48,7 @@ struct CreateSecret {
|
|||
/// 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 mut body_json = body_to_json(body);
|
||||
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")
|
||||
|
|
|
|||
Loading…
Reference in a new issue