diff --git a/README.md b/README.md index 3d3b6b4..d154135 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ This project depends on internal APIs used by the GitHub Actions Cache. See `gha-cache/README.md` for more details on how to obtain the required tokens. ``` -cargo run -- -c creds.json +cargo run -- -c creds.json --upstream https://cache.nixos.org cargo build --release --target x86_64-unknown-linux-musl cargo build --release --target aarch64-unknown-linux-musl nix copy --to 'http://127.0.0.1:3000' $(which bash) diff --git a/nix-actions-cache/src/main.rs b/nix-actions-cache/src/main.rs index 6360ef8..b9c481b 100644 --- a/nix-actions-cache/src/main.rs +++ b/nix-actions-cache/src/main.rs @@ -58,12 +58,20 @@ struct Args { /// Using another version string allows you to "bust" the cache. #[arg(long)] cache_version: Option, + + /// The upstream cache. + /// + /// Requests for unknown NARs are redirected to this cache + /// instead. + #[arg(long)] + upstream: Option, } /// The global server state. #[derive(Debug)] struct StateInner { api: Api, + upstream: Option, } #[tokio::main] @@ -91,7 +99,10 @@ async fn main() { api.mutate_version(cache_version.as_bytes()); } - let state = Arc::new(StateInner { api }); + let state = Arc::new(StateInner { + api, + upstream: args.upstream, + }); let app = Router::new() .route("/", get(root)) @@ -160,7 +171,11 @@ async fn get_narinfo( return Ok(Redirect::temporary(&url)); } - Err(Error::NotFound) + if let Some(upstream) = &state.upstream { + Ok(Redirect::temporary(&format!("{}/{}", upstream, path))) + } else { + Err(Error::NotFound) + } } async fn put_narinfo( Extension(state): Extension, @@ -193,7 +208,11 @@ async fn get_nar(Extension(state): Extension, Path(path): Path) - return Ok(Redirect::temporary(&url)); } - Err(Error::NotFound) + if let Some(upstream) = &state.upstream { + Ok(Redirect::temporary(&format!("{}/nar/{}", upstream, path))) + } else { + Err(Error::NotFound) + } } async fn put_nar( Extension(state): Extension,