From a9189dc052f0c4c1595c87901b1f210ef2ea178c Mon Sep 17 00:00:00 2001 From: C0ffeeCode Date: Mon, 15 Apr 2024 16:08:38 +0200 Subject: [PATCH] Basic routers for components --- Justfile | 26 +++++++++++++++ crates/server/src/auth.rs | 10 +++--- crates/server/src/identity.rs | 5 +++ crates/server/src/main.rs | 59 ++++++++++++++++++----------------- crates/server/src/secrets.rs | 30 ++++++++++++++++++ crates/server/src/sys.rs | 6 ++++ 6 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 Justfile diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..f307acd --- /dev/null +++ b/Justfile @@ -0,0 +1,26 @@ + +build_tests: + podman build -t rvault-go-tests -f Containerfile ./go_client + +run_tests: build_tests + podman run --rm -it --net=host rvault-go-tests + +build_server: + cargo build + +start_server: build_server + cargo run + +# watch_server: +# cargo watch -x run + +# test_server: build_server build_tests +# just start_server & sleep 1 && podman run --rm -it --net=host rvault-go-tests + +check: + cargo fmt --check + cargo clippy --all-targets --all-features + cargo test + +kill_junk: + fuser -k 8200/tcp diff --git a/crates/server/src/auth.rs b/crates/server/src/auth.rs index 7dfb82a..038e3ae 100644 --- a/crates/server/src/auth.rs +++ b/crates/server/src/auth.rs @@ -1,10 +1,12 @@ use axum::Router; -use self::token::token_auth_router; - // route prefix: `/auth/token/` -mod token; +// mod token; + +// use self::token::token_auth_router; + pub fn auth_router() -> Router { - Router::new().nest("/token", token_auth_router()) + Router::new() + // .nest("/token", token_auth_router()) } diff --git a/crates/server/src/identity.rs b/crates/server/src/identity.rs index 8b13789..83b91d1 100644 --- a/crates/server/src/identity.rs +++ b/crates/server/src/identity.rs @@ -1 +1,6 @@ +use axum::Router; + +pub fn identity_router() -> Router { + Router::new() +} diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 7cd9b58..a4c21c3 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -1,45 +1,48 @@ -use axum::{extract::Request, routing::{get, post}, Router}; -use log; -use std::env; +use axum::{extract::Request, http::StatusCode, routing::get, Router}; +use log::{info, warn}; +use tokio::net::TcpListener; +use std::{env, net::SocketAddr, str::FromStr}; + +mod auth; +mod identity; +mod secrets; +mod sys; #[tokio::main] async fn main() { env::set_var("RUST_LOG", "trace"); 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)) - .route("/v1/secret/data/foo", post(foo)) - .route_service("/v1/secret/data/bar", post(bar)) - .fallback(fallback); - + .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 + .fallback(fallback_route_unknown); - // run our app with hyper, listening globally on port 8200 - let listener = tokio::net::TcpListener::bind("127.0.0.1:8200").await.unwrap(); + warn!("Listening on: {}", listen_addr.to_string()); + + // Start listening + let listener = TcpListener::bind(listen_addr).await.unwrap(); axum::serve(listener, app).await.unwrap(); } -/// Test function foo for routing -async fn foo(req: Request) -> String { - log::debug!("`{:?}`", req); - String::from("RoutingTest foo 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") } -/// Test function bar for routing -async fn bar(req: Request)-> String { - log::debug!("`{:?}`", req); - String::from("RoutingTest bar successful") -} - -async fn fallback(req: Request)-> String { - log::debug!("`{:?}`", req); - String::from("Fallback triggered") -} - -// basic handler that responds with a static string +/// basic handler that responds with a static string async fn root() -> &'static str { - log::info!("Hello world"); + info!("Hello world"); "Hello, World!" } diff --git a/crates/server/src/secrets.rs b/crates/server/src/secrets.rs index 8b13789..8d65ddb 100644 --- a/crates/server/src/secrets.rs +++ b/crates/server/src/secrets.rs @@ -1 +1,31 @@ +use axum::{body::Body, http::Request, routing::*, Router}; +pub fn secrets_router() -> Router { + Router::new() + .fallback_service(any(handler)) +} + +// async fn handler(Host(hostname): Host, request: Request) -> &'static str { +async fn handler(request: Request) -> &'static str { + let path: Vec<&str> = request.uri().path().split('/').collect(); + log::info!("path, {:?}", path[1]); + + match path[1] { + "test" => { + log::info!("test route"); + // TODO: Nest another Router here + + } + _ => { + log::info!("default"); + } + } + + "Hello, World!" +} + +// basic handler that responds with a static string +async fn tada() -> &'static str { + log::info!("Hello world"); + "Hello, World!" +} diff --git a/crates/server/src/sys.rs b/crates/server/src/sys.rs index 8b13789..70f846b 100644 --- a/crates/server/src/sys.rs +++ b/crates/server/src/sys.rs @@ -1 +1,7 @@ +use axum::Router; + + +pub fn sys_router() -> Router { + Router::new() +}