actix / actix-web

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
https://actix.rs
Apache License 2.0
21.83k stars 1.69k forks source link

Simplify type for actix_web::App? #1402

Open alex opened 4 years ago

alex commented 4 years ago

I'm trying to write a function that builds my app, that way it can be reused between my main function and my tests. Unfortunate, the type parameters for actix_web::App or the IntoServiceFactory traits are incredibly verbose. Ideally doing something like this would be straightforward:

fn create_app() -> actix_web::App {
    let templates = tera::Tera::new("templates/*").unwrap();
    actix_web::App::new()
        .data(AppState { templates })
        .route("/", actix_web::web::get().to(index))
        .route("/tool/", actix_web::web::post().to(tool))
}

I think the right solution might be a type alias that would let you do -> impl Something, but I'm open to suggestions on what the right way to handle this ergonomic issue are.

onelson commented 4 years ago

There were some tactics explored in the comments for #1190. There might be something there for you, but I agree. The trait bounds are tricky to reverse engineer for this.

alex commented 4 years ago

Waaa, a wild Owen appears! 👋

I ultimately went with a configure() solution, which seems to work well enough, even if it makes the caller slightly more verbose than I'd like.

It seems like the best short-term solution would be to highlight configure() more in the docs -- particularly I think it'd be good if the testing docs used it.

dzikoysk commented 4 years ago

It could be great to have such a possibility. I don't think that workarounds are a proper solution for this.

robjtede commented 4 years ago

Some progress made in #1692. As a breaking change, it won't be around until v4 though.