{"id":436,"date":"2024-10-27T10:46:09","date_gmt":"2024-10-27T02:46:09","guid":{"rendered":"https:\/\/cococat.top\/?p=436"},"modified":"2024-10-27T10:46:09","modified_gmt":"2024-10-27T02:46:09","slug":"rust-note-10-axum","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/10\/27\/rust-note-10-axum\/","title":{"rendered":"rust \u7b14\u8bb0 10\uff1aaxum \u6742\u8bb0"},"content":{"rendered":"<h2>1. axum middleware<\/h2>\n<p>axum crate \u672c\u8eab\u4e5f\u6709\u4e00\u4e9b\u57fa\u7840\u7684 middleware \u673a\u5236\uff0c\u5728\u5b9e\u73b0\u7b80\u5355\u7684\u903b\u8f91\u65f6\u53ef\u4ee5\u4e0d\u7528\u63a5\u5165 tower\u3002<\/p>\n<h3>1.1. \u8c03\u7528\u987a\u5e8f<\/h3>\n<p>\u6839\u636e<a href=\"https:\/\/blog.csdn.net\/qq_35270805\/article\/details\/132410650\">\u8fd9\u7bc7\u6587\u7ae0<\/a>\u7684\u4ecb\u7ecd\uff0c\u539f\u751f axum middleware \u7684\u58f0\u660e\u5c42\u7ea7\u548c\u8c03\u7528\u987a\u5e8f\u5176\u5b9e\u662f\u53cd\u8fc7\u6765\u7684\uff0c\u6bd4\u8f83\u6076\u5fc3\u3002\u4f8b\u5982\u8fd9\u4e48\u58f0\u660e\uff1a<\/p>\n<pre><code class=\"language-rust\">use axum::{routing::get, Router};\n\nasync fn handler() {}\n\nlet app = Router::new()\n    .route(&quot;\/&quot;, get(handler))\n    .layer(layer_one)\n    .layer(layer_two)\n    .layer(layer_three);\n<\/code><\/pre>\n<p>\u90a3\u4e48\u5b9e\u9645\u4e0a\uff0c\u5728\u5904\u7406\u8bf7\u6c42\u65f6\u5404\u5c42\u7684\u8c03\u7528\u987a\u5e8f\u662f\uff1a<\/p>\n<pre><code>requests\n           |\n           v\n+----- layer_three -----+\n| +---- layer_two ----+ |\n| | +-- layer_one --+ | |\n| | |               | | |\n| | |    handler    | | |\n| | |               | | |\n| | +-- layer_one --+ | |\n| +---- layer_two ----+ |\n+----- layer_three -----+\n           |\n           v\n        responses\n<\/code><\/pre>\n<p>\u786e\u5b9e\u6709\u70b9\u53cd\u76f4\u89c9\u3002\u5982\u679c\u8981\u7b26\u5408\u76f4\u89c9\uff0c\u5219\u8fd8\u662f\u8981\u7528 tower \u7684\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-rust\">use tower::ServiceBuilder;\nuse axum::{routing::get, Router};\n\nasync fn handler() {}\n\nlet app = Router::new()\n    .route(&quot;\/&quot;, get(handler))\n    .layer(\n        ServiceBuilder::new()\n            .layer(layer_one)\n            .layer(layer_two)\n            .layer(layer_three),\n    );<\/code><\/pre>\n<h3>1.2. \u7b80\u5355\u7684\u4f8b\u5b50<\/h3>\n<p>\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u89e3\u91ca axum \u4e2d\u95f4\u4ef6\u7684\u7528\u6cd5\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u5bf9 handler \u5904\u7406\u65f6\u95f4\u7684\u8ba1\u65f6\u5c42\uff0c\u5219\u8be5 layer \u7684\u51fd\u6570\u9700\u8981\u8fd9\u4e48\u5199\uff1a<\/p>\n<pre><code class=\"language-rust\">use axum::{\n    body::Body,\n    http::{Request, Response},\n    middleware::Next,\n};\n\npub async fn log_request_duration(req: Request&lt;Body&gt;, next: Next) -&gt; Response&lt;Body&gt; {\n    let uri: String = req.uri().to_string();\n    let start_time = Instant::now();\n    let response = next.run(req).await;\n    let ms = start_time.elapsed().as_millis();\n    info!(\n        &quot;Request {} took: {}ms, [{}]&quot;,\n        uri,\n        ms,\n        if ms &gt;= 100 { &quot;SLOW&quot; } else { &quot;OK&quot; }\n    );\n    response\n}<\/code><\/pre>\n<p>\u8fd8\u662f\u6bd4\u8f83\u6e05\u695a\u7684\uff0c\u53ef\u4ee5\u770b\u5230\u4e2d\u95f4\u4ef6 layer \u62e5\u6709\u4e86 Request \u7684\u6240\u6709\u6743\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u8fdb\u5165\u4e1a\u52a1 handler \u4e4b\u524d\u5bf9 Request \u8fdb\u884c\u4fee\u6539\u3002\u4e2d\u95f4\u4ef6\u4e4b\u95f4\u7684\u5c42\u7ea7\u8c03\u7528\u5219\u662f\u901a\u8fc7 <code>next.run(req).await<\/code> \u3002<\/p>\n<p>\u5728\u8def\u7531\u4e0a\uff0c\u53ea\u9700\u8981\u4f7f\u7528 <code>axum::middleware::from_fn<\/code> \u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-rust\">let app = Router::new()\n    .route(&quot;\/&quot;, get(handler))\n    .layer(axum::middleware::from_fn(crate::log_request_duration));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. axum middleware axum crate \u672c\u8eab\u4e5f\u6709\u4e00\u4e9b\u57fa\u7840\u7684 middleware \u673a\u5236\uff0c\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[14,24],"class_list":["post-436","post","type-post","status-publish","format-standard","hentry","category-rust","tag-axum","tag-rust"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=436"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/436\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}