Support acting as a pull-through cache

This commit is contained in:
Zhaofeng Li 2023-05-08 12:59:57 -06:00
parent 7b5da1fb69
commit b1b798ef40
2 changed files with 23 additions and 4 deletions

View file

@ -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)

View file

@ -58,12 +58,20 @@ struct Args {
/// Using another version string allows you to "bust" the cache.
#[arg(long)]
cache_version: Option<String>,
/// The upstream cache.
///
/// Requests for unknown NARs are redirected to this cache
/// instead.
#[arg(long)]
upstream: Option<String>,
}
/// The global server state.
#[derive(Debug)]
struct StateInner {
api: Api,
upstream: Option<String>,
}
#[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<State>,
@ -193,7 +208,11 @@ async fn get_nar(Extension(state): Extension<State>, Path(path): Path<String>) -
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<State>,