Skip to main content

Module actix

Module actix 

Source
Expand description

actix-web 4 集成。

守卫注册进 app_data,处理器把它直接写在参数里:

use actix_web::{web, App, HttpServer};
use encryption::guard::Guard;

async fn store_phone(guard: Guard) -> String {
    guard.encrypt("13800138000".as_bytes()).unwrap().len().to_string()
}

let guard: Guard = todo!();
let app = move || {
    App::new()
        .app_data(web::Data::new(guard.clone()))
        .route("/", web::get().to(store_phone))
};

§与 Guarded 的关系

actix 没有「单一应用状态」这个概念,它的 app_data 是按类型取值的, 所以这里走的是 Data<Guard> 这条路,而不是 Guarded。

状态结构体比较大时,注册 Data<AppState> 并让 AppState 实现 Guarded,然后在处理器里写 state.guard() —— 那条路 一样通,只是取用方式不同。