-
Notifications
You must be signed in to change notification settings - Fork 2
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
refactor: shuffle code and responsibility of LocalState #49
Changes from 7 commits
120a86b
913e3e9
7a38173
9b272bc
9de9c40
fef3dcf
ea2f792
28055f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,14 +11,13 @@ use crate::background_local_server::{ | |
}; | ||
use crate::background_tunnel::start_tunnel; | ||
use crate::local_config::{LocalState, ServiceTarget}; | ||
use crate::start::save_state; | ||
use crate::status::print_session_names; | ||
use crate::worker_client::WorkerClient; | ||
use crate::CliError; | ||
use crate::LINKUP_LOCALSERVER_PORT; | ||
use crate::{start::get_state, CliError}; | ||
|
||
pub fn boot_background_services() -> Result<(), CliError> { | ||
let mut state = get_state()?; | ||
let mut state = LocalState::load()?; | ||
|
||
let local_url = Url::parse(&format!("http://localhost:{}", LINKUP_LOCALSERVER_PORT)) | ||
.expect("linkup url invalid"); | ||
|
@@ -40,35 +39,34 @@ pub fn boot_background_services() -> Result<(), CliError> { | |
println!("Cloudflare tunnel was already running.. Try stopping linkup first if you have problems."); | ||
} | ||
|
||
let (local_server_conf, remote_server_conf) = server_config_from_state(&state); | ||
let server_config = ServerConfig::from(&state); | ||
|
||
let server_session_name = load_config( | ||
&state.linkup.remote, | ||
&state.linkup.session_name, | ||
remote_server_conf, | ||
server_config.remote, | ||
)?; | ||
let local_session_name = load_config(&local_url, &server_session_name, local_server_conf)?; | ||
let local_session_name = load_config(&local_url, &server_session_name, server_config.local)?; | ||
|
||
if server_session_name != local_session_name { | ||
return Err(CliError::InconsistentState); | ||
} | ||
|
||
let tunnel_url = state.linkup.tunnel.clone(); | ||
state.linkup.session_name = server_session_name; | ||
state.save()?; | ||
|
||
state.linkup.session_name = server_session_name.clone(); | ||
let state_to_print = state.clone(); | ||
|
||
save_state(state)?; | ||
|
||
println!("Waiting for tunnel to be ready at {}...", tunnel_url); | ||
println!( | ||
"Waiting for tunnel to be ready at {}...", | ||
&state.linkup.tunnel | ||
); | ||
|
||
// If the tunnel is checked too quickly, it dies ¯\_(ツ)_/¯ | ||
thread::sleep(Duration::from_millis(1000)); | ||
wait_till_ok(format!("{}linkup-check", tunnel_url))?; | ||
wait_till_ok(format!("{}linkup-check", &state.linkup.tunnel))?; | ||
|
||
println!(); | ||
|
||
print_session_names(&state_to_print); | ||
print_session_names(&state); | ||
|
||
Ok(()) | ||
} | ||
|
@@ -80,7 +78,7 @@ pub fn load_config( | |
) -> Result<String, CliError> { | ||
let session_update_req = UpdateSessionRequest { | ||
session_token: config.session_token, | ||
desired_name: desired_name.into(), | ||
desired_name: desired_name.to_string(), | ||
services: config.services, | ||
domains: config.domains, | ||
cache_routes: config.cache_routes, | ||
|
@@ -93,49 +91,66 @@ pub fn load_config( | |
Ok(content) | ||
} | ||
|
||
pub fn server_config_from_state(state: &LocalState) -> (StorableSession, StorableSession) { | ||
let local_server_services = state | ||
.services | ||
.iter() | ||
.map(|local_service| StorableService { | ||
name: local_service.name.clone(), | ||
location: if local_service.current == ServiceTarget::Remote { | ||
local_service.remote.clone() | ||
} else { | ||
local_service.local.clone() | ||
}, | ||
rewrites: Some(local_service.rewrites.clone()), | ||
}) | ||
.collect::<Vec<StorableService>>(); | ||
|
||
let remote_server_services = state | ||
.services | ||
.iter() | ||
.map(|local_service| StorableService { | ||
name: local_service.name.clone(), | ||
location: if local_service.current == ServiceTarget::Remote { | ||
local_service.remote.clone() | ||
} else { | ||
state.linkup.tunnel.clone() | ||
}, | ||
rewrites: Some(local_service.rewrites.clone()), | ||
}) | ||
.collect::<Vec<StorableService>>(); | ||
|
||
( | ||
StorableSession { | ||
pub struct ServerConfig { | ||
pub local: StorableSession, | ||
pub remote: StorableSession, | ||
} | ||
|
||
impl From<&LocalState> for ServerConfig { | ||
fn from(state: &LocalState) -> Self { | ||
let local_server_services = state | ||
.services | ||
.iter() | ||
.map(|local_service| StorableService { | ||
name: local_service.name.clone(), | ||
location: if local_service.current == ServiceTarget::Remote { | ||
local_service.remote.clone() | ||
} else { | ||
local_service.local.clone() | ||
}, | ||
rewrites: Some(local_service.rewrites.clone()), | ||
}) | ||
.collect::<Vec<StorableService>>(); | ||
|
||
let remote_server_services = state | ||
.services | ||
.iter() | ||
.map(|local_service| StorableService { | ||
name: local_service.name.clone(), | ||
location: if local_service.current == ServiceTarget::Remote { | ||
local_service.remote.clone() | ||
} else { | ||
state.linkup.tunnel.clone() | ||
}, | ||
rewrites: Some(local_service.rewrites.clone()), | ||
}) | ||
.collect::<Vec<StorableService>>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or switch local to remote in the remote case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point! Changed on 28055f6 |
||
|
||
let local_storable_session = StorableSession { | ||
session_token: state.linkup.session_token.clone(), | ||
services: local_server_services, | ||
domains: state.domains.clone(), | ||
cache_routes: state.linkup.cache_routes.clone(), | ||
}, | ||
StorableSession { | ||
}; | ||
|
||
let remote_storable_session = StorableSession { | ||
session_token: state.linkup.session_token.clone(), | ||
services: remote_server_services, | ||
domains: state.domains.clone(), | ||
cache_routes: state.linkup.cache_routes.clone(), | ||
}, | ||
) | ||
}; | ||
|
||
ServerConfig { | ||
local: local_storable_session, | ||
remote: remote_storable_session, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> From<&'a ServerConfig> for (&'a StorableSession, &'a StorableSession) { | ||
fn from(config: &'a ServerConfig) -> Self { | ||
(&config.local, &config.remote) | ||
} | ||
} | ||
|
||
pub fn wait_till_ok(url: String) -> Result<(), CliError> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok 👍 Cool!