#query-string #routerify #middleware #hyper #request-response #response-body

routerify-query

一个用于解析请求查询字符串的 Routerify 中间件

4 个稳定版本

3.0.0 2022 年 1 月 2 日
2.0.0 2021 年 6 月 17 日
1.1.0 2020 年 5 月 6 日
1.0.0 2020 年 5 月 2 日

#1159 in HTTP 服务器

Download history 115/week @ 2024-03-25 96/week @ 2024-04-01 42/week @ 2024-04-08 103/week @ 2024-04-15 119/week @ 2024-04-22 61/week @ 2024-04-29 63/week @ 2024-05-06 63/week @ 2024-05-13 90/week @ 2024-05-20 32/week @ 2024-05-27 39/week @ 2024-06-03 49/week @ 2024-06-10 292/week @ 2024-06-17 265/week @ 2024-06-24 34/week @ 2024-07-01 32/week @ 2024-07-08

633 每月下载量
用于 minidsp-daemon

MIT 许可证

12KB

Github Actions Status crates.io Documentation MIT

routerify-query

一个 Routerify 中间件,用于解析请求查询字符串并在 req 对象中填充。

文档

安装

将以下内容添加到您的 Cargo.toml

[dependencies]
routerify = "3"
routerify-query = "3" 

示例

use hyper::{Body, Request, Response, Server};
use routerify::{Router, RouterService};
// Import the query_parser function and the RequestQueryExt trait.
use routerify_query::{query_parser, RequestQueryExt};
use std::{convert::Infallible, net::SocketAddr};

// A handler for "/" page. Visit: "/?username=Alice&bookname=HarryPotter" to see query values.
async fn home_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    // Access the query values.
    let user_name = req.query("username").unwrap();
    let book_name = req.query("bookname").unwrap();

    Ok(Response::new(Body::from(format!(
        "User: {}, Book: {}",
        user_name, book_name
    ))))
}

// Create a router.
fn router() -> Router<Body, Infallible> {
    Router::builder()
        // Attach the query_parser middleware.
        .middleware(query_parser())
        .get("/", home_handler)
        .build()
        .unwrap()
}

#[tokio::main]
async fn main() {
    let router = router();

    // Create a Service from the router above to handle incoming requests.
    let service = RouterService::new(router).unwrap();

    // The address on which the server will be listening.
    let addr = SocketAddr::from(([127, 0, 0, 1], 3001));

    // Create a server by passing the created service to `.serve` method.
    let server = Server::bind(&addr).serve(service);

    println!("App is running on: {}", addr);
    if let Err(err) = server.await {
        eprintln!("Server error: {}", err);
    }
}

贡献

欢迎您的 PR 和建议。

依赖关系

~8–19MB
~261K SLoC