Skip to content

Commit

Permalink
Enable running remote cargo registry server (solana-labs#33629)
Browse files Browse the repository at this point in the history
* Enable running remote cargo registry server

* use server URL to configure index
  • Loading branch information
pgarg66 authored Oct 10, 2023
1 parent 7c80fa1 commit c929775
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
19 changes: 18 additions & 1 deletion cargo-registry/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
clap::{crate_description, crate_name, value_t_or_exit, App, Arg, ArgMatches},
clap::{crate_description, crate_name, value_t, value_t_or_exit, App, Arg, ArgMatches},
solana_clap_utils::{
hidden_unless_forced,
input_validators::is_url_or_moniker,
Expand Down Expand Up @@ -37,6 +37,7 @@ impl<'a> ClientConfig<'a> {
pub struct Client {
pub rpc_client: Arc<RpcClient>,
pub port: u16,
pub server_url: String,
websocket_url: String,
commitment: commitment_config::CommitmentConfig,
cli_signers: Vec<Keypair>,
Expand Down Expand Up @@ -112,6 +113,18 @@ impl Client {
.takes_value(true)
.help("Cargo registry's local TCP port. The server will bind to this port and wait for requests."),
)
.arg(
Arg::with_name("server_url")
.short("s")
.long("server-url")
.value_name("URL_OR_MONIKER")
.takes_value(true)
.global(true)
.validator(is_url_or_moniker)
.help(
"URL where the registry service will be hosted. Default: http://0.0.0.0:<port>",
),
)
.arg(
Arg::with_name("commitment")
.long("commitment")
Expand Down Expand Up @@ -192,6 +205,9 @@ impl Client {

let port = value_t_or_exit!(matches, "port", u16);

let server_url =
value_t!(matches, "server_url", String).unwrap_or(format!("http://0.0.0.0:{}", port));

Ok(Client {
rpc_client: Arc::new(RpcClient::new_with_timeouts_and_commitment(
json_rpc_url.to_string(),
Expand All @@ -200,6 +216,7 @@ impl Client {
confirm_transaction_initial_timeout,
)),
port,
server_url,
websocket_url,
commitment,
cli_signers: vec![payer_keypair, authority_keypair],
Expand Down
9 changes: 4 additions & 5 deletions cargo-registry/src/dummy_git_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use {
std::{
fs::{self, create_dir_all},
io::ErrorKind,
net::SocketAddr,
path::PathBuf,
process::Command,
},
Expand All @@ -19,15 +18,15 @@ struct RegistryConfig {
pub struct DummyGitIndex {}

impl DummyGitIndex {
pub fn create_or_update_git_repo(root_dir: PathBuf, server_addr: &SocketAddr) {
pub fn create_or_update_git_repo(root_dir: PathBuf, server_url: &str) {
create_dir_all(&root_dir).expect("Failed to create root directory");

let expected_config = serde_json::to_string(&RegistryConfig {
dl: format!(
"http://{}/api/v1/crates/{{crate}}/{{version}}/download",
server_addr
"{}/api/v1/crates/{{crate}}/{{version}}/download",
server_url
),
api: Some(format!("http://{}", server_addr)),
api: Some(server_url.to_string()),
})
.expect("Failed to create expected config");

Expand Down
11 changes: 5 additions & 6 deletions cargo-registry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ impl CargoRegistryService {
async fn main() {
solana_logger::setup_with_default("solana=info");
let client = Arc::new(Client::new().expect("Failed to get RPC Client instance"));
let port = client.port;

let bind_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), client.port);
DummyGitIndex::create_or_update_git_repo(PathBuf::from("/tmp/dummy-git"), &client.server_url);

let registry_service = make_service_fn(move |_| {
let client_inner = client.clone();
Expand All @@ -317,11 +319,8 @@ async fn main() {
}
});

let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), port);
DummyGitIndex::create_or_update_git_repo(PathBuf::from("/tmp/dummy-git"), &addr);

let server = Server::bind(&addr).serve(registry_service);
info!("Server running on on http://{}", addr);
let server = Server::bind(&bind_addr).serve(registry_service);
info!("Server running on on http://{}", bind_addr);

let _ = server.await;
}

0 comments on commit c929775

Please sign in to comment.