Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(torii): update cli to match katana's #2672

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 71 additions & 24 deletions bin/torii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! for more info.

use std::cmp;
use std::net::SocketAddr;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
Expand All @@ -21,8 +21,9 @@
use clap::{ArgAction, CommandFactory, FromArgMatches, Parser};
use clap_config::ClapConfig;
use dojo_metrics::exporters::prometheus::PrometheusRecorder;
use dojo_utils::parse::{parse_socket_address, parse_url};
use dojo_utils::parse::parse_url;
use dojo_world::contracts::world::WorldContractReader;
use serde::{Deserialize, Serialize};
use sqlx::sqlite::{
SqliteAutoVacuum, SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous,
};
Expand Down Expand Up @@ -60,15 +61,14 @@
#[arg(long, value_name = "URL", default_value = ":5050", value_parser = parse_url)]
rpc: Url,

#[command(flatten)]
server: ServerOptions,

/// Database filepath (ex: indexer.db). If specified file doesn't exist, it will be
/// created. Defaults to in-memory database
#[arg(short, long, default_value = "")]
database: String,

/// Address to serve api endpoints at.
#[arg(long, value_name = "SOCKET", default_value = "0.0.0.0:8080", value_parser = parse_socket_address)]
addr: SocketAddr,

/// Port to serve Libp2p TCP & UDP Quic transports
#[arg(long, value_name = "PORT", default_value = "9090")]
relay_port: u16,
Expand All @@ -90,21 +90,13 @@
#[arg(long, value_name = "PATH")]
relay_cert_path: Option<String>,

/// Specify allowed origins for api endpoints (comma-separated list of allowed origins, or "*"
/// for all)
#[arg(long, value_delimiter = ',')]
allowed_origins: Vec<String>,

/// The external url of the server, used for configuring the GraphQL Playground in a hosted
/// environment
#[arg(long, value_parser = parse_url)]
external_url: Option<Url>,

/// Enable Prometheus metrics.
///
/// The metrics will be served at the given interface and port.
#[arg(long, value_name = "SOCKET", value_parser = parse_socket_address, help_heading = "Metrics")]
metrics: Option<SocketAddr>,
#[command(flatten)]
metrics: MetricsOptions,

/// Open World Explorer on the browser.
#[arg(long)]
Expand Down Expand Up @@ -153,6 +145,56 @@
config: Option<PathBuf>,
}

/// Metrics server default address.
const DEFAULT_METRICS_ADDR: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
/// Torii metrics server default port.
const DEFAULT_METRICS_PORT: u16 = 9200;

#[derive(Debug, clap::Args, Clone, Serialize, Deserialize)]

Check warning on line 153 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L153

Added line #L153 was not covered by tests
#[command(next_help_heading = "Metrics options")]
struct MetricsOptions {
/// Enable metrics.
///
/// For now, metrics will still be collected even if this flag is not set. This only
/// controls whether the metrics server is started or not.
#[arg(long)]
metrics: bool,

Check warning on line 161 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L161

Added line #L161 was not covered by tests

/// The metrics will be served at the given address.
#[arg(requires = "metrics")]
#[arg(long = "metrics.addr", value_name = "ADDRESS")]
#[arg(default_value_t = DEFAULT_METRICS_ADDR)]
metrics_addr: IpAddr,

Check warning on line 167 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L166-L167

Added lines #L166 - L167 were not covered by tests

/// The metrics will be served at the given port.
#[arg(requires = "metrics")]
#[arg(long = "metrics.port", value_name = "PORT")]
#[arg(default_value_t = DEFAULT_METRICS_PORT)]
metrics_port: u16,

Check warning on line 173 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L172-L173

Added lines #L172 - L173 were not covered by tests
}

const DEFAULT_HTTP_ADDR: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
const DEFAULT_HTTP_PORT: u16 = 8080;

#[derive(Debug, clap::Args, Clone, Serialize, Deserialize, PartialEq)]

Check warning on line 179 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L179

Added line #L179 was not covered by tests
#[command(next_help_heading = "Server options")]
struct ServerOptions {
/// HTTP server listening interface.
#[arg(long = "http.addr", value_name = "ADDRESS")]
#[arg(default_value_t = DEFAULT_HTTP_ADDR)]
http_addr: IpAddr,

Check warning on line 185 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L184-L185

Added lines #L184 - L185 were not covered by tests

/// HTTP server listening port.
#[arg(long = "http.port", value_name = "PORT")]
#[arg(default_value_t = DEFAULT_HTTP_PORT)]
http_port: u16,

Check warning on line 190 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L189-L190

Added lines #L189 - L190 were not covered by tests

/// Comma separated list of domains from which to accept cross origin requests.
#[arg(long = "http.corsdomain")]
#[arg(value_delimiter = ',')]
http_cors_origins: Vec<String>,

Check warning on line 195 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L195

Added line #L195 was not covered by tests
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let matches = <Args as CommandFactory>::command().get_matches();
Expand Down Expand Up @@ -276,9 +318,14 @@
)
.expect("Failed to start libp2p relay server");

let addr = SocketAddr::new(args.server.http_addr, args.server.http_port);

Check warning on line 321 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L321

Added line #L321 was not covered by tests
let proxy_server = Arc::new(Proxy::new(
args.addr,
if args.allowed_origins.is_empty() { None } else { Some(args.allowed_origins) },
addr,
if args.server.http_cors_origins.is_empty() {
None
} else {
Some(args.server.http_cors_origins)
},

Check warning on line 328 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L323-L328

Added lines #L323 - L328 were not covered by tests
Some(grpc_addr),
None,
));
Expand All @@ -290,13 +337,12 @@
proxy_server.clone(),
);

let endpoint = format!("http://{}", args.addr);
let gql_endpoint = format!("{}/graphql", endpoint);
let gql_endpoint = format!("{addr}/graphql");

Check warning on line 340 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L340

Added line #L340 was not covered by tests
let encoded: String =
form_urlencoded::byte_serialize(gql_endpoint.replace("0.0.0.0", "localhost").as_bytes())
.collect();
let explorer_url = format!("https://worlds.dev/torii?url={}", encoded);
info!(target: LOG_TARGET, endpoint = %endpoint, "Starting torii endpoint.");
info!(target: LOG_TARGET, endpoint = %addr, "Starting torii endpoint.");

Check warning on line 345 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L345

Added line #L345 was not covered by tests
info!(target: LOG_TARGET, endpoint = %gql_endpoint, "Serving Graphql playground.");
info!(target: LOG_TARGET, url = %explorer_url, "Serving World Explorer.");

Expand All @@ -306,11 +352,12 @@
}
}

if let Some(listen_addr) = args.metrics {
info!(target: LOG_TARGET, addr = %listen_addr, "Starting metrics endpoint.");
if args.metrics.metrics {
let addr = SocketAddr::new(args.metrics.metrics_addr, args.metrics.metrics_port);
info!(target: LOG_TARGET, %addr, "Starting metrics endpoint.");

Check warning on line 357 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L355-L357

Added lines #L355 - L357 were not covered by tests
let prometheus_handle = PrometheusRecorder::install("torii")?;
let server = dojo_metrics::Server::new(prometheus_handle).with_process_metrics();
tokio::spawn(server.start(listen_addr));
tokio::spawn(server.start(addr));

Check warning on line 360 in bin/torii/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/torii/src/main.rs#L360

Added line #L360 was not covered by tests
}

let engine_handle = tokio::spawn(async move { engine.start().await });
Expand Down
Loading