Skip to content

Commit dcd9025

Browse files
committed
refactor: introduce NetworkConfigService pattern
- Rename network_config.rs to network.rs for consistency - Wrap network configuration functions in NetworkConfigService struct - Apply consistent service pattern with static methods - Update all imports to use NetworkConfigService - Add conditional compilation for mock feature in main.rs This refactoring improves code organization by applying the same service pattern used throughout the codebase. Signed-off-by: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com>
1 parent b12ba17 commit dcd9025

File tree

6 files changed

+366
-326
lines changed

6 files changed

+366
-326
lines changed

src/api.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
},
66
keycloak_client::SingleSignOnProvider,
77
middleware::TOKEN_EXPIRE_HOURS,
8-
network_config::{self, NetworkConfig},
8+
network::{NetworkConfig, NetworkConfigService},
99
omnect_device_service_client::{DeviceServiceClient, FactoryReset, LoadUpdate, RunUpdate},
1010
};
1111
use actix_files::NamedFile;
@@ -163,7 +163,7 @@ where
163163
pub async fn token(session: Session) -> impl Responder {
164164
debug!("token() called");
165165

166-
network_config::cancel_rollback();
166+
NetworkConfigService::cancel_rollback();
167167
Self::session_token(session)
168168
}
169169

@@ -304,10 +304,10 @@ where
304304
}
305305

306306
if let Err(e) =
307-
network_config::apply_network_config(&api.service_client, &network_config).await
307+
NetworkConfigService::apply_network_config(&api.service_client, &network_config).await
308308
{
309309
error!("set_network_config() failed: {e:#}");
310-
if let Err(err) = network_config::rollback_network_config(&network_config) {
310+
if let Err(err) = NetworkConfigService::rollback_network_config(&network_config) {
311311
error!("Failed to restore network config: {err:#}");
312312
}
313313
return HttpResponse::InternalServerError().body(format!("{e:#}"));

src/certificate.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![cfg_attr(feature = "mock", allow(dead_code, unused_imports))]
22

33
use crate::{
4-
common::handle_http_response,
5-
http_client::HttpClientFactory,
6-
omnect_device_service_client::{DeviceServiceClient},
4+
common::handle_http_response, http_client::HttpClientFactory,
5+
omnect_device_service_client::DeviceServiceClient,
76
};
87
use anyhow::{Context, Result};
98
use log::info;
@@ -42,7 +41,10 @@ pub fn key_path() -> String {
4241
}
4342

4443
#[cfg(feature = "mock")]
45-
pub async fn create_module_certificate() -> Result<()> {
44+
pub async fn create_module_certificate<T>(_service_client: &T) -> Result<()>
45+
where
46+
T: DeviceServiceClient,
47+
{
4648
Ok(())
4749
}
4850

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ pub mod common;
55
pub mod http_client;
66
pub mod keycloak_client;
77
pub mod middleware;
8-
pub mod network_config;
8+
pub mod network;
99
pub mod omnect_device_service_client;

src/main.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ mod common;
55
mod http_client;
66
mod keycloak_client;
77
mod middleware;
8-
mod network_config;
8+
mod network;
99
mod omnect_device_service_client;
1010

1111
use crate::{
1212
api::Api,
1313
certificate::create_module_certificate,
1414
common::{centrifugo_config, config_path},
1515
keycloak_client::KeycloakProvider,
16-
network_config::init_server_restart_channel,
16+
network::NetworkConfigService,
1717
omnect_device_service_client::{DeviceServiceClient, OmnectDeviceServiceClient},
1818
};
1919
use actix_cors::Cors;
@@ -74,7 +74,7 @@ async fn main() {
7474

7575
// Create restart signal channel
7676
let (restart_tx, mut restart_rx) = broadcast::channel(1);
77-
init_server_restart_channel(restart_tx).expect("failed to set restart channel");
77+
NetworkConfigService::init_server_restart_channel(restart_tx).expect("failed to set restart channel");
7878

7979
let mut sigterm = signal(SignalKind::terminate()).expect("failed to install SIGTERM handler");
8080

@@ -177,14 +177,10 @@ async fn run_server() -> (
177177
.await
178178
.expect("failed to create module certificate");
179179

180-
tokio::spawn({
181-
let service_client = service_client.clone();
182-
async move {
183-
if let Err(e) = network_config::process_pending_rollback(&service_client).await {
184-
error!("failed to check pending rollback: {e:#}");
185-
}
186-
}
187-
});
180+
if let Err(e) = network::NetworkConfigService::process_pending_rollback(&service_client).await
181+
{
182+
error!("failed to check pending rollback: {e:#}");
183+
}
188184

189185
let mut tls_certs = std::io::BufReader::new(
190186
std::fs::File::open(certificate::cert_path()).expect("failed to read certificate file"),

0 commit comments

Comments
 (0)