Basic routers for components
This commit is contained in:
parent
ec63deb6eb
commit
a9189dc052
6 changed files with 104 additions and 32 deletions
26
Justfile
Normal file
26
Justfile
Normal 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
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
|
||||||
use self::token::token_auth_router;
|
|
||||||
|
|
||||||
// route prefix: `/auth/token/`
|
// route prefix: `/auth/token/`
|
||||||
mod token;
|
// mod token;
|
||||||
|
|
||||||
|
// use self::token::token_auth_router;
|
||||||
|
|
||||||
|
|
||||||
pub fn auth_router() -> Router {
|
pub fn auth_router() -> Router {
|
||||||
Router::new().nest("/token", token_auth_router())
|
Router::new()
|
||||||
|
// .nest("/token", token_auth_router())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,6 @@
|
||||||
|
use axum::Router;
|
||||||
|
|
||||||
|
|
||||||
|
pub fn identity_router() -> Router {
|
||||||
|
Router::new()
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,45 +1,48 @@
|
||||||
use axum::{extract::Request, routing::{get, post}, Router};
|
use axum::{extract::Request, http::StatusCode, routing::get, Router};
|
||||||
use log;
|
use log::{info, warn};
|
||||||
use std::env;
|
use tokio::net::TcpListener;
|
||||||
|
use std::{env, net::SocketAddr, str::FromStr};
|
||||||
|
|
||||||
|
mod auth;
|
||||||
|
mod identity;
|
||||||
|
mod secrets;
|
||||||
|
mod sys;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
env::set_var("RUST_LOG", "trace");
|
env::set_var("RUST_LOG", "trace");
|
||||||
env_logger::init();
|
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
|
// build our application with routes
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(root))
|
.route("/", get(root))
|
||||||
.route("/v1/secret/data/foo", post(foo))
|
.nest("/v1/auth", auth::auth_router())
|
||||||
.route_service("/v1/secret/data/bar", post(bar))
|
.nest("/v1/identity", identity::identity_router())
|
||||||
.fallback(fallback);
|
.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
|
warn!("Listening on: {}", listen_addr.to_string());
|
||||||
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();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test function foo for routing
|
async fn fallback_route_unknown(req: Request) -> (StatusCode, &'static str) {
|
||||||
async fn foo(req: Request) -> String {
|
log::error!("Route not found: {} {}, payload {:?}", req.method(), req.uri(), req.body());
|
||||||
log::debug!("`{:?}`", req);
|
|
||||||
String::from("RoutingTest foo successful")
|
(StatusCode::NOT_FOUND, "Route not implemented")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test function bar for routing
|
/// basic handler that responds with a static string
|
||||||
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
|
|
||||||
async fn root() -> &'static str {
|
async fn root() -> &'static str {
|
||||||
log::info!("Hello world");
|
info!("Hello world");
|
||||||
"Hello, World!"
|
"Hello, World!"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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!"
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,7 @@
|
||||||
|
use axum::Router;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pub fn sys_router() -> Router {
|
||||||
|
Router::new()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue