From 04caa5954514a289adbac77db903c684a44361d6 Mon Sep 17 00:00:00 2001 From: oxade <93547199+oxade@users.noreply.github.com> Date: Thu, 12 Oct 2023 23:20:50 -0700 Subject: [PATCH] [graphql/rpc] easy: allows loading cfg from path (#14244) ## Description Needed for easier deploys ## Test Plan Manual --- If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process. ### Type of Change (Check all that apply) - [ ] protocol change - [ ] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes --- crates/sui-graphql-rpc/src/commands.rs | 5 +++++ crates/sui-graphql-rpc/src/config.rs | 6 +++++- crates/sui-graphql-rpc/src/main.rs | 6 ++++++ crates/sui-graphql-rpc/src/server/builder.rs | 2 +- crates/sui-graphql-rpc/src/server/mod.rs | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/sui-graphql-rpc/src/commands.rs b/crates/sui-graphql-rpc/src/commands.rs index d4a4c0b1fc546..b7af9f8b3d555 100644 --- a/crates/sui-graphql-rpc/src/commands.rs +++ b/crates/sui-graphql-rpc/src/commands.rs @@ -18,6 +18,11 @@ pub enum Command { #[clap(short, long)] file: Option, }, + FromConfig { + /// Path to TOML file containing configuration for server. + #[clap(short, long)] + path: PathBuf, + }, StartServer { /// URL of the RPC server for data fetching #[clap(short, long)] diff --git a/crates/sui-graphql-rpc/src/config.rs b/crates/sui-graphql-rpc/src/config.rs index b20d347772b7a..028f698f91b67 100644 --- a/crates/sui-graphql-rpc/src/config.rs +++ b/crates/sui-graphql-rpc/src/config.rs @@ -180,8 +180,12 @@ impl ServerConfig { serde_yaml::from_str::(&contents).unwrap() } + pub fn to_yaml(&self) -> String { + serde_yaml::to_string(&self).unwrap() + } + pub fn to_yaml_file(&self, path: PathBuf) { - let config = serde_yaml::to_string(&self).unwrap(); + let config = self.to_yaml(); std::fs::write(path, config).unwrap(); } } diff --git a/crates/sui-graphql-rpc/src/main.rs b/crates/sui-graphql-rpc/src/main.rs index 7155545e28223..1637430e50fc5 100644 --- a/crates/sui-graphql-rpc/src/main.rs +++ b/crates/sui-graphql-rpc/src/main.rs @@ -8,6 +8,7 @@ use clap::Parser; use sui_graphql_rpc::commands::Command; use sui_graphql_rpc::config::{ConnectionConfig, ServiceConfig}; use sui_graphql_rpc::schema_sdl_export; +use sui_graphql_rpc::server::builder::Server; use sui_graphql_rpc::server::simple_server::start_example_server; #[tokio::main] @@ -38,6 +39,11 @@ async fn main() { println!("Starting server..."); start_example_server(conn, service_config).await; } + Command::FromConfig { path } => { + let server = Server::from_yaml_config(path.to_str().unwrap()); + println!("Starting server..."); + server.await.run().await; + } } } diff --git a/crates/sui-graphql-rpc/src/server/builder.rs b/crates/sui-graphql-rpc/src/server/builder.rs index 7f8daa14d58db..22d5547b4f78b 100644 --- a/crates/sui-graphql-rpc/src/server/builder.rs +++ b/crates/sui-graphql-rpc/src/server/builder.rs @@ -31,7 +31,7 @@ use hyper::server::conn::AddrIncoming as HyperAddrIncoming; use hyper::Server as HyperServer; use std::{any::Any, net::SocketAddr, sync::Arc}; -pub(crate) struct Server { +pub struct Server { pub server: HyperServer>, } diff --git a/crates/sui-graphql-rpc/src/server/mod.rs b/crates/sui-graphql-rpc/src/server/mod.rs index 8948ba2794600..e46d2c6f3bfca 100644 --- a/crates/sui-graphql-rpc/src/server/mod.rs +++ b/crates/sui-graphql-rpc/src/server/mod.rs @@ -3,5 +3,5 @@ pub mod simple_server; -mod builder; +pub mod builder; mod version;