1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

update web-cors

This commit is contained in:
krircc 2018-04-13 16:03:58 +08:00
parent 77498f7bc5
commit 8afcbcd1e2
3 changed files with 7429 additions and 26 deletions

View File

@ -7,7 +7,7 @@ extern crate actix_web;
extern crate env_logger;
use std::env;
use actix_web::{http, middleware, server, App};
use actix_web::{server, App, http::{header, Method}, middleware, middleware::cors::Cors};
mod user;
use user::info;
@ -20,23 +20,18 @@ fn main() {
let sys = actix::System::new("Actix-web-CORS");
server::new(
|| App::new()
move || App::new()
.middleware(middleware::Logger::default())
.resource("/user/info", |r| {
middleware::cors::Cors::build()
.allowed_origin("http://localhost:1234")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(
vec![http::header::AUTHORIZATION,
http::header::ACCEPT,
http::header::CONTENT_TYPE])
.max_age(3600)
.finish().expect("Can not create CORS middleware")
.register(r);
r.method(http::Method::POST).a(info);
}))
.configure(|app| Cors::for_app(app)
.allowed_origin("http://localhost:1234")
.allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE)
.max_age(3600)
.resource("/user/info", |r| { r.method(Method::POST).with(info);})
.register()))
.bind("127.0.0.1:8000").unwrap()
.shutdown_timeout(200)
.shutdown_timeout(2)
.start();
let _ = sys.run();

View File

@ -1,19 +1,20 @@
use actix_web::{AsyncResponder, Error, HttpMessage, HttpResponse, HttpRequest};
use futures::Future;
use actix_web::{Result, Json};
#[derive(Deserialize,Serialize, Debug)]
struct Info {
pub struct Info {
username: String,
email: String,
password: String,
confirm_password: String,
}
pub fn info(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
req.json()
.from_err()
.and_then(|res: Info| {
Ok(HttpResponse::Ok().json(res))
}).responder()
pub fn info(info: Json<Info>) -> Result<Json<Info>> {
println!("=========={:?}=========", info);
Ok(Json(Info{
username: info.username.clone(),
email: info.email.clone(),
password: info.password.clone(),
confirm_password: info.confirm_password.clone(),
}))
}

7407
web-cors/frontend/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff