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 RAILWAY_TOKEN not working with railway up --ci #498

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 1 addition & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ impl GQLClient {
let mut headers = HeaderMap::new();
if let Some(token) = &Configs::get_railway_token() {
headers.insert("project-access-token", HeaderValue::from_str(token)?);
} else if let Some(token) = &Configs::get_railway_api_token() {
headers.insert(
"authorization",
HeaderValue::from_str(&format!("Bearer {token}"))?,
);
} else if let Some(token) = &configs.root_config.user.token {
if token.is_empty() {
return Err(RailwayError::Unauthorized);
}
} else if let Some(token) = configs.get_railway_auth_token() {
headers.insert(
"authorization",
HeaderValue::from_str(&format!("Bearer {token}"))?,
Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ impl Configs {
std::env::var("RAILWAY_API_TOKEN").ok()
}

/// tries the environment variable and the config file
pub fn get_railway_auth_token(&self) -> Option<String> {
Self::get_railway_api_token().or(self
.root_config
.user
.token
.clone()
.filter(|t| !t.is_empty()))
}
pub fn get_environment_id() -> Environment {
match std::env::var("RAILWAY_ENV")
.map(|env| env.to_lowercase())
Expand Down
21 changes: 12 additions & 9 deletions src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ where
<T as GraphQLQuery>::ResponseData: std::fmt::Debug,
{
let configs = Configs::new()?;
let Some(token) = configs.root_config.user.token.clone() else {
bail!("Unauthorized. Please login with `railway login`")
};
let bearer = format!("Bearer {token}");
let hostname = configs.get_host();
let mut request = format!("wss://backboard.{hostname}/graphql/v2").into_client_request()?;

request.headers_mut().insert(
let headers = request.headers_mut();
if let Some(token) = &Configs::get_railway_token() {
headers.insert("project-access-token", HeaderValue::from_str(token)?);
} else if let Some(token) = configs.get_railway_auth_token() {
headers.insert(
"authorization",
HeaderValue::from_str(&format!("Bearer {token}"))?,
);
} else {
bail!("Not authorized");
}
headers.insert(
"Sec-WebSocket-Protocol",
HeaderValue::from_str("graphql-transport-ws").unwrap(),
);
request
.headers_mut()
.insert("Authorization", HeaderValue::from_str(&bearer)?);

let (connection, _) = async_tungstenite::tokio::connect_async(request).await?;

Expand Down
Loading