Skip to content

Commit 5c104af

Browse files
committed
Tidy up and don't clone as much
1 parent 780ee78 commit 5c104af

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

src/check.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ pub async fn start_status_checks(state: super::AppState) {
4242
loop {
4343
tokio::select! {
4444
_ = ticker.tick() => {
45-
check_status(state.clone(), &http_client).await;
45+
check_status(&state, &http_client).await;
4646
}
4747
}
4848
}
4949
}
5050

51-
pub async fn check_status(state: super::AppState, client: &reqwest::Client) {
51+
pub async fn check_status(state: &super::AppState, client: &reqwest::Client) {
5252
let service_response = get_service_tags(&state).await;
5353

5454
let is_active_service = is_active_service(&service_response).await;
@@ -66,7 +66,7 @@ pub async fn check_status(state: super::AppState, client: &reqwest::Client) {
6666
println!("Changing service state");
6767
match state
6868
.consul
69-
.register_service(is_active_host, is_eligible_host)
69+
.register_service(state, is_active_host, is_eligible_host)
7070
.await
7171
{
7272
Result::Ok(_) => println!("Success!"),
@@ -78,7 +78,7 @@ pub async fn check_status(state: super::AppState, client: &reqwest::Client) {
7878
pub async fn get_service_tags(state: &super::AppState) -> ServiceResponse {
7979
state
8080
.consul
81-
.get_self()
81+
.get_self(state)
8282
.await
8383
.unwrap_or(ServiceResponse { tags: Vec::new() })
8484
}

src/consul/client.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use hyper::http::StatusCode;
22
use reqwest::{Method, RequestBuilder, Result};
33
use serde::{Deserialize, Serialize};
44

5-
use crate::config::BalancerConfig;
5+
use crate::AppState;
66

77
use super::config::Config;
88
use super::service::{AgentServiceRegister, ServiceCheck};
99

1010
pub struct Consul {
1111
config: Config,
12-
app_config: BalancerConfig,
1312
http_client: reqwest::Client,
1413
}
1514

@@ -31,11 +30,10 @@ pub struct ServiceResponse {
3130
}
3231

3332
impl Consul {
34-
pub fn new(config: Config, app_config: BalancerConfig) -> Self {
33+
pub fn new(config: Config) -> Self {
3534
let http_client = reqwest::Client::new();
3635
Self {
3736
config,
38-
app_config,
3937
http_client,
4038
}
4139
}
@@ -66,9 +64,9 @@ impl Consul {
6664
Ok(res)
6765
}
6866

69-
pub async fn get_self(&self) -> Result<ServiceResponse> {
70-
let hostname = &self.app_config.hostname;
71-
let service_name = &self.app_config.consul.service_name;
67+
pub async fn get_self(&self, state: &AppState) -> Result<ServiceResponse> {
68+
let hostname = &state.app_config.hostname;
69+
let service_name = &state.app_config.consul.service_name;
7270

7371
let res = self
7472
.make_request(
@@ -82,9 +80,14 @@ impl Consul {
8280
Ok(res)
8381
}
8482

85-
pub async fn register_service(&self, active: bool, eligible: bool) -> Result<StatusCode> {
86-
let hostname = &self.app_config.hostname;
87-
let service_name = &self.app_config.consul.service_name;
83+
pub async fn register_service(
84+
&self,
85+
state: &AppState,
86+
active: bool,
87+
eligible: bool,
88+
) -> Result<StatusCode> {
89+
let hostname = &state.app_config.hostname;
90+
let service_name = &state.app_config.consul.service_name;
8891

8992
let mut tags: Vec<String> = Vec::from(["live".into()]);
9093

@@ -100,8 +103,8 @@ impl Consul {
100103
tags.push("ineligible".into());
101104
}
102105

103-
let service_address = &self.app_config.consul.service_address;
104-
let service_port = &self.app_config.http.port;
106+
let service_address = &state.app_config.consul.service_address;
107+
let service_port = &state.app_config.http.port;
105108

106109
let http_check = ServiceCheck {
107110
http: format!("http://{service_address}:{service_port}/healthz").into(),
@@ -112,8 +115,8 @@ impl Consul {
112115
let service = AgentServiceRegister {
113116
name: service_name.into(),
114117
id: Some(format!("{service_name}/{hostname}").into()),
115-
address: Some((&self.app_config.consul.service_address).into()),
116-
port: Some(self.app_config.http.port),
118+
address: Some((&state.app_config.consul.service_address).into()),
119+
port: Some(state.app_config.http.port),
117120
tags,
118121
checks: Vec::from([http_check]),
119122
};

src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ async fn main() {
3737
address: app_config.consul.agent_url.clone(),
3838
};
3939

40-
let consul = Consul::new(consul_config, app_config.clone());
41-
42-
let _ = consul.register_service(false, false).await;
40+
let consul = Consul::new(consul_config);
4341

4442
let state = AppState {
4543
consul: Arc::new(consul),
4644
app_config: Arc::new(app_config),
4745
};
4846

47+
let _ = state.consul.register_service(&state, false, false).await;
48+
4949
tokio::spawn(check::start_status_checks(state.clone()));
5050

5151
// build our application with a single route
@@ -56,9 +56,11 @@ async fn main() {
5656
.route("/status", get(get_eligibility))
5757
.with_state(state.clone());
5858

59-
println!("> Starting HTTP server on port 3000");
59+
let port = &state.app_config.http.port;
60+
61+
println!("> Starting HTTP server on port {port}");
6062
// run our app with hyper, listening globally on port 3000
61-
let listener = util::get_http_server(state.clone()).await;
63+
let listener = util::get_http_server(&state).await;
6264
axum::serve(listener, app).await.unwrap();
6365
}
6466

src/util.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
use std::sync::Arc;
2-
31
use axum::Json;
42
use hyper::StatusCode;
53
use serde_json::json;
64

7-
use crate::{config::BalancerConfig, AppState};
5+
use crate::AppState;
86

9-
pub async fn get_http_server(state: AppState) -> tokio::net::TcpListener {
10-
let listen_address = get_listen_host(state.app_config);
7+
pub async fn get_http_server(state: &AppState) -> tokio::net::TcpListener {
8+
let listen_address = get_listen_host(state);
119
return tokio::net::TcpListener::bind(listen_address).await.unwrap();
1210
}
1311

14-
pub fn get_listen_host(config: Arc<BalancerConfig>) -> String {
15-
let listen_address = &config.http.address;
16-
let port = &config.http.port;
12+
pub fn get_listen_host(state: &AppState) -> String {
13+
let listen_address = &state.app_config.http.address;
14+
let port = &state.app_config.http.port;
1715
format!("{listen_address}:{port}")
1816
}
1917

0 commit comments

Comments
 (0)