Transferring a vector of structures in Rust Ntex server configuration

49 Views Asked by At

Is there a way to pass a variable of structure variables in the configuration of an HTTP server using ntex without use clone() or global variable?

Here is my example code:

use ntex::web::{self, App, HttpServer};

struct HV {
    name: String,
}

struct HS {
    hosts: Vec<HV>,
    sock_addr: Option<String>,
}

struct MS {
    servers: Vec<HS>,
}

impl MS {
    fn new() -> Self {
        MS {
            servers: Vec::new(),
        }
    }
}

async fn index() -> impl web::Responder {
    "Hello World! (index)"
}

#[ntex::main]
async fn main() -> std::io::Result<()> {
    let mut ms = MS::new();
    ms.servers.push(HS {
        hosts: vec![
            HV { name: "example.com".to_string() },
            HV { name: "users.example.com".to_string() },
        ],
        sock_addr: Some("localhost:8080".to_string()),
    });
    ms.servers.push(HS {
        hosts: vec![
            HV { name: "example.net".to_string() },
            HV { name: "users.example.net".to_string() },
        ],
        sock_addr: Some("localhost:8081".to_string()),
    });

    for server in ms.servers.iter_mut() {
        if let Some(sock_addr) = &server.sock_addr {
            println!("Socket Address : {}", sock_addr);

            HttpServer::new(move || {
                let mut app = App::new();
                for host in server.hosts.iter_mut() {
                    println!("Host Name : {}", host.name);
                    let scope = web::scope("/")
                        .guard(web::guard::Host(&host.name))
                        .route("/", web::get().to(index));

                    app = app.service(scope);
                }

                app
            })
            .bind(sock_addr)?
            .run()
            .await?;
        } else {
            println!("Sock Addr Not Found");
        }
    }

    Ok(())
}

And here is the displayed error:

error[E0277]: the trait bound `&mut HS: Clone` is not satisfied in `{closure@src/main.rs:54:29: 54:36}`
  --> src/main.rs:54:29
   |
54 |               HttpServer::new(move || {
   |               --------------- ^------
   |               |               |
   |  _____________|_______________within this `{closure@src/main.rs:54:29: 54:36}`
   | |             |
   | |             required by a bound introduced by this call
55 | |                 let mut app = App::new();
56 | |                 for host in server.hosts.iter_mut() {
57 | |                     println!("Host Name : {}", host.name);
...  |
65 | |                 app
66 | |             })
   | |_____________^ within `{closure@src/main.rs:54:29: 54:36}`, the trait `Clone` is not implemented for `&mut HS`, which is required by `{closure@src/main.rs:54:29: 54:36}: Clone`
   |
   = note: `Clone` is implemented for `&HS`, but not for `&mut HS`
note: required because it's used within this closure
  --> src/main.rs:54:29
   |
54 |             HttpServer::new(move || {
   |                             ^^^^^^^
note: required by a bound in `HttpServer::<F, I, S, B>::new`
  --> /home/explorer/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ntex-1.1.2/src/web/server.rs:87:27
   |
87 |     F: Fn() -> I + Send + Clone + 'static,
   |                           ^^^^^ required by this bound in `HttpServer::<F, I, S, B>::new`
...
96 |     pub fn new(factory: F) -> Self {
   |            --- required by a bound in this associated function

Isn't there a method with Nginx to achieve my goal?

0

There are 0 best solutions below