@@ -2,14 +2,13 @@ use hyper::http::StatusCode;
22use reqwest:: { Method , RequestBuilder , Result } ;
33use serde:: { Deserialize , Serialize } ;
44
5- use crate :: config :: BalancerConfig ;
5+ use crate :: AppState ;
66
77use super :: config:: Config ;
88use super :: service:: { AgentServiceRegister , ServiceCheck } ;
99
1010pub 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
3332impl 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 } ;
0 commit comments