Skip to content

Commit

Permalink
Ensure tunnel working on start
Browse files Browse the repository at this point in the history
  • Loading branch information
ostenbom committed May 17, 2023
1 parent 14e9a4e commit 7882bec
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
42 changes: 40 additions & 2 deletions linkup-cli/src/check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use std::thread;


use reqwest::blocking::Client;
use reqwest::StatusCode;
Expand All @@ -19,11 +22,18 @@ use crate::{LINKUP_ENV_SEPARATOR, LINKUP_LOCALSERVER_PORT};
pub fn check() -> Result<(), CliError> {
let mut state = get_state()?;

let local_url = Url::parse(&format!("http://localhost:{}", LINKUP_LOCALSERVER_PORT))
.expect("linkup url invalid");

if is_local_server_started().is_err() {
println!("starting linkup local server...");
start_local_server()?;
}

wait_till_ok(format!("{}linkup-check", local_url))?;

if is_tunnel_started().is_err() {
println!("starting tunnel...");
let tunnel = start_tunnel()?;
state.linkup.tunnel = tunnel;
}
Expand All @@ -36,8 +46,6 @@ pub fn check() -> Result<(), CliError> {
}

let (local_server_conf, remote_server_conf) = server_config_from_state(&state);
let local_url = Url::parse(&format!("http://localhost:{}", LINKUP_LOCALSERVER_PORT))
.expect("linkup url invalid");

let server_session_name = load_config(
&state.linkup.remote,
Expand All @@ -50,11 +58,17 @@ pub fn check() -> Result<(), CliError> {
return Err(CliError::InconsistentState);
}

let tunnel_url = state.linkup.tunnel.clone();

state.linkup.session_name = server_session_name.clone();
save_state(state)?;

println!("{}", server_session_name);

// If the tunnel is checked too quickly, it dies ¯\_(ツ)_/¯
thread::sleep(Duration::from_millis(1000));
wait_till_ok(format!("{}linkup-check", tunnel_url))?;

// final checks services are responding
// print status

Expand Down Expand Up @@ -143,6 +157,30 @@ fn server_config_from_state(state: &LocalState) -> (StorableSession, StorableSes
)
}

pub fn wait_till_ok(url: String) -> Result<(), CliError> {
let client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(1))
.build()
.map_err(|err| CliError::StartLinkupTimeout(err.to_string()))?;

let start = Instant::now();
loop {
if start.elapsed() > Duration::from_secs(15) {
return Err(CliError::StartLinkupTimeout(format!("{} took too long to load", url)));
}

let response = client.get(&url).send();

if let Ok(resp) = response {
if resp.status() == StatusCode::OK {
return Ok(());
}
}

thread::sleep(Duration::from_millis(2000));
}
}

fn set_service_env(directory: String, config_path: String) -> Result<(), CliError> {
let config_dir = Path::new(&config_path).parent().ok_or_else(|| {
CliError::SetServiceEnv(
Expand Down
5 changes: 5 additions & 0 deletions linkup-cli/src/local_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ async fn convert_reqwest_response(
Ok(response_builder.body(body))
}

async fn always_ok() -> impl Responder {
HttpResponse::Ok().finish()
}

#[actix_web::main]
pub async fn local_linkup_main() -> io::Result<()> {
env_logger::Builder::new()
Expand All @@ -173,6 +177,7 @@ pub async fn local_linkup_main() -> io::Result<()> {
.app_data(string_store.clone()) // Add shared state
.wrap(middleware::Logger::default()) // Enable logger
.route("/linkup", web::post().to(linkup_config_handler))
.route("/linkup-check", web::route().to(always_ok))
.default_service(web::route().to(linkup_request_handler))
})
.bind(("127.0.0.1", LINKUP_LOCALSERVER_PORT))?
Expand Down
2 changes: 2 additions & 0 deletions linkup-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pub enum CliError {
StartLocalServer(String),
#[error("could not start local tunnel: {0}")]
StartLocalTunnel(String),
#[error("linkup component did not start in time: {0}")]
StartLinkupTimeout(String),
#[error("could not load config to {0}: {1}")]
LoadConfig(String, String),
#[error("could not stop: {0}")]
Expand Down

0 comments on commit 7882bec

Please sign in to comment.