#cors #routerify #middleware #hyper #response-body

routerify-cors

一个启用CORS的Routerify中间件

4个稳定版本

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

HTTP服务器中排名1305

Download history 58/week @ 2024-03-14 40/week @ 2024-03-21 76/week @ 2024-03-28 65/week @ 2024-04-04 31/week @ 2024-04-11 17/week @ 2024-04-18 32/week @ 2024-04-25 35/week @ 2024-05-02 49/week @ 2024-05-09 33/week @ 2024-05-16 15/week @ 2024-05-23 23/week @ 2024-05-30 10/week @ 2024-06-06 11/week @ 2024-06-13 28/week @ 2024-06-20 5/week @ 2024-06-27

每月下载量56
用于safepkt-backend

MIT许可

9KB

Github Actions Status crates.io Documentation MIT

routerify-cors

一个Routerify中间件,用于启用CORS

文档

安装

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

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

示例

use hyper::{Body, Request, Response, Server};
use routerify::{Router, RouterService};
// Import the CORS crate.
use routerify_cors::enable_cors_all;
use std::{convert::Infallible, net::SocketAddr};

// A handler for "/" page.
async fn home_handler(_: Request<Body>) -> Result<Response<Body>, Infallible> {
    Ok(Response::new(Body::from("Home page")))
}

// Create a router.
fn router() -> Router<Body, Infallible> {
    Router::builder()
        // Attach the CORS middleware.
        .middleware(enable_cors_all())
        .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和建议总是受欢迎的。

依赖

~7–18MB
~212K SLoC