refactor(tvix/store/pathinfo/from_addr): use match guards
This will allow feature-flagging some of the backends. Change-Id: Ie92914c3e2ad870eee87e73b3b5abe605fb56fe7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11202 Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Reviewed-by: raitobezarius <tvl@lahfa.xyz>
This commit is contained in:
parent
c0566985b0
commit
65b8359ff3
1 changed files with 59 additions and 56 deletions
|
@ -40,78 +40,81 @@ pub async fn from_addr(
|
||||||
let url =
|
let url =
|
||||||
Url::parse(uri).map_err(|e| Error::StorageError(format!("unable to parse url: {}", e)))?;
|
Url::parse(uri).map_err(|e| Error::StorageError(format!("unable to parse url: {}", e)))?;
|
||||||
|
|
||||||
Ok(if url.scheme() == "memory" {
|
let path_info_service: Box<dyn PathInfoService> = match url.scheme() {
|
||||||
// memory doesn't support host or path in the URL.
|
"memory" => {
|
||||||
if url.has_host() || !url.path().is_empty() {
|
// memory doesn't support host or path in the URL.
|
||||||
return Err(Error::StorageError("invalid url".to_string()));
|
if url.has_host() || !url.path().is_empty() {
|
||||||
}
|
return Err(Error::StorageError("invalid url".to_string()));
|
||||||
Box::new(MemoryPathInfoService::new(blob_service, directory_service))
|
}
|
||||||
} else if url.scheme() == "sled" {
|
Box::new(MemoryPathInfoService::new(blob_service, directory_service))
|
||||||
// sled doesn't support host, and a path can be provided (otherwise
|
|
||||||
// it'll live in memory only).
|
|
||||||
if url.has_host() {
|
|
||||||
return Err(Error::StorageError("no host allowed".to_string()));
|
|
||||||
}
|
}
|
||||||
|
"sled" => {
|
||||||
|
// sled doesn't support host, and a path can be provided (otherwise
|
||||||
|
// it'll live in memory only).
|
||||||
|
if url.has_host() {
|
||||||
|
return Err(Error::StorageError("no host allowed".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
if url.path() == "/" {
|
if url.path() == "/" {
|
||||||
return Err(Error::StorageError(
|
return Err(Error::StorageError(
|
||||||
"cowardly refusing to open / with sled".to_string(),
|
"cowardly refusing to open / with sled".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: expose other parameters as URL parameters?
|
// TODO: expose other parameters as URL parameters?
|
||||||
|
|
||||||
if url.path().is_empty() {
|
Box::new(if url.path().is_empty() {
|
||||||
return Ok(Box::new(
|
|
||||||
SledPathInfoService::new_temporary(blob_service, directory_service)
|
SledPathInfoService::new_temporary(blob_service, directory_service)
|
||||||
.map_err(|e| Error::StorageError(e.to_string()))?,
|
.map_err(|e| Error::StorageError(e.to_string()))?
|
||||||
));
|
} else {
|
||||||
|
SledPathInfoService::new(url.path(), blob_service, directory_service)
|
||||||
|
.map_err(|e| Error::StorageError(e.to_string()))?
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return Ok(Box::new(
|
"nix+http" | "nix+https" => {
|
||||||
SledPathInfoService::new(url.path(), blob_service, directory_service)
|
// Stringify the URL and remove the nix+ prefix.
|
||||||
.map_err(|e| Error::StorageError(e.to_string()))?,
|
// We can't use `url.set_scheme(rest)`, as it disallows
|
||||||
));
|
// setting something http(s) that previously wasn't.
|
||||||
} else if url.scheme() == "nix+http" || url.scheme() == "nix+https" {
|
let new_url = Url::parse(url.to_string().strip_prefix("nix+").unwrap()).unwrap();
|
||||||
// Stringify the URL and remove the nix+ prefix.
|
|
||||||
// We can't use `url.set_scheme(rest)`, as it disallows
|
|
||||||
// setting something http(s) that previously wasn't.
|
|
||||||
let new_url = Url::parse(url.to_string().strip_prefix("nix+").unwrap()).unwrap();
|
|
||||||
|
|
||||||
let mut nix_http_path_info_service =
|
let mut nix_http_path_info_service =
|
||||||
NixHTTPPathInfoService::new(new_url, blob_service, directory_service);
|
NixHTTPPathInfoService::new(new_url, blob_service, directory_service);
|
||||||
|
|
||||||
let pairs = &url.query_pairs();
|
let pairs = &url.query_pairs();
|
||||||
for (k, v) in pairs.into_iter() {
|
for (k, v) in pairs.into_iter() {
|
||||||
if k == "trusted-public-keys" {
|
if k == "trusted-public-keys" {
|
||||||
let pubkey_strs: Vec<_> = v.split_ascii_whitespace().collect();
|
let pubkey_strs: Vec<_> = v.split_ascii_whitespace().collect();
|
||||||
|
|
||||||
let mut pubkeys: Vec<narinfo::PubKey> = Vec::with_capacity(pubkey_strs.len());
|
let mut pubkeys: Vec<narinfo::PubKey> = Vec::with_capacity(pubkey_strs.len());
|
||||||
for pubkey_str in pubkey_strs {
|
for pubkey_str in pubkey_strs {
|
||||||
pubkeys
|
pubkeys.push(narinfo::PubKey::parse(pubkey_str).map_err(|e| {
|
||||||
.push(narinfo::PubKey::parse(pubkey_str).map_err(|e| {
|
|
||||||
Error::StorageError(format!("invalid public key: {e}"))
|
Error::StorageError(format!("invalid public key: {e}"))
|
||||||
})?);
|
})?);
|
||||||
|
}
|
||||||
|
|
||||||
|
nix_http_path_info_service.set_public_keys(pubkeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
nix_http_path_info_service.set_public_keys(pubkeys);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Box::new(nix_http_path_info_service)
|
Box::new(nix_http_path_info_service)
|
||||||
} else if url.scheme().starts_with("grpc+") {
|
}
|
||||||
// schemes starting with grpc+ go to the GRPCPathInfoService.
|
scheme if scheme.starts_with("grpc+") => {
|
||||||
// That's normally grpc+unix for unix sockets, and grpc+http(s) for the HTTP counterparts.
|
// schemes starting with grpc+ go to the GRPCPathInfoService.
|
||||||
// - In the case of unix sockets, there must be a path, but may not be a host.
|
// That's normally grpc+unix for unix sockets, and grpc+http(s) for the HTTP counterparts.
|
||||||
// - In the case of non-unix sockets, there must be a host, but no path.
|
// - In the case of unix sockets, there must be a path, but may not be a host.
|
||||||
// Constructing the channel is handled by tvix_castore::channel::from_url.
|
// - In the case of non-unix sockets, there must be a host, but no path.
|
||||||
let client = PathInfoServiceClient::new(tvix_castore::tonic::channel_from_url(&url).await?);
|
// Constructing the channel is handled by tvix_castore::channel::from_url.
|
||||||
Box::new(GRPCPathInfoService::from_client(client))
|
let client =
|
||||||
} else {
|
PathInfoServiceClient::new(tvix_castore::tonic::channel_from_url(&url).await?);
|
||||||
Err(Error::StorageError(format!(
|
Box::new(GRPCPathInfoService::from_client(client))
|
||||||
|
}
|
||||||
|
_ => Err(Error::StorageError(format!(
|
||||||
"unknown scheme: {}",
|
"unknown scheme: {}",
|
||||||
url.scheme()
|
url.scheme()
|
||||||
)))?
|
)))?,
|
||||||
})
|
};
|
||||||
|
|
||||||
|
Ok(path_info_service)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue