1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 09:42:40 +01:00
actix-extras/examples/web-cors/backend/src/main.rs

46 lines
1.2 KiB
Rust
Raw Normal View History

2018-01-27 04:00:26 +01:00
#[macro_use] extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate futures;
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate http;
use std::env;
2018-01-27 04:51:13 +01:00
use http::header;
use actix_web::*;
use actix_web::middleware::cors;
2018-01-27 04:00:26 +01:00
mod user;
2018-01-27 04:51:13 +01:00
use user::info;
2018-01-27 04:00:26 +01:00
fn main() {
2018-01-27 04:51:13 +01:00
env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
2018-01-27 04:00:26 +01:00
let sys = actix::System::new("Actix-web-CORS");
2018-01-27 04:51:13 +01:00
2018-01-27 04:00:26 +01:00
HttpServer::new(
|| Application::new()
.middleware(middleware::Logger::default())
.resource("/user/info", |r| {
cors::Cors::build()
.allowed_origin("http://localhost:1234")
.allowed_methods(vec!["GET", "POST"])
2018-01-27 04:51:13 +01:00
.allowed_headers(
vec![header::AUTHORIZATION,
header::ACCEPT, header::CONTENT_TYPE])
.max_age(3600)
.finish().expect("Can not create CORS middleware")
.register(r);
2018-01-27 04:00:26 +01:00
r.method(Method::POST).a(info);
}))
.bind("127.0.0.1:8000").unwrap()
.shutdown_timeout(200)
.start();
let _ = sys.run();
2018-01-27 04:51:13 +01:00
}