use axum::{ extract::Request, http::StatusCode, routing::{get,post, trace}, Router }; use log::*; use std::{env, net::SocketAddr, str::FromStr}; use tokio::net::TcpListener; mod auth; mod identity; mod secrets; mod sys; use serde::Deserialize; 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"); // build our application with routes let app = Router::new() .route("/", get(root)) .nest("/v1/auth", auth::auth_router()) .nest("/v1/identity", identity::identity_router()) .nest("/v1/sys", sys::sys_router()) .nest("/v1", secrets::secrets_router()) // mountable secret backends .route("/v1/kv-v2/data/foo", post(baz)) .fallback(fallback_route_unknown); warn!("Listening on: {}", listen_addr.to_string()); // Start listening let listener = TcpListener::bind(listen_addr).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 = 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") } async fn fallback_route_unknown(req: Request) -> (StatusCode, &'static str) { log::error!( "Route not found: {} {}, payload {:?}", req.method(), req.uri(), req.body() ); (StatusCode::NOT_FOUND, "Route not implemented") } /// basic handler that responds with a static string async fn root() -> &'static str { info!("Hello world"); "Hello, World!" }