1- use anyhow :: { Context , Result } ;
1+ use crate :: error :: FlagdError ;
22use std:: str:: FromStr ;
33use tonic:: transport:: { Endpoint , Uri } ;
44use tracing:: debug;
55
66pub struct UpstreamConfig {
77 endpoint : Endpoint ,
8- authority : Uri ,
8+ authority : Option < String > , // Only set for custom name resolution (envoy://)
99}
1010
1111impl UpstreamConfig {
12- pub fn new ( target : String , is_in_process : bool ) -> Result < Self > {
12+ pub fn new ( target : String , is_in_process : bool ) -> Result < Self , FlagdError > {
1313 debug ! ( "Creating upstream config for target: {}" , target) ;
1414
1515 if target. starts_with ( "http://" ) {
1616 debug ! ( "Target is already an HTTP endpoint" ) ;
17- let uri = Uri :: from_str ( & target) ? ;
18- let endpoint = Endpoint :: from_shared ( target ) ?;
17+ let endpoint = Endpoint :: from_shared ( target)
18+ . map_err ( |e| FlagdError :: Config ( format ! ( "Invalid endpoint: {}" , e ) ) ) ?;
1919 return Ok ( Self {
2020 endpoint,
21- authority : uri
22- . authority ( )
23- . map ( |a| a. as_str ( ) )
24- . unwrap_or_default ( )
25- . parse ( ) ?,
21+ authority : None , // Standard HTTP doesn't need custom authority
2622 } ) ;
2723 }
2824
2925 let ( endpoint_str, authority) = if target. starts_with ( "envoy://" ) {
30- let uri = Uri :: from_str ( & target) . context ( "Failed to parse target URI" ) ?;
26+ let uri = Uri :: from_str ( & target)
27+ . map_err ( |e| FlagdError :: Config ( format ! ( "Failed to parse target URI: {}" , e) ) ) ?;
3128 let authority = uri. path ( ) . trim_start_matches ( '/' ) ;
3229
3330 if authority. is_empty ( ) {
34- return Err ( anyhow:: anyhow!( "Service name (authority) cannot be empty" ) ) ;
31+ return Err ( FlagdError :: Config (
32+ "Service name (authority) cannot be empty" . to_string ( ) ,
33+ ) ) ;
3534 }
3635
3736 let host = uri. host ( ) . unwrap_or ( "localhost" ) ;
3837 let port = uri. port_u16 ( ) . unwrap_or ( 9211 ) ; // Use Envoy port directly
3938
40- ( format ! ( "http://{}:{}" , host, port) , authority. to_string ( ) )
39+ (
40+ format ! ( "http://{}:{}" , host, port) ,
41+ Some ( authority. to_string ( ) ) ,
42+ )
4143 } else {
4244 let parts: Vec < & str > = target. split ( ':' ) . collect ( ) ;
4345 let host = parts. first ( ) . unwrap_or ( & "localhost" ) . to_string ( ) ;
@@ -47,24 +49,23 @@ impl UpstreamConfig {
4749 . unwrap_or ( if is_in_process { 8015 } else { 8013 } ) ;
4850
4951 debug ! ( "Using standard resolution with {}:{}" , host, port) ;
50- ( format ! ( "http://{}:{}" , host, port) , host )
52+ ( format ! ( "http://{}:{}" , host, port) , None ) // Standard resolution doesn't need custom authority
5153 } ;
5254
53- let endpoint = Endpoint :: from_shared ( endpoint_str) ?;
54- let authority_uri =
55- Uri :: from_str ( authority. as_str ( ) ) . context ( "Failed to parse authority" ) ?;
55+ let endpoint = Endpoint :: from_shared ( endpoint_str)
56+ . map_err ( |e| FlagdError :: Config ( format ! ( "Invalid endpoint: {}" , e) ) ) ?;
5657
5758 Ok ( Self {
5859 endpoint,
59- authority : authority_uri ,
60+ authority,
6061 } )
6162 }
6263
6364 pub fn endpoint ( & self ) -> & Endpoint {
6465 & self . endpoint
6566 }
6667
67- pub fn authority ( & self ) -> & Uri {
68- & self . authority
68+ pub fn authority ( & self ) -> Option < String > {
69+ self . authority . clone ( )
6970 }
7071}
0 commit comments