Try to figure out "dynamic routing"
This commit is contained in:
parent
a9189dc052
commit
8c6bafbb5c
9 changed files with 115 additions and 82 deletions
90
Cargo.lock
generated
90
Cargo.lock
generated
|
|
@ -76,13 +76,13 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.79"
|
||||
version = "0.1.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681"
|
||||
checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.58",
|
||||
"syn 2.0.59",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -196,9 +196,9 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
|
|||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.92"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2678b2e3449475e95b0aa6f9b506a28e61b3dc8996592b983695e8ebb58a8b41"
|
||||
checksum = "17f6e324229dc011159fcc089755d1e2e216a90d43a7dea6853ca740b84f35e7"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
|
|
@ -404,9 +404,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
|||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "1.2.0"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "186548d73ac615b32a73aafe38fb4f56c0d340e110e5a200bcadbaf2e199263a"
|
||||
checksum = "9f24ce812868d86d19daa79bf3bf9175bc44ea323391147a5e3abde2a283871b"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-channel",
|
||||
|
|
@ -619,7 +619,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.58",
|
||||
"syn 2.0.59",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -660,9 +660,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.79"
|
||||
version = "1.0.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
|
||||
checksum = "a56dea16b0a29e94408b9aa5e2940a4eedbd128a1ba20e8f7ae60fd3d465af0e"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
|
@ -764,14 +764,14 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.58",
|
||||
"syn 2.0.59",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.115"
|
||||
version = "1.0.116"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd"
|
||||
checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
|
|
@ -808,6 +808,7 @@ dependencies = [
|
|||
"env_logger",
|
||||
"log",
|
||||
"tokio",
|
||||
"tower",
|
||||
"utoipa",
|
||||
]
|
||||
|
||||
|
|
@ -872,9 +873,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.58"
|
||||
version = "2.0.59"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
|
||||
checksum = "4a6531ffc7b071655e4ce2e04bd464c4830bb585a61cabb96cf808f05172615a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
|
@ -920,7 +921,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.58",
|
||||
"syn 2.0.59",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1005,7 +1006,7 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
"quote",
|
||||
"regex",
|
||||
"syn 2.0.58",
|
||||
"syn 2.0.59",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1057,7 +1058,7 @@ version = "0.52.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||
dependencies = [
|
||||
"windows-targets 0.52.4",
|
||||
"windows-targets 0.52.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1077,17 +1078,18 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.4"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b"
|
||||
checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.52.4",
|
||||
"windows_aarch64_msvc 0.52.4",
|
||||
"windows_i686_gnu 0.52.4",
|
||||
"windows_i686_msvc 0.52.4",
|
||||
"windows_x86_64_gnu 0.52.4",
|
||||
"windows_x86_64_gnullvm 0.52.4",
|
||||
"windows_x86_64_msvc 0.52.4",
|
||||
"windows_aarch64_gnullvm 0.52.5",
|
||||
"windows_aarch64_msvc 0.52.5",
|
||||
"windows_i686_gnu 0.52.5",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc 0.52.5",
|
||||
"windows_x86_64_gnu 0.52.5",
|
||||
"windows_x86_64_gnullvm 0.52.5",
|
||||
"windows_x86_64_msvc 0.52.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1098,9 +1100,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
|||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.4"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9"
|
||||
checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
|
|
@ -1110,9 +1112,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
|||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.4"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675"
|
||||
checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
|
|
@ -1122,9 +1124,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
|||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.4"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3"
|
||||
checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
|
|
@ -1134,9 +1142,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
|||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.4"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02"
|
||||
checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
|
|
@ -1146,9 +1154,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
|||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.4"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03"
|
||||
checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
|
|
@ -1158,9 +1166,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
|||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.4"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177"
|
||||
checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
|
|
@ -1170,6 +1178,6 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
|||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.4"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"
|
||||
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ log = "0.4.21"
|
|||
env_logger = "0.11.3"
|
||||
tokio = "1.37.0"
|
||||
axum = "0.7.5"
|
||||
tower = "0.4.13"
|
||||
utoipa = "4.2.0"
|
||||
|
||||
[workspace.lints.clippy]
|
||||
uninlined_format_args = "warn"
|
||||
|
|
|
|||
4
Justfile
4
Justfile
|
|
@ -9,10 +9,10 @@ build_server:
|
|||
cargo build
|
||||
|
||||
start_server: build_server
|
||||
cargo run
|
||||
RUST_LOG=server=trace cargo run
|
||||
|
||||
# watch_server:
|
||||
# cargo watch -x run
|
||||
# RUST_LOG=server=trace cargo watch -x run
|
||||
|
||||
# test_server: build_server build_tests
|
||||
# just start_server & sleep 1 && podman run --rm -it --net=host rvault-go-tests
|
||||
|
|
|
|||
|
|
@ -13,5 +13,6 @@ workspace = true
|
|||
log = { workspace = true }
|
||||
env_logger = { workspace = true }
|
||||
tokio = { workspace = true, features=["full"] }
|
||||
tower = { workspace = true, features = []}
|
||||
axum = { workspace = true }
|
||||
utoipa = { version = "4", features = ["axum_extras"] }
|
||||
utoipa = { workspace = true, features = ["axum_extras"] }
|
||||
|
|
|
|||
|
|
@ -5,8 +5,7 @@ use axum::Router;
|
|||
|
||||
// use self::token::token_auth_router;
|
||||
|
||||
|
||||
pub fn auth_router() -> Router {
|
||||
Router::new()
|
||||
// .nest("/token", token_auth_router())
|
||||
// .nest("/token", token_auth_router())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
use axum::Router;
|
||||
|
||||
|
||||
pub fn identity_router() -> Router {
|
||||
Router::new()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
use axum::{extract::Request, http::StatusCode, routing::get, Router};
|
||||
use log::{info, warn};
|
||||
use tokio::net::TcpListener;
|
||||
use axum::{
|
||||
extract::Request,
|
||||
http::StatusCode,
|
||||
routing::{get, trace},
|
||||
Router,
|
||||
};
|
||||
use log::*;
|
||||
use std::{env, net::SocketAddr, str::FromStr};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
mod auth;
|
||||
mod identity;
|
||||
|
|
@ -10,14 +15,12 @@ mod sys;
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env::set_var("RUST_LOG", "trace");
|
||||
// To be configured via environment variables
|
||||
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");
|
||||
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()
|
||||
|
|
@ -29,14 +32,18 @@ async fn main() {
|
|||
.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();
|
||||
}
|
||||
|
||||
async fn fallback_route_unknown(req: Request) -> (StatusCode, &'static str) {
|
||||
log::error!("Route not found: {} {}, payload {:?}", req.method(), req.uri(), req.body());
|
||||
log::error!(
|
||||
"Route not found: {} {}, payload {:?}",
|
||||
req.method(),
|
||||
req.uri(),
|
||||
req.body()
|
||||
);
|
||||
|
||||
(StatusCode::NOT_FOUND, "Route not implemented")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +1,50 @@
|
|||
use axum::{body::Body, http::Request, routing::*, Router};
|
||||
use std::convert::Infallible;
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode},
|
||||
middleware::map_request,
|
||||
response::Response,
|
||||
routing::*,
|
||||
Router,
|
||||
};
|
||||
use tower::{service_fn, util::BoxService, Service};
|
||||
|
||||
pub fn secrets_router() -> Router {
|
||||
Router::new()
|
||||
.fallback_service(any(handler))
|
||||
// let middleware = tower::util::MapRequestLayer::new(handler);
|
||||
|
||||
Router::new().layer(map_request(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]);
|
||||
// TODO: Find a solution for this mess
|
||||
async fn handler(request: Request<Body>) -> Result<Request<Body>, StatusCode> {
|
||||
// let path: Vec<&str> = request.uri().path().split('/').clone().collect();
|
||||
// log::info!("path, {:?}", path[1]);
|
||||
|
||||
match path[1] {
|
||||
"test" => {
|
||||
log::info!("test route");
|
||||
// TODO: Nest another Router here
|
||||
let root = service_fn(|req: Request<String>| async move {
|
||||
let res = Response::new("Hello, World!".to_string());
|
||||
Ok::<_, Infallible>(res)
|
||||
});
|
||||
let root = BoxService::new(root);
|
||||
|
||||
}
|
||||
_ => {
|
||||
log::info!("default");
|
||||
}
|
||||
}
|
||||
let mut routes = vec!["/abc", "/def"];
|
||||
routes.sort_unstable_by(|a, b| a.len().cmp(&b.len()));
|
||||
|
||||
"Hello, World!"
|
||||
}
|
||||
|
||||
// basic handler that responds with a static string
|
||||
async fn tada() -> &'static str {
|
||||
log::info!("Hello world");
|
||||
"Hello, World!"
|
||||
let mut app = Router::new();
|
||||
app.as_service().call(request).await.unwrap();
|
||||
|
||||
// match path[1] {
|
||||
// "test" => {
|
||||
// log::info!("test route");
|
||||
// // TODO: Nest another Router here
|
||||
// return Ok(Request::new(Body::empty()));
|
||||
// }
|
||||
// _ => {
|
||||
// log::info!("default");
|
||||
// return Err(StatusCode::NOT_FOUND);
|
||||
// }
|
||||
// }
|
||||
|
||||
Err(StatusCode::IM_A_TEAPOT)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
use axum::Router;
|
||||
|
||||
|
||||
|
||||
pub fn sys_router() -> Router {
|
||||
Router::new()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue