pub async fn perseus_routes<M: MutableStore + 'static, T: TranslationsManager + 'static>(
    turbine: &'static Turbine<M, T>,
    opts: ServerOptions,
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
    // --- File handlers ---
    let js_bundle = warp::path!(".perseus" / "bundle.js")
        .and(warp::path::end())
        .and(warp::fs::file(opts.js_bundle.clone()));
    let js_bundle_compressed = warp::path!(".perseus" / "bundle.js")
        .and(warp::path::end())
        .and(warp::fs::file(format!("{}.br", opts.js_bundle.clone())))
        .map(|reply| {
            warp::reply::with_header(
                reply,
                "Content-Type",
                "application/javascript; charset=utf-8",
            )
        })
        .map(|reply| warp::reply::with_header(reply, "Content-Encoding", "br"));

    let wasm_bundle_compressed = warp::path!(".perseus" / "bundle.wasm")
        .and(warp::path::end())
        .and(warp::fs::file(format!("{}.br", opts.wasm_bundle.clone())))
        .map(|reply| warp::reply::with_header(reply, "Content-Type", "application/wasm"))
        .map(|reply| warp::reply::with_header(reply, "Content-Encoding", "br"));
    let wasm_bundle = warp::path!(".perseus" / "bundle.wasm")
        .and(warp::path::end())
        .and(warp::fs::file(opts.wasm_bundle.clone()));

    let wasm_js_bundle = warp::path!(".perseus" / "bundle.wasm.js")
        .and(warp::path::end())
        .and(warp::fs::file(opts.wasm_js_bundle.clone()));
    let wasm_js_bundle_compressed = warp::path!(".perseus" / "bundle.wasm.js")
        .and(warp::path::end())
        .and(warp::fs::file(format!(
            "{}.br",
            opts.wasm_js_bundle.clone()
        )))
        .map(|reply| {
            warp::reply::with_header(
                reply,
                "Content-Type",
                "application/javascript; charset=utf-8",
            )
        })
        .map(|reply| warp::reply::with_header(reply, "Content-Encoding", "br"));

    let snippets = warp::path!(".perseus" / "snippets" / ..).and(warp::fs::dir(opts.snippets));

    // --- Translation and subsequent load handlers ---
    let translations =
        warp::path!(".perseus" / "translations" / String).then(move |locale: String| async move {
            ApiResponse(turbine.get_translations(&locale).await)
        });
    let localized_initial_consts = warp::path!(".perseus" / "initial_consts" / String).then(
        move |locale: String| async move {
            let locale = locale.strip_suffix(".js").unwrap_or(&locale);
            ApiResponse(turbine.get_initial_consts(&locale).await)
        },
    );
    let unlocalized_initial_consts = warp::path!(".perseus" / "initial_consts.js")
        .then(move || async move { ApiResponse(turbine.get_initial_consts("").await) });
    let page_data = warp::path!(".perseus" / "page" / String / ..)
        .and(warp::path::tail())
        .and(warp::query::<SubsequentLoadQueryParams>())
        .and(get_http_req())
        .then(
            move |locale: String,
                  path: Tail, // This is the path after the locale that was sent
                  SubsequentLoadQueryParams {
                      entity_name,
                      was_incremental_match,
                  }: SubsequentLoadQueryParams,
                  http_req: Request| async move {
                ApiResponse(
                    turbine
                        .get_subsequent_load(
                            PathWithoutLocale(path.as_str().to_string()),
                            locale,
                            entity_name,
                            was_incremental_match,
                            http_req,
                        )
                        .await,
                )
            },
        );

    // --- Static directory and alias handlers ---
    let static_dir_path = Arc::new(turbine.static_dir.clone());
    let static_dir_path_filter = warp::any().map(move || static_dir_path.clone());
    let static_dir = warp::path!(".perseus" / "static" / ..)
        .and(static_dir_path_filter)
        .and_then(|static_dir_path: Arc<PathBuf>| async move {
            if static_dir_path.exists() {
                Ok(())
            } else {
                Err(warp::reject::not_found())
            }
        })
        .untuple_one() // We need this to avoid a ((), File) (which makes the return type fail)
        // This alternative will never be served, but if we don't have it we'll get a runtime panic
        .and(warp::fs::dir(turbine.static_dir.clone()));
    let static_aliases = warp::any()
        .and(static_aliases_filter(turbine.static_aliases.clone()))
        .and_then(serve_file);

    // --- Initial load handler ---
    let initial_loads = warp::any()
        .and(warp::path::full())
        .and(get_http_req())
        .then(move |path: FullPath, http_req: Request| async move {
            ApiResponse(
                turbine
                    .get_initial_load(PathMaybeWithLocale(path.as_str().to_string()), http_req)
                    .await,
            )
        });

    // Now put all those routes together in the final thing (the user will add this
    // to an existing Warp server)
    js_bundle_compressed
        .or(js_bundle)
        .or(wasm_bundle_compressed)
        .or(wasm_bundle)
        .or(wasm_js_bundle_compressed)
        .or(wasm_js_bundle)
        .or(snippets)
        .or(static_dir)
        .or(static_aliases)
        .or(translations)
        .or(localized_initial_consts)
        .or(unlocalized_initial_consts)
        .or(page_data)
        .or(initial_loads)
}
The routes for Perseus. These will configure an existing Warp instance to
run Perseus, and should be provided after any other routes, as they include
a wildcard route.
packages/perseus-warp/src/lib.rs