Skip to content

Commit 1f534b4

Browse files
committed
fix: added connection retry for site-id
1 parent ad42110 commit 1f534b4

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

backend/src/lib.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,9 @@ impl Environment {
5353
};
5454

5555
let unifi_has_valid_cert: bool = match env::var("UNIFI_HAS_VALID_CERT") {
56-
Ok(val) => match val.trim().to_lowercase().as_str() {
57-
"true" | "1" | "yes" => true,
58-
"false" | "0" | "no" => false,
59-
_ => {
60-
return Err("Invalid UNIFI_HAS_VALID_CERT, must be true/false".to_string());
61-
}
62-
},
56+
Ok(val) => {
57+
Self::parse_bool(&val).map_err(|e| format!("Invalid UNIFI_HAS_VALID_CERT: {e}"))?
58+
}
6359
Err(_) => true,
6460
};
6561

@@ -90,4 +86,12 @@ impl Environment {
9086
timezone,
9187
})
9288
}
89+
90+
fn parse_bool(s: &str) -> Result<bool, String> {
91+
match s.trim().to_lowercase().as_str() {
92+
"true" | "1" | "yes" => Ok(true),
93+
"false" | "0" | "no" => Ok(false),
94+
_ => Err(format!("Boolean value must be true or false, found: {s}")),
95+
}
96+
}
9397
}

backend/src/main.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use backend::{
99
unifi_api::{UNIFI_API, UnifiAPI},
1010
};
1111
use tower_http::cors::{Any, CorsLayer};
12-
use tracing::{error, info, level_filters::LevelFilter};
12+
use tracing::{error, info, level_filters::LevelFilter, warn};
1313
use tracing_subscriber::EnvFilter;
1414

1515
#[tokio::main]
@@ -35,14 +35,20 @@ async fn main() {
3535
.set(env)
3636
.expect("Failed to set environment variables");
3737

38-
let unifi_api = match UnifiAPI::new().await {
39-
Ok(api) => api,
40-
Err(_) => {
41-
error!("Failed to initialize UnifiAPI wrapper");
42-
std::process::exit(1);
38+
loop {
39+
match UnifiAPI::try_new().await {
40+
Ok(api) => {
41+
UNIFI_API.set(api).expect("Failed to set UnifiAPI");
42+
info!("Successfully connected to Unifi controller");
43+
break;
44+
}
45+
Err(e) => {
46+
error!("Failed to initialize UnifiAPI wrapper: {}", e);
47+
warn!("Retrying connection in 5 seconds...");
48+
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
49+
}
4350
}
44-
};
45-
UNIFI_API.set(unifi_api).expect("Failed to set UnifiAPI");
51+
}
4652

4753
let cors = CorsLayer::new()
4854
.allow_headers([http::header::CONTENT_TYPE])

backend/src/unifi_api.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use axum::http::HeaderValue;
12
use chrono::DateTime;
23
use chrono_tz::Tz;
34
use reqwest::{Client, ClientBuilder, StatusCode};
@@ -32,20 +33,18 @@ pub struct UnifiAPI<'a> {
3233
}
3334

3435
impl<'a> UnifiAPI<'a> {
35-
pub async fn new() -> Result<Self, ()> {
36+
pub async fn try_new() -> Result<Self, String> {
3637
let environment: &Environment = ENVIRONMENT.get().expect("Environment not set");
3738

3839
let mut headers = reqwest::header::HeaderMap::with_capacity(2);
3940
headers.insert(
4041
reqwest::header::CONTENT_TYPE,
41-
reqwest::header::HeaderValue::from_static("application/json"),
42+
HeaderValue::from_static("application/json"),
4243
);
4344
headers.insert(
4445
"X-API-Key",
45-
environment
46-
.unifi_api_key
47-
.parse()
48-
.expect("Could not parse API Key"),
46+
HeaderValue::from_str(&environment.unifi_api_key)
47+
.map_err(|e| format!("Failed to set X-API-Key header: {e}"))?,
4948
);
5049

5150
let client = ClientBuilder::new()
@@ -70,8 +69,7 @@ impl<'a> UnifiAPI<'a> {
7069
let id = match unifi_api.get_default_site_id().await {
7170
Ok(id) => id,
7271
Err(e) => {
73-
error!("Failed to fetch default site ID: {}", e);
74-
return Err(());
72+
return Err(format!("Failed to fetch default site ID: {e}"));
7573
}
7674
};
7775
info!("Default site ID found: {}", id);

0 commit comments

Comments
 (0)