1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +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.
## 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
Welcome to contribute !

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()
.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