mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
14
security/web-cors/backend/Cargo.toml
Normal file
14
security/web-cors/backend/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "actix-web-cors"
|
||||
version = "1.0.0"
|
||||
authors = ["krircc <krircc@aliyun.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-cors = "0.5"
|
||||
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
env_logger = "0.8"
|
||||
futures = "0.3"
|
28
security/web-cors/backend/src/main.rs
Normal file
28
security/web-cors/backend/src/main.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use actix_cors::Cors;
|
||||
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=info");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(
|
||||
Cors::default()
|
||||
.allowed_origin("http://localhost:8080")
|
||||
.allowed_methods(vec!["GET", "POST"])
|
||||
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
|
||||
.allowed_header(header::CONTENT_TYPE)
|
||||
.supports_credentials()
|
||||
.max_age(3600),
|
||||
)
|
||||
.wrap(Logger::default())
|
||||
.service(user::info)
|
||||
})
|
||||
.bind(("127.0.0.1", 8000))?
|
||||
.run()
|
||||
.await
|
||||
}
|
21
security/web-cors/backend/src/user.rs
Normal file
21
security/web-cors/backend/src/user.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use actix_web::{post, web};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Info {
|
||||
username: String,
|
||||
email: String,
|
||||
password: String,
|
||||
confirm_password: String,
|
||||
}
|
||||
|
||||
#[post("/user/info")]
|
||||
pub async fn info(info: web::Json<Info>) -> web::Json<Info> {
|
||||
println!("=========={:?}=========", info);
|
||||
web::Json(Info {
|
||||
username: info.username.clone(),
|
||||
email: info.email.clone(),
|
||||
password: info.password.clone(),
|
||||
confirm_password: info.confirm_password.clone(),
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user