Actix-web: Simplify type for actix_web::App?

Created on 9 Mar 2020  路  4Comments  路  Source: actix/actix-web

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.

C-improvement P-web

Most helpful comment

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.

All 4 comments

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.

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kocoten1992 picture kocoten1992  路  3Comments

bbigras picture bbigras  路  5Comments

fafhrd91 picture fafhrd91  路  5Comments

fafhrd91 picture fafhrd91  路  5Comments

volfco picture volfco  路  4Comments