Borrowed data escapes outside of associated function in Server::run

Issue Title: Borrowed data escapes outside of associated function in Server::run

Description

This issue reports a compilation error encountered in the Server::run function located in src/server/server.rs. The error message indicates that a borrowed reference (vars) is being used in a closure where its lifetime doesn't match the requirements.

Error Message:

error[E0521]: borrowed data escapes outside of associated function
  --> src/server/server.rs:40:7
   |
22 |     pub async fn run(vars: &Config, auth: bool) {
   |                      ----  - let's call the lifetime of this reference `'1`
   |                      |                                                                |                      `vars` is a reference that is only valid in the associated function body
...
40 | /       App::new()
41 | |       .wrap(HttpAuthentication::basic(move |req, creds| {
42 | |         async move {
43 | |           Auth::auth(auth,vars.clone(), req, creds).await
44 | |         }
45 | |       }))
   | |         ^
   | |         |
   | |_________`vars` escapes the associated function body here
   |           argument requires that `'1` must outlive `'static`

Code

pub struct Server;

impl Server {
  #[actix_web::main]
  pub async fn run(vars: &Config, auth: bool) {
    let socket_addr: SocketAddr = vars.bind.parse().expect("Failed to parse Socket Address");
    info!("[SERVER] running on {}.", socket_addr);

    let governor_conf = GovernorConfigBuilder::default()
    .per_second(1)
    .burst_size(1)
    .finish()
    .unwrap_or_else( || {
      error!("{}", ServerError::RateLimitConfigError.to_string());
      process::exit(1);
    });

    if !auth {
      warn!("[SERVER] Authentication is not enabled! Your server is vulnerable to attacks. Use --auth to enable authentication.");
    }
    
    HttpServer::new(move || {
      App::new()
      .wrap(HttpAuthentication::basic(move |req, creds| {
        async move {
          Auth::auth(auth,vars.clone(), req, creds).await
        }
      }))
      .wrap(Governor::new(&governor_conf))
      .route("/", web::post().to(endpoint))
    })
    .workers(vars.workers)
    .max_connections(vars.max_connections)
    .bind(socket_addr)
    .unwrap_or_else(|err| {
      error!("{}", ServerError::BindError(err.to_string()).to_string());
      process::exit(1);
    })
    .run()
    .await
    .unwrap();
  }
}

Additional Information

  • Programming language: Rust
  • Steps to reproduce (if applicable): Describe the steps to trigger the error.

Labels

  • bug
  • lifetime

Thanks for helping!

Why don't you pass vars by value? Seems like the config is meant for the Server, so it feels like taking ownership here is the most sensible solution to avoid the lifetime issue.


As a side note: Your topic looks a bit atypical for help posts here on URLO and more like you copied a GH issue you created based on some template. Please always share a link to any cross-posts you made on other sites, to avoid duplicated effort by the community.

1 Like

Firstly, thank you for your help.

Secondly, I didn't create any GitHub issues, and this wasn't copied. I formatted it this way to make it easier for others to understand the issue. You could say it's a bit more organized!

But overall, thanks again.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.