Actix: How to have a default redirect which is not allocated / construced every time?

I am trying to replace the catch-all 404 in Actix-Web with a redirect (307) to a constant URL.

Currently I've implemented it via a handler registered to a default service:

async fn redirector() -> HttpResponse {
    HttpResponse::build(StatusCode::TEMPORARY_REDIRECT).append_header((header::LOCATION, "https://tracker.mywaifu.best")).finish()
}

(Commit: Redirect to mywaifu homepage · ckcr4lyf/kiryuu@00d91b5 · GitHub)

But I'm guessing on every missed route now it is constructing this new response, allocating the header memory etc.

Since the redirect I want to return is static, is it possible to somehow do this more efficiently?

Try using web::redirect instead of implementing your own handler. I don't know how much more efficient it will be, but I'm certain there won't be a more efficient way to handle redirects in actix-web that would be worth the effort, unless you really have to deal with extraordinary circumstances in your production workload.

Thanks for the suggestion, I basically ended up doing:

static HOMEPAGE: &'static str = "https://tracker.mywaifu.best";
...
        .default_service(web::to(|| async {
            Redirect::to(HOMEPAGE)
        }))

i.e. use a closure for the default service.