diff --git a/Cargo.lock b/Cargo.lock index 336b7114..9b2c5aa6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,6 +166,20 @@ dependencies = [ "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]] name = "actix-files" version = "0.2.2" @@ -712,9 +726,9 @@ dependencies = [ name = "actix-web-cors" version = "1.0.0" dependencies = [ - "actix-cors", + "actix-cors 0.5.0", "actix-web 3.1.0", - "env_logger 0.7.1", + "env_logger 0.8.1", "futures 0.3.6", "serde 1.0.116", "serde_json", @@ -2326,7 +2340,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" dependencies = [ "atty", - "humantime", + "humantime 1.3.0", "log", "regex", "termcolor", @@ -2339,7 +2353,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" dependencies = [ "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", "regex", "termcolor", @@ -2893,6 +2920,12 @@ dependencies = [ "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]] name = "hyper" version = "0.13.8" @@ -3165,7 +3198,7 @@ dependencies = [ name = "juniper-example" version = "0.2.0" dependencies = [ - "actix-cors", + "actix-cors 0.4.1", "actix-web 3.1.0", "env_logger 0.7.1", "juniper", diff --git a/web-cors/README.md b/web-cors/README.md index cff3c1d0..4dd9f8d4 100644 --- a/web-cors/README.md +++ b/web-cors/README.md @@ -1,15 +1,16 @@ # Actix Web CORS example -## start -1 - backend server -```bash -$ cd web-cors/backend -$ cargo run +## Run Server +```sh +cd web-cors/backend +cargo run ``` -2 - frontend server -```bash -$ cd web-cors/frontend -$ npm install -$ npm run serve + +## Run Frontend +In a separate terminal, also run: +```sh +cd web-cors/frontend +npm install +npm run serve ``` -then open browser 'http://localhost:8080' +then open browser at: http://localhost:8080 diff --git a/web-cors/backend/Cargo.toml b/web-cors/backend/Cargo.toml index 713bc5de..3db02f44 100644 --- a/web-cors/backend/Cargo.toml +++ b/web-cors/backend/Cargo.toml @@ -6,9 +6,9 @@ edition = "2018" [dependencies] actix-web = "3" -actix-cors = "0.4" +actix-cors = "0.5" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -env_logger = "0.7" +env_logger = "0.8" futures = "0.3" diff --git a/web-cors/backend/src/main.rs b/web-cors/backend/src/main.rs index b88f239c..6f2cc4c8 100644 --- a/web-cors/backend/src/main.rs +++ b/web-cors/backend/src/main.rs @@ -1,28 +1,28 @@ 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; #[actix_web::main] 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(); HttpServer::new(move || { App::new() .wrap( - Cors::new() + Cors::default() .allowed_origin("http://localhost:8080") .allowed_methods(vec!["GET", "POST"]) .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) .allowed_header(header::CONTENT_TYPE) - .max_age(3600) - .finish(), + .supports_credentials() + .max_age(3600), ) .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() .await } diff --git a/web-cors/backend/src/user.rs b/web-cors/backend/src/user.rs index 09239c16..77664944 100644 --- a/web-cors/backend/src/user.rs +++ b/web-cors/backend/src/user.rs @@ -1,4 +1,4 @@ -use actix_web::web; +use actix_web::{post, web}; use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize, Debug)] @@ -9,6 +9,7 @@ pub struct Info { confirm_password: String, } +#[post("/user/info")] pub async fn info(info: web::Json) -> web::Json { println!("=========={:?}=========", info); web::Json(Info {