You are looking at the wrong docs. That's a free function, not the service method of App. That's here and it takes an HttpServiceFactory. The implementors of which are also enumerated in the documentation of that trait.
You want to use App::configure for abstracting App creation into it's own function that you can share between your tests and main function, for example:
Seems like you are able to get make_appto work. I was sure that it would only work for vectors with a single route (i.e. because web::resource("/test").to(|| async { "OK" }) has a distinct type from web::resource("/test2").to(|| async { "OK" }), but that is not the case).
As I was saying in my previous reply though, returning an App from a function is impossible, because AppEntry is private and you can't instantiate App other than through App::new. That's what App::configure is for.
I know this a bit old, but I found an issue with the function. Would you happen to know how to solve the issue?
The issue:
For my request, I return a web::Json object which has to implement Responder instead of the http response in your example. I tried making a vector of those routes (eg. vec![getUser, createUser, registerUser]), but it would defer its type to the first element in the vector (eg. let routes: getUser = ...). I tried making the vector with [web::resource("/test").to(registerUser), ....], but I am getting issues that it doesn't implement handler.
If you have any questions or want a more explicit coding example, please let me know.
Thank you again for all the help if you have been.
is true for handlers created with the get, post, etc. macros. Every handler has its own distinct type. AFAICT you need to use Resources instead of the macro-created handlers:
use actix_web::body::BoxBody;
use actix_web::dev::{HttpServiceFactory, Service, ServiceResponse};
use actix_web::test;
use actix_web::web;
use actix_web::{App, HttpRequest, Responder};
use actix_http::Request;
async fn make_app<T: HttpServiceFactory + 'static>(
routes: Vec<T>,
) -> impl Service<Request, Response = ServiceResponse<BoxBody>, Error = actix_web::Error> {
test::init_service(App::new().service(routes)).await
}
async fn index_get(_: HttpRequest) -> impl Responder {
web::Json(())
}
async fn index_post(_: HttpRequest) -> impl Responder {
web::Json(())
}
#[actix_web::main]
async fn main() {
let routes = vec![
web::resource("/route/index_get").route(web::get().to(index_get)),
web::resource("/route/index_post").route(web::post().to(index_post)),
];
let app = make_app(routes).await;
}
I tried to work around this by creating HttpServiceFactory trait objects from the handlers, but HttpServiceFactory::register is not dynamically dispatchable, hence this won't work unfortunately.