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

Merge branch 'master' of github.com:actix/examples

This commit is contained in:
Nikolay Kim 2018-04-13 12:32:44 -07:00
commit 2198cd7bbc
4 changed files with 7432 additions and 26 deletions

View File

@ -2,6 +2,9 @@
A curated list of examples related to actix. A curated list of examples related to actix.
## from commulity
* [Rust-webapp-starter](https://github.com/OUISRC/Rust-webapp-starter) : A full-stack Single Page Webapp written in actix-web with vuejs.
## Contribute ## Contribute
Welcome to contribute ! Welcome to contribute !

View File

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

View File

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