Skip to content

Commit

Permalink
fix(indexer): implement code review comments from tari-project#1303
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Feb 26, 2025
1 parent 86f378e commit effc9fd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
7 changes: 5 additions & 2 deletions applications/tari_indexer/src/http_ui/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ const LOG_TARGET: &str = "tari::indexer::web_ui::server";
pub async fn run_http_ui_server(
address: SocketAddr,
json_rpc_address: Url,
graphql_address: Url,
graphql_address: Option<Url>,
) -> Result<(), anyhow::Error> {
let json_rpc_address = Arc::new(json_rpc_address);

let router = Router::new()
.route("/json_rpc_address", get(|| async move { json_rpc_address.to_string() }))
.route("/graphql_address", get(|| async move { graphql_address.to_string() }))
.route(
"/graphql_address",
get(|| async move { graphql_address.map(|a| a.to_string()).unwrap_or_default() }),
)
.fallback(handler);

info!(target: LOG_TARGET, "🕸️ HTTP UI started at {}", address);
Expand Down
40 changes: 23 additions & 17 deletions applications/tari_indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub async fn run_indexer(config: ApplicationConfig, mut shutdown_signal: Shutdow

// Run the JSON-RPC API
let jrpc_address = config.indexer.json_rpc_address;
if let (Some(jrpc_address), Some(graphql_address)) = (jrpc_address, graphql_address) {
if let Some(jrpc_address) = jrpc_address {
info!(target: LOG_TARGET, "🌐 Started JSON-RPC server on {}", jrpc_address);
let handlers = JsonRpcHandlers::new(
&services,
Expand All @@ -166,28 +166,34 @@ pub async fn run_indexer(config: ApplicationConfig, mut shutdown_signal: Shutdow
// Run the http ui
if let Some(address) = config.indexer.web_ui_address {
// json rpc
let mut public_jrpc_address = config
let public_jrpc_url = config
.indexer
.web_ui_public_json_rpc_url
.unwrap_or_else(|| jrpc_address.to_string());
if !public_jrpc_address.starts_with("http://") && !public_jrpc_address.starts_with("https://") {
public_jrpc_address = format!("http://{}", public_jrpc_address);
}
let public_jrpc_address = url::Url::parse(&public_jrpc_address)
.map_err(|err| ExitError::new(ExitCode::ConfigError, format!("Invalid public JSON-rpc url: {err}")))?;
.unwrap_or_else(|| format!("http://{jrpc_address}"));
let public_jrpc_address = url::Url::parse(&public_jrpc_url).map_err(|err| {
ExitError::new(
ExitCode::ConfigError,
format!("Invalid public JSON-rpc url '{public_jrpc_url}': {err}"),
)
})?;

// graphql
let mut public_graphql_address = config
let public_graphql_url = config
.indexer
.web_ui_public_graphql_url
.unwrap_or_else(|| graphql_address.to_string());
if !public_graphql_address.starts_with("http://") && !public_graphql_address.starts_with("https://") {
public_graphql_address = format!("http://{}", public_graphql_address);
}
let public_graphql_address = url::Url::parse(&public_graphql_address)
.map_err(|err| ExitError::new(ExitCode::ConfigError, format!("Invalid public GraphQL url: {err}")))?;

task::spawn(run_http_ui_server(address, public_jrpc_address, public_graphql_address));
.filter(|_| graphql_address.is_some())
.or_else(|| graphql_address.map(|a| format!("http://{a}")))
.map(|addr| {
url::Url::parse(&addr).map_err(|err| {
ExitError::new(
ExitCode::ConfigError,
format!("Invalid public GraphQL url '{addr}': {err}"),
)
})
})
.transpose()?;

task::spawn(run_http_ui_server(address, public_jrpc_address, public_graphql_url));
}
}

Expand Down
2 changes: 1 addition & 1 deletion applications/tari_indexer_web_ui/src/utils/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function getGraphQLAddress(): Promise<URL> {
try {
return new URL(url);
} catch (e) {
throw new Error(`Invalid URL: ${url} : {e}`);
throw new Error(`Invalid URL: ${url} : ${e}`);
}
}
} catch (e) {
Expand Down
19 changes: 14 additions & 5 deletions applications/tari_swarm_daemon/src/process_manager/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ pub struct InstanceInfo {
impl InstanceInfo {
pub fn get_public_web_url(&self) -> Url {
match self.settings.get("public_web_url") {
Some(url) => url.parse().expect("Invalid web URL"),
Some(url) => url
.parse()
.expect("Invalid web URL. Please check `public_web_url` in your config."),
None => {
let public_ip = self
.settings
Expand All @@ -100,7 +102,9 @@ impl InstanceInfo {

pub fn get_public_json_rpc_url(&self) -> Url {
match self.settings.get("public_json_rpc_url") {
Some(url) => url.parse().expect("Invalid JSON RPC URL"),
Some(url) => url
.parse()
.expect("Invalid JSON RPC URL. Please check `public_json_rpc_url` in your config."),
None => {
let public_ip = self
.settings
Expand All @@ -117,17 +121,22 @@ impl InstanceInfo {

pub fn get_public_graphql_url(&self) -> Url {
match self.settings.get("public_graphql_url") {
Some(url) => url.parse().expect("Invalid JSON RPC URL"),
Some(url) => url
.parse()
.expect("Invalid GraphQL URL. Please check `public_graphql_url` in your config."),
None => {
let public_ip = self
.settings
.get("public_ip")
.map(|s| s.as_str())
.unwrap_or("127.0.0.1");
let web_port = self.ports.get("jrpc").expect("jrpc port not found");
let web_port = self
.ports
.get("graphql")
.expect("Graphql port must be allocated before calling get_graphql_url");
format!("http://{public_ip}:{web_port}/json_rpc")
.parse()
.expect("Invalid web URL")
.expect("Invalid GraphQL URL")
},
}
}
Expand Down

0 comments on commit effc9fd

Please sign in to comment.