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

update cors example to 0.5

This commit is contained in:
Rob Ede 2020-10-19 23:36:53 +01:00
parent 15197a271c
commit 5573e5fd34
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
5 changed files with 61 additions and 26 deletions

43
Cargo.lock generated
View File

@ -166,6 +166,20 @@ dependencies = [
"futures-util", "futures-util",
] ]
[[package]]
name = "actix-cors"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aaf0c4345c9663a2822d42602391418fd5766f269109ec6bf1784b056a9356a7"
dependencies = [
"actix-web 3.1.0",
"derive_more",
"futures-util",
"log",
"once_cell",
"tinyvec 1.0.1",
]
[[package]] [[package]]
name = "actix-files" name = "actix-files"
version = "0.2.2" version = "0.2.2"
@ -712,9 +726,9 @@ dependencies = [
name = "actix-web-cors" name = "actix-web-cors"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-cors", "actix-cors 0.5.0",
"actix-web 3.1.0", "actix-web 3.1.0",
"env_logger 0.7.1", "env_logger 0.8.1",
"futures 0.3.6", "futures 0.3.6",
"serde 1.0.116", "serde 1.0.116",
"serde_json", "serde_json",
@ -2326,7 +2340,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" checksum = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3"
dependencies = [ dependencies = [
"atty", "atty",
"humantime", "humantime 1.3.0",
"log", "log",
"regex", "regex",
"termcolor", "termcolor",
@ -2339,7 +2353,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [ dependencies = [
"atty", "atty",
"humantime", "humantime 1.3.0",
"log",
"regex",
"termcolor",
]
[[package]]
name = "env_logger"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54532e3223c5af90a6a757c90b5c5521564b07e5e7a958681bcd2afad421cdcd"
dependencies = [
"atty",
"humantime 2.0.1",
"log", "log",
"regex", "regex",
"termcolor", "termcolor",
@ -2893,6 +2920,12 @@ dependencies = [
"quick-error 1.2.3", "quick-error 1.2.3",
] ]
[[package]]
name = "humantime"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.13.8" version = "0.13.8"
@ -3165,7 +3198,7 @@ dependencies = [
name = "juniper-example" name = "juniper-example"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"actix-cors", "actix-cors 0.4.1",
"actix-web 3.1.0", "actix-web 3.1.0",
"env_logger 0.7.1", "env_logger 0.7.1",
"juniper", "juniper",

View File

@ -1,15 +1,16 @@
# Actix Web CORS example # Actix Web CORS example
## start ## Run Server
1 - backend server ```sh
```bash cd web-cors/backend
$ cd web-cors/backend cargo run
$ cargo run
``` ```
2 - frontend server
```bash ## Run Frontend
$ cd web-cors/frontend In a separate terminal, also run:
$ npm install ```sh
$ npm run serve cd web-cors/frontend
npm install
npm run serve
``` ```
then open browser 'http://localhost:8080' then open browser at: http://localhost:8080

View File

@ -6,9 +6,9 @@ edition = "2018"
[dependencies] [dependencies]
actix-web = "3" actix-web = "3"
actix-cors = "0.4" actix-cors = "0.5"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
env_logger = "0.7" env_logger = "0.8"
futures = "0.3" futures = "0.3"

View File

@ -1,28 +1,28 @@
use actix_cors::Cors; use actix_cors::Cors;
use actix_web::{http::header, middleware::Logger, web, App, HttpServer}; use actix_web::{http::header, middleware::Logger, App, HttpServer};
mod user; mod user;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info"); std::env::set_var("RUST_LOG", "actix=info");
env_logger::init(); env_logger::init();
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap( .wrap(
Cors::new() Cors::default()
.allowed_origin("http://localhost:8080") .allowed_origin("http://localhost:8080")
.allowed_methods(vec!["GET", "POST"]) .allowed_methods(vec!["GET", "POST"])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE) .allowed_header(header::CONTENT_TYPE)
.max_age(3600) .supports_credentials()
.finish(), .max_age(3600),
) )
.wrap(Logger::default()) .wrap(Logger::default())
.service(web::resource("/user/info").route(web::post().to(user::info))) .service(user::info)
}) })
.bind("127.0.0.1:8000")? .bind(("127.0.0.1", 8000))?
.run() .run()
.await .await
} }

View File

@ -1,4 +1,4 @@
use actix_web::web; use actix_web::{post, web};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
@ -9,6 +9,7 @@ pub struct Info {
confirm_password: String, confirm_password: String,
} }
#[post("/user/info")]
pub async fn info(info: web::Json<Info>) -> web::Json<Info> { pub async fn info(info: web::Json<Info>) -> web::Json<Info> {
println!("=========={:?}=========", info); println!("=========={:?}=========", info);
web::Json(Info { web::Json(Info {