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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sold on the name, but can't think of anything better either 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other things I can really think of are network or local_ip, but yeah to be honest I'm not huge on either of them and router_ip is not really that descriptive

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly tempted to use network or lan as the flag since router_ip or use_router_ip isn't technically accurate anymore, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I was thinking more like 'host' but don't like it. How about external?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that sounds like a good idea, I'll add that in then

}

#[derive(Parser, Debug)]
Expand Down
8 changes: 7 additions & 1 deletion cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,13 @@ impl Shuttle {
secrets,
working_directory.to_path_buf(),
)?;
let addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), run_args.port);
let addr = if run_args.router_ip {
std::net::IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))
} else {
Ipv4Addr::LOCALHOST.into()
};

let addr = SocketAddr::new(addr, run_args.port);

trace!("loading project");
println!(
Expand Down
64 changes: 47 additions & 17 deletions cargo-shuttle/tests/integration/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ use std::{fs::canonicalize, process::exit, time::Duration};
use tokio::time::sleep;

/// creates a `cargo-shuttle` run instance with some reasonable defaults set.
async fn cargo_shuttle_run(working_directory: &str) -> u16 {
async fn cargo_shuttle_run(working_directory: &str, router_ip: bool) -> u16 {
let working_directory = canonicalize(working_directory).unwrap();

let url = if !router_ip { "localhost" } else { "0.0.0.0" };
chesedo marked this conversation as resolved.
Show resolved Hide resolved

let port = pick_unused_port().unwrap();
let run_args = RunArgs { port };

let run_args = if !router_ip {
RunArgs {
port,
router_ip: false,
}
} else {
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()),
Expand All @@ -35,7 +49,7 @@ async fn cargo_shuttle_run(working_directory: &str) -> u16 {

// Wait for service to be responsive
while (reqwest::Client::new()
.get(format!("http://localhost:{port}"))
.get(format!("http://{url}:{port}"))
.send()
.await)
.is_err()
Expand All @@ -52,7 +66,7 @@ async fn cargo_shuttle_run(working_directory: &str) -> u16 {

#[tokio::test(flavor = "multi_thread")]
async fn rocket_hello_world() {
let port = cargo_shuttle_run("../examples/rocket/hello-world").await;
let port = cargo_shuttle_run("../examples/rocket/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -68,7 +82,7 @@ async fn rocket_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn rocket_secrets() {
let port = cargo_shuttle_run("../examples/rocket/secrets").await;
let port = cargo_shuttle_run("../examples/rocket/secrets", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/secret"))
Expand All @@ -85,7 +99,7 @@ async fn rocket_secrets() {
// This example uses a shared Postgres. Thus local runs should create a docker container for it.
#[tokio::test(flavor = "multi_thread")]
async fn rocket_postgres() {
let port = cargo_shuttle_run("../examples/rocket/postgres").await;
let port = cargo_shuttle_run("../examples/rocket/postgres", false).await;
let client = reqwest::Client::new();

let post_text = client
Expand Down Expand Up @@ -114,7 +128,7 @@ async fn rocket_postgres() {

#[tokio::test(flavor = "multi_thread")]
async fn rocket_authentication() {
let port = cargo_shuttle_run("../examples/rocket/authentication").await;
let port = cargo_shuttle_run("../examples/rocket/authentication", false).await;
let client = reqwest::Client::new();

let public_text = client
Expand Down Expand Up @@ -170,7 +184,7 @@ async fn rocket_authentication() {

#[tokio::test(flavor = "multi_thread")]
async fn actix_web_hello_world() {
let port = cargo_shuttle_run("../examples/actix-web/hello-world").await;
let port = cargo_shuttle_run("../examples/actix-web/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -186,7 +200,7 @@ async fn actix_web_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn axum_hello_world() {
let port = cargo_shuttle_run("../examples/axum/hello-world").await;
let port = cargo_shuttle_run("../examples/axum/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -202,7 +216,7 @@ async fn axum_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn tide_hello_world() {
let port = cargo_shuttle_run("../examples/tide/hello-world").await;
let port = cargo_shuttle_run("../examples/tide/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -218,7 +232,7 @@ async fn tide_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn tower_hello_world() {
let port = cargo_shuttle_run("../examples/tower/hello-world").await;
let port = cargo_shuttle_run("../examples/tower/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -234,7 +248,7 @@ async fn tower_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn warp_hello_world() {
let port = cargo_shuttle_run("../examples/warp/hello-world").await;
let port = cargo_shuttle_run("../examples/warp/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -250,7 +264,7 @@ async fn warp_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn poem_hello_world() {
let port = cargo_shuttle_run("../examples/poem/hello-world").await;
let port = cargo_shuttle_run("../examples/poem/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -267,7 +281,7 @@ async fn poem_hello_world() {
// This example uses a shared Postgres. Thus local runs should create a docker container for it.
#[tokio::test(flavor = "multi_thread")]
async fn poem_postgres() {
let port = cargo_shuttle_run("../examples/poem/postgres").await;
let port = cargo_shuttle_run("../examples/poem/postgres", false).await;
let client = reqwest::Client::new();

let post_text = client
Expand Down Expand Up @@ -298,7 +312,7 @@ async fn poem_postgres() {
// This example uses a shared MongoDb. Thus local runs should create a docker container for it.
#[tokio::test(flavor = "multi_thread")]
async fn poem_mongodb() {
let port = cargo_shuttle_run("../examples/poem/mongodb").await;
let port = cargo_shuttle_run("../examples/poem/mongodb", false).await;
let client = reqwest::Client::new();

// Post a todo note and get the persisted todo objectId
Expand Down Expand Up @@ -330,7 +344,7 @@ async fn poem_mongodb() {

#[tokio::test(flavor = "multi_thread")]
async fn salvo_hello_world() {
let port = cargo_shuttle_run("../examples/salvo/hello-world").await;
let port = cargo_shuttle_run("../examples/salvo/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -346,7 +360,7 @@ async fn salvo_hello_world() {

#[tokio::test(flavor = "multi_thread")]
async fn thruster_hello_world() {
let port = cargo_shuttle_run("../examples/thruster/hello-world").await;
let port = cargo_shuttle_run("../examples/thruster/hello-world", false).await;

let request_text = reqwest::Client::new()
.get(format!("http://localhost:{port}/hello"))
Expand All @@ -359,3 +373,19 @@ async fn thruster_hello_world() {

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

#[tokio::test(flavor = "multi_thread")]
async fn rocket_hello_world_with_router_ip() {
let port = cargo_shuttle_run("../examples/rocket/hello-world", true).await;

let request_text = reqwest::Client::new()
.get(format!("http://0.0.0.0:{port}/hello"))
chesedo marked this conversation as resolved.
Show resolved Hide resolved
.send()
.await
.unwrap()
.text()
.await
.unwrap();

assert_eq!(request_text, "Hello, world!");
}
2 changes: 1 addition & 1 deletion service/tests/integration/helpers/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ pub fn build_so_create_loader(resources: &str, crate_name: &str) -> Result<Loade

let so_path = crate_dir.join("target/release").join(lib_name);

Loader::from_so_file(&so_path)
Loader::from_so_file(so_path)
}