Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add flag for router IP local run #565

Merged
merged 13 commits into from
Jan 10, 2023
Merged
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cargo-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ headers = "0.3.8"
indicatif = "0.17.2"
ignore = "0.4.18"
indoc = "1.0.7"
local-ip-address = "0.5.0"
log = "0.4.17"
openssl = { version = '0.10', optional = true }
portpicker = "0.1.1"
Expand Down
3 changes: 3 additions & 0 deletions cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ pub struct RunArgs {
/// port to start service on
#[clap(long, default_value = "8000")]
pub port: u16,
/// use router ip address instead of localhost
#[clap(long)]
pub router_ip: bool
}

#[derive(Parser, Debug)]
Expand Down
4 changes: 3 additions & 1 deletion cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use futures::StreamExt;
use git2::{Repository, StatusOptions};
use ignore::overrides::OverrideBuilder;
use ignore::WalkBuilder;
use local_ip_address::local_ip;
use shuttle_common::models::{project, secret};
use shuttle_service::loader::{build_crate, Loader};
use shuttle_service::Logger;
Expand Down Expand Up @@ -405,7 +406,8 @@ impl Shuttle {
secrets,
working_directory.to_path_buf(),
)?;
let addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), run_args.port);
let addr = SocketAddr::new(if run_args.router_ip
chesedo marked this conversation as resolved.
Show resolved Hide resolved
{local_ip().unwrap()} else {Ipv4Addr::LOCALHOST.into()}, run_args.port);

trace!("loading project");
println!(
Expand Down
68 changes: 67 additions & 1 deletion cargo-shuttle/tests/integration/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cargo_shuttle::{Args, Command, ProjectArgs, RunArgs, Shuttle};
use local_ip_address::local_ip;
use portpicker::pick_unused_port;
use reqwest::StatusCode;
use std::{fs::canonicalize, process::exit, time::Duration};
Expand All @@ -8,7 +9,7 @@ use tokio::time::sleep;
async fn cargo_shuttle_run(working_directory: &str) -> u16 {
let working_directory = canonicalize(working_directory).unwrap();
let port = pick_unused_port().unwrap();
let run_args = RunArgs { port };
let run_args = RunArgs { port, router_ip: false};

let runner = Shuttle::new().unwrap().run(Args {
api_url: Some("http://shuttle.invalid:80".to_string()),
Expand Down Expand Up @@ -359,3 +360,68 @@ async fn thruster_hello_world() {

assert_eq!(request_text, "Hello, World!");
}

/// creates a `cargo-shuttle` run instance with some reasonable defaults set, but using the router IP instead.
async fn cargo_shuttle_run_with_router_ip(working_directory: &str) -> u16 {
let working_directory = canonicalize(working_directory).unwrap();
let port = pick_unused_port().unwrap();
let ip = local_ip().unwrap();
let run_args = RunArgs { port, router_ip: true };
chesedo marked this conversation as resolved.
Show resolved Hide resolved

let runner = Shuttle::new().unwrap().run(Args {
api_url: Some("http://shuttle.invalid:80".to_string()),
project_args: ProjectArgs {
working_directory: working_directory.clone(),
name: None,
},
cmd: Command::Run(run_args),
});

let working_directory_clone = working_directory.clone();

tokio::spawn(async move {
sleep(Duration::from_secs(600)).await;

println!(
"run test for '{}' took too long. Did it fail to shutdown?",
working_directory_clone.display()
);
exit(1);
});

tokio::spawn(runner);

// Wait for service to be responsive
while (reqwest::Client::new()
.get(format!("http://{ip}:{port}"))
.send()
.await)
.is_err()
{
println!(
"waiting for '{}' to start up...",
working_directory.display()
);
sleep(Duration::from_millis(350)).await;
}

port
}

#[tokio::test(flavor = "multi_thread")]
async fn rocket_hello_world_with_router_ip() {

let port = cargo_shuttle_run_with_router_ip("../examples/rocket/hello-world").await;
let ip = local_ip().unwrap();

let request_text = reqwest::Client::new()
.get(format!("http://{ip:?}:{port}/hello"))
.send()
.await
.unwrap()
.text()
.await
.unwrap();

assert_eq!(request_text, "Hello, world!");
}