1 个不稳定版本
0.2.0-r1 | 2020年1月18日 |
---|
#991 in HTTP服务器
485KB
11K SLoC
Cors中间件可用于actix web框架

文档与社区资源
- 用户指南
- API文档
- 在gitter上聊天
- Cargo包:actix-cors
- 最低支持的Rust版本:1.34或更高
lib.rs
:
为Actix应用程序提供跨域资源共享(CORS)
CORS中间件可以与应用程序和资源一起使用。CORS中间件可以用作App::wrap()
、Resource::wrap()
或Scope::wrap()
方法的参数。
示例
use requiem_cors::Cors;
use requiem_web::{http, web, App, HttpRequest, HttpResponse, HttpServer};
async fn index(req: HttpRequest) -> &'static str {
"Hello world"
}
fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new()
.wrap(
Cors::new() // <- Construct CORS middleware builder
.allowed_origin("https://rust-lang.net.cn/")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
.allowed_header(http::header::CONTENT_TYPE)
.max_age(3600)
.finish())
.service(
web::resource("/index.html")
.route(web::get().to(index))
.route(web::head().to(|| HttpResponse::MethodNotAllowed()))
))
.bind("127.0.0.1:8080")?;
Ok(())
}
在这个示例中,为"/index.html"端点注册了自定义的CORS中间件。
CORS中间件会自动处理OPTIONS预请求。
依赖项
~26MB
~555K SLoC