2021-08-02 23:09:09 +02:00
|
|
|
//! Run with
|
|
|
|
|
//!
|
|
|
|
|
//! ```not_rust
|
2023-03-10 12:02:11 +01:00
|
|
|
//! cargo run -p example-versioning
|
2021-08-02 23:09:09 +02:00
|
|
|
//! ```
|
|
|
|
|
|
2021-07-22 13:23:50 +02:00
|
|
|
use axum::{
|
2022-08-22 12:23:20 +02:00
|
|
|
extract::{FromRequestParts, Path},
|
|
|
|
|
http::{request::Parts, StatusCode},
|
2025-03-18 11:54:02 +02:00
|
|
|
response::{Html, IntoResponse, Response},
|
2021-10-24 22:05:16 +02:00
|
|
|
routing::get,
|
2022-10-04 19:26:51 +02:00
|
|
|
RequestPartsExt, Router,
|
2021-07-22 13:23:50 +02:00
|
|
|
};
|
2023-03-22 23:42:14 +01:00
|
|
|
use std::collections::HashMap;
|
2022-03-06 12:37:00 +01:00
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
2021-06-13 13:50:56 +02:00
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
2022-03-06 12:37:00 +01:00
|
|
|
tracing_subscriber::registry()
|
2022-11-30 18:46:19 +09:00
|
|
|
.with(
|
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
2024-08-24 14:36:08 +08:00
|
|
|
.unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
|
2022-11-30 18:46:19 +09:00
|
|
|
)
|
2022-03-06 12:37:00 +01:00
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
|
.init();
|
2021-08-01 22:01:33 +02:00
|
|
|
|
2021-06-13 13:50:56 +02:00
|
|
|
// build our application with some routes
|
2025-03-18 11:54:02 +02:00
|
|
|
let app = app();
|
2021-06-13 13:50:56 +02:00
|
|
|
|
|
|
|
|
// run it
|
2023-03-22 23:42:14 +01:00
|
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
2021-06-19 12:50:33 +02:00
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2023-03-22 23:42:14 +01:00
|
|
|
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
2025-12-28 09:25:50 +01:00
|
|
|
axum::serve(listener, app).await;
|
2021-06-13 13:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 11:54:02 +02:00
|
|
|
fn app() -> Router {
|
|
|
|
|
Router::new().route("/{version}/foo", get(handler))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn handler(version: Version) -> Html<String> {
|
|
|
|
|
Html(format!("received request with version {version:?}"))
|
2021-06-13 13:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
enum Version {
|
|
|
|
|
V1,
|
|
|
|
|
V2,
|
|
|
|
|
V3,
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-22 12:23:20 +02:00
|
|
|
impl<S> FromRequestParts<S> for Version
|
2021-06-19 12:50:33 +02:00
|
|
|
where
|
2022-08-17 22:08:24 +02:00
|
|
|
S: Send + Sync,
|
2021-06-19 12:50:33 +02:00
|
|
|
{
|
2021-12-05 18:16:46 +00:00
|
|
|
type Rejection = Response;
|
2021-06-13 13:50:56 +02:00
|
|
|
|
2022-10-04 19:26:51 +02:00
|
|
|
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
|
|
|
|
let params: Path<HashMap<String, String>> =
|
|
|
|
|
parts.extract().await.map_err(IntoResponse::into_response)?;
|
2021-06-13 13:50:56 +02:00
|
|
|
|
|
|
|
|
let version = params
|
|
|
|
|
.get("version")
|
|
|
|
|
.ok_or_else(|| (StatusCode::NOT_FOUND, "version param missing").into_response())?;
|
|
|
|
|
|
2021-08-06 16:17:57 +08:00
|
|
|
match version.as_str() {
|
2021-06-13 13:50:56 +02:00
|
|
|
"v1" => Ok(Version::V1),
|
|
|
|
|
"v2" => Ok(Version::V2),
|
|
|
|
|
"v3" => Ok(Version::V3),
|
|
|
|
|
_ => Err((StatusCode::NOT_FOUND, "unknown version").into_response()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-18 11:54:02 +02:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use axum::{body::Body, http::Request, http::StatusCode};
|
|
|
|
|
use http_body_util::BodyExt;
|
|
|
|
|
use tower::ServiceExt;
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_v1() {
|
|
|
|
|
let response = app()
|
2026-02-11 17:08:22 +09:00
|
|
|
.oneshot(Request::get("/v1/foo").body(Body::empty()).unwrap())
|
2025-03-18 11:54:02 +02:00
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
|
let body = response.into_body();
|
|
|
|
|
let bytes = body.collect().await.unwrap().to_bytes();
|
|
|
|
|
let html = String::from_utf8(bytes.to_vec()).unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(html, "received request with version V1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_v4() {
|
|
|
|
|
let response = app()
|
2026-02-11 17:08:22 +09:00
|
|
|
.oneshot(Request::get("/v4/foo").body(Body::empty()).unwrap())
|
2025-03-18 11:54:02 +02:00
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
let body = response.into_body();
|
|
|
|
|
let bytes = body.collect().await.unwrap().to_bytes();
|
|
|
|
|
let html = String::from_utf8(bytes.to_vec()).unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(html, "unknown version");
|
|
|
|
|
}
|
|
|
|
|
}
|