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

fix: use additional headers on status request #105

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion linkup-cli/src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn check_local_not_started() -> Result<(), CliError> {
if service.local == service.remote {
continue;
}
if server_status(service.local.to_string()) == ServerStatus::Ok {
if server_status(service.local.to_string(), None) == ServerStatus::Ok {
println!("⚠️ Service {} is already running locally!! You need to restart it for linkup's environment variables to be loaded.", service.name);
}
}
Expand Down
50 changes: 39 additions & 11 deletions linkup-cli/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use colored::{ColoredString, Colorize};
use linkup::StorableDomain;
use linkup::{get_additional_headers, HeaderMap, StorableDomain, TargetService};
use serde::{Deserialize, Serialize};
use std::{thread, time::Duration};

use crate::{
local_config::{LocalState, ServiceTarget},
local_config::{LocalService, LocalState, ServiceTarget},
CliError, LINKUP_LOCALSERVER_PORT,
};

Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn status(json: bool, all: bool) -> Result<(), CliError> {

let (tx, rx) = std::sync::mpsc::channel();
linkup_status(tx.clone(), &state);
service_status(tx.clone(), &state);
services_status(tx.clone(), &state);

drop(tx);

Expand Down Expand Up @@ -170,7 +170,7 @@ fn linkup_status(tx: std::sync::mpsc::Sender<ServiceStatus>, state: &LocalState)
name: "local_server".to_string(),
component_kind: "linkup".to_string(),
location: local_url.clone(),
status: server_status(local_url),
status: server_status(local_url, None),
};

local_tx
Expand All @@ -186,7 +186,7 @@ fn linkup_status(tx: std::sync::mpsc::Sender<ServiceStatus>, state: &LocalState)
name: "remote_server".to_string(),
component_kind: "linkup".to_string(),
location: remote.clone(),
status: server_status(remote),
status: server_status(remote, None),
};

remote_tx
Expand All @@ -202,7 +202,7 @@ fn linkup_status(tx: std::sync::mpsc::Sender<ServiceStatus>, state: &LocalState)
name: "tunnel".to_string(),
component_kind: "linkup".to_string(),
location: tunnel.clone(),
status: server_status(tunnel),
status: server_status(tunnel, None),
};

tunnel_tx
Expand All @@ -211,9 +211,10 @@ fn linkup_status(tx: std::sync::mpsc::Sender<ServiceStatus>, state: &LocalState)
});
}

fn service_status(tx: std::sync::mpsc::Sender<ServiceStatus>, state: &LocalState) {
fn services_status(tx: std::sync::mpsc::Sender<ServiceStatus>, state: &LocalState) {
for service in state.services.iter().cloned() {
let tx = tx.clone();
let session_name = state.linkup.session_name.clone();

thread::spawn(move || {
let url = match service.current {
Expand All @@ -222,10 +223,10 @@ fn service_status(tx: std::sync::mpsc::Sender<ServiceStatus>, state: &LocalState
};

let service_status = ServiceStatus {
name: service.name,
name: service.name.clone(),
location: url.to_string(),
component_kind: service.current.to_string(),
status: server_status(url.to_string()),
status: service_status(&service, &session_name),
};

tx.send(service_status)
Expand All @@ -234,13 +235,40 @@ fn service_status(tx: std::sync::mpsc::Sender<ServiceStatus>, state: &LocalState
}
}

pub fn server_status(url: String) -> ServerStatus {
fn service_status(service: &LocalService, session_name: &str) -> ServerStatus {
let url = match service.current {
ServiceTarget::Local => service.local.clone(),
ServiceTarget::Remote => service.remote.clone(),
};

let headers = get_additional_headers(
url.as_ref(),
&HeaderMap::new(),
session_name,
&TargetService {
name: service.name.clone(),
url: url.to_string(),
},
);

server_status(url.to_string(), Some(headers))
}

pub fn server_status(url: String, extra_headers: Option<HeaderMap>) -> ServerStatus {
let client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(2))
.build();

match client {
Ok(client) => client.get(url).send().into(),
Ok(client) => {
let mut request = client.get(url);

if let Some(extra_headers) = extra_headers {
request = request.headers(extra_headers.into());
}

request.send().into()
}
Err(_) => ServerStatus::Error,
}
}
Loading