-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose common types for App<T, _> #2301
Conversation
Before this commit, many of the common monomorphizations of `App<T, B>` where impossible to return from a function. For example: ``` /// Create the actix-web application that can be used to start an HttpServer. pub fn create_app( config: AppConfig, ) -> App< impl ServiceFactory< ServiceRequest, Config = (), Response = ServiceResponse<StreamLog<Body>>, Error = actix_web::error::Error, InitError = (), >, StreamLog<Body>, > { unimplemented!() } ``` Would fail to compile since the `ServiceFactory` was not publicly exported from actix-web. --- In this commit, a few types that are commonly used when constructing an App are now exported. This allows you to have a `fn create_app () -> App<T, B>` that can be re-used across your test suite.
we can see about exposing |
pub fn create_app() -> App<
impl ServiceFactory<
ServiceRequest,
Response = ServiceResponse<impl MessageBody>,
Config = (),
InitError = (),
Error = Error,
>,
impl MessageBody,
> {
App::new()
.wrap(Logger::default()) // <-- can be removed without affecting the return type
.route("/", web::to(|| async { "hello" }))
} |
Thanks for the I think your example still doesn't work currently since Can we re-open this PR and I'll change it to re-export |
Ah, one other issue. Your signature works fine, but it doesn't pass the trait bounds for
HttpServer::new(|| {
create_app()
}) I'm reading through the different trait bounds now to get myself familiar with how to get this working, but if you can suggest any tips here that would be awesome. Also, I can't re-open the PR so I believe you'll have to. Thanks! EDIT - I think the issue is that the two |
there's a problem with reopening, you'll have to create a new pr |
indeed it seems like there's no nice way to express the link between those two pub fn create_app() -> App<
impl ServiceFactory<
ServiceRequest,
Response = ServiceResponse<AnyBody>,
Config = (),
InitError = (),
Error = Error,
>,
AnyBody,
> {
App::new()
.wrap(Logger::default())
.wrap(Compat::new(Logger::default()))
.route("/", web::to(|| async { "hello" }))
}
fn main() {
let _ = HttpServer::new(|| create_app());
} |
Alternatively, (and this is what I did in my own projects) using macro_rules! create_app {
() => {
App::new()
.wrap(Logger::default())
.route("/", web::to(|| async { "hello" }))
};
}
fn main() {
let _ = HttpServer::new(|| create_app!());
} |
Yeah the macro was going to be my last resort. Awesome, thanks for teaching me about Compat. Worked like a charm. I'll open a new PR thanks! |
Before this commit, many of the common monomorphizations of
App<T, B>
where impossible to return from a function.
For example:
Would fail to compile since the
ServiceFactory
a a couple of other types were not publicly exported from actix-web.In this commit, a few types that are commonly used when constructing an App are now exported.
This allows you to have a
fn create_app () -> App<T, B>
that can be re-used across your test suite.