Support acting as a pull-through cache
This commit is contained in:
parent
7b5da1fb69
commit
b1b798ef40
|
@ -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.
|
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 x86_64-unknown-linux-musl
|
||||||
cargo build --release --target aarch64-unknown-linux-musl
|
cargo build --release --target aarch64-unknown-linux-musl
|
||||||
nix copy --to 'http://127.0.0.1:3000' $(which bash)
|
nix copy --to 'http://127.0.0.1:3000' $(which bash)
|
||||||
|
|
|
@ -58,12 +58,20 @@ struct Args {
|
||||||
/// Using another version string allows you to "bust" the cache.
|
/// Using another version string allows you to "bust" the cache.
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
cache_version: Option<String>,
|
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.
|
/// The global server state.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct StateInner {
|
struct StateInner {
|
||||||
api: Api,
|
api: Api,
|
||||||
|
upstream: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -91,7 +99,10 @@ async fn main() {
|
||||||
api.mutate_version(cache_version.as_bytes());
|
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()
|
let app = Router::new()
|
||||||
.route("/", get(root))
|
.route("/", get(root))
|
||||||
|
@ -160,8 +171,12 @@ async fn get_narinfo(
|
||||||
return Ok(Redirect::temporary(&url));
|
return Ok(Redirect::temporary(&url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(upstream) = &state.upstream {
|
||||||
|
Ok(Redirect::temporary(&format!("{}/{}", upstream, path)))
|
||||||
|
} else {
|
||||||
Err(Error::NotFound)
|
Err(Error::NotFound)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
async fn put_narinfo(
|
async fn put_narinfo(
|
||||||
Extension(state): Extension<State>,
|
Extension(state): Extension<State>,
|
||||||
Path(path): Path<String>,
|
Path(path): Path<String>,
|
||||||
|
@ -193,8 +208,12 @@ async fn get_nar(Extension(state): Extension<State>, Path(path): Path<String>) -
|
||||||
return Ok(Redirect::temporary(&url));
|
return Ok(Redirect::temporary(&url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(upstream) = &state.upstream {
|
||||||
|
Ok(Redirect::temporary(&format!("{}/nar/{}", upstream, path)))
|
||||||
|
} else {
|
||||||
Err(Error::NotFound)
|
Err(Error::NotFound)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
async fn put_nar(
|
async fn put_nar(
|
||||||
Extension(state): Extension<State>,
|
Extension(state): Extension<State>,
|
||||||
Path(path): Path<String>,
|
Path(path): Path<String>,
|
||||||
|
|
Loading…
Reference in a new issue