Actix Web 多重配置注册

问题描述 投票:0回答:1

当在 actix web 中注册多个 serviceConfig 进行配置时,只有第一个可以工作,并且我无法访问已注册的第二个配置。

HttpServer::new(move || {
    let cors = Cors::default()
        .allow_any_origin()
        .allow_any_method()
        .allow_any_header()
        .supports_credentials();
    App::new()
        .app_data(app_state.clone())
        .wrap(cors)
        .configure(roles::services::config)
        .configure(users::services::config)
})
.bind(("127.0.0.1", 8080))?
.run()
.await

访问所有已注册的配置

rust actix-web
1个回答
0
投票

我正在使用这样的多重配置,在

main.rs

HttpServer::new(|| {
        App::new()
            .configure(collar_controller::config)
            .configure(health_controller::config)
            .configure(proj_controller::config)
            .configure(template_controller::config)
            .configure(file_controller::config)
            .configure(profile_controller::config)
            .configure(snippet_controller::config)
            .configure(proj_share_controller::config)
            .configure(file_version_controller::config)
    })
    .workers(3)
    .bind(address)?
    .run()
    .await

在控制器中,指定如下配置:

use actix_web::{web, HttpResponse, Responder};

pub async fn health() -> impl Responder  {
    HttpResponse::Ok().body("Ok")
}

pub async fn liveness() -> impl Responder  {
    HttpResponse::Ok().body("Ok")
}

pub fn config(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/texhub/actuator")
            .route("/liveness", web::get().to(liveness))
    );
}

这适用于多个配置。

© www.soinside.com 2019 - 2024. All rights reserved.