Basic routers for components

This commit is contained in:
Laurenz 2024-04-15 16:08:38 +02:00
parent ec63deb6eb
commit a9189dc052
6 changed files with 104 additions and 32 deletions

26
Justfile Normal file
View file

@ -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

View file

@ -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())
}

View file

@ -1 +1,6 @@
use axum::Router;
pub fn identity_router() -> Router {
Router::new()
}

View file

@ -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);
warn!("Listening on: {}", listen_addr.to_string());
// run our app with hyper, listening globally on port 8200
let listener = tokio::net::TcpListener::bind("127.0.0.1:8200").await.unwrap();
// 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!"
}

View file

@ -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<Body>) -> &'static str {
async fn handler(request: Request<Body>) -> &'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!"
}

View file

@ -1 +1,7 @@
use axum::Router;
pub fn sys_router() -> Router {
Router::new()
}