Skip to main content

fetchlet_http/axum_server/
artifact.rs

1use axum::Router;
2use tower_http::services::ServeDir;
3
4use super::AxumService;
5use crate::{Artifact, AssetService};
6
7/// A router that serves the artifact's client assets and passes every other
8/// request to the application.
9///
10/// Assets answer GET and HEAD, from the artifact's asset archive when it has
11/// one and from the client directory otherwise; other methods and missing
12/// paths reach `rendering`, so the framework handles its own routes, actions
13/// and 404 pages. Merge it into a router of Rust routes with
14/// [`Router::merge`]; it supplies the fallback.
15pub fn artifact_router(artifact: &Artifact, rendering: AxumService) -> Router {
16    if let Some(archive) = artifact.asset_archive() {
17        return Router::new().fallback_service(AssetService::new(archive.clone(), rendering));
18    }
19    match artifact.client_directory() {
20        Some(client) => Router::new().fallback_service(
21            ServeDir::new(client)
22                .call_fallback_on_method_not_allowed(true)
23                .fallback(rendering),
24        ),
25        None => Router::new().fallback_service(rendering),
26    }
27}