use miko::app::Application;
use miko::router::Router;
use miko::{macros::*, *};
use std::time::Duration;
use tower::timeout::TimeoutLayer;
use tower_http::{compression::CompressionLayer, trace::TraceLayer};
#[get("/slow")]
async fn slow_handler() -> &'static str {
tokio::time::sleep(Duration::from_secs(2)).await;
"Success (Should not see this)"
}
#[get("/fast")]
async fn fast_handler() -> &'static str {
"Fast response"
}
#[get("/large")]
async fn large_handler() -> String {
"A".repeat(1024 * 10) }
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let mut router = Router::new();
router
.with_layer(TraceLayer::new_for_http())
.with_layer(TimeoutLayer::new(Duration::from_secs(1)))
.with_layer(CompressionLayer::new())
.get("/slow", slow_handler)
.get("/fast", fast_handler)
.get("/large", large_handler);
println!("🚀 Server running on http://localhost:8080");
println!("🧪 测试方案:");
println!(" 1. 超时测试: curl -v http://localhost:8080/slow");
println!(
" 预期: 500 Internal Server Error (使用 tower::timeout) 或 408 Request Timeout (使用 tower_http::timeout)"
);
println!(" 2. 正常测试: curl -v http://localhost:8080/fast");
println!(" 预期: 200 OK");
println!(
" 3. 压缩测试: curl -v -H 'Accept-Encoding: gzip' http://localhost:8080/large --output - | gunzip | wc -c"
);
println!(" 预期: 解压后 10240 字节");
Application::new_(router).run().await.unwrap();
}