1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +02: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
5 changed files with 61 additions and 26 deletions

View File

@ -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

View File

@ -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"

View File

@ -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
}

View File

@ -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<Info>) -> web::Json<Info> {
println!("=========={:?}=========", info);
web::Json(Info {