Skip to content

Creating a hyper service in a match expression #2757

Answered by olix0r
mihaigalos asked this question in Q&A
Discussion options

You must be logged in to vote

You probably want to use tower::util::BoxService. If you really only want to vary the service definition i'd probably move the match into the makeservice, too:

let is_secure = args.is_present("secure");
let service =  make_service_fn(move |_| async move {
    let svc = if is_secure {
        BoxService::new(service_fn(call_secure))
    } else {
        BoxService::new(service_fn(call_insecure))
    };
    Ok::<_, hyper::Error>(svc)
});

If you don't want to move the comparison into make_service, you'll need both the make_service_fn and service_fn to be wrapped in BoxService.

Also note there's a BoxCloneService if the service needs to implement clone.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mihaigalos
Comment options

Answer selected by mihaigalos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants