Skip to content

Commit 3480c70

Browse files
authored
[graphql/rpc] add db url to server conn config (#14218)
1 parent 4543b5a commit 3480c70

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

crates/sui-graphql-rpc/src/commands.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ pub enum Command {
2828
/// Host to bind the server to
2929
#[clap(long)]
3030
host: Option<String>,
31+
/// DB URL to connect to
32+
#[clap(long)]
33+
db_url: Option<String>,
3134
/// Path to TOML file containing configuration for service.
3235
#[clap(short, long)]
3336
config: Option<PathBuf>,

crates/sui-graphql-rpc/src/config.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{collections::BTreeSet, path::PathBuf};
55

66
use async_graphql::*;
77
use serde::{Deserialize, Serialize};
8+
use std::env;
89

910
use crate::functional_group::FunctionalGroup;
1011

@@ -17,6 +18,7 @@ pub struct ConnectionConfig {
1718
pub(crate) port: u16,
1819
pub(crate) host: String,
1920
pub(crate) rpc_url: String,
21+
pub(crate) db_url: String,
2022
}
2123

2224
/// Configuration on features supported by the RPC, passed in a TOML-based file.
@@ -52,12 +54,18 @@ pub struct Experiments {
5254
}
5355

5456
impl ConnectionConfig {
55-
pub fn new(port: Option<u16>, host: Option<String>, rpc_url: Option<String>) -> Self {
57+
pub fn new(
58+
port: Option<u16>,
59+
host: Option<String>,
60+
rpc_url: Option<String>,
61+
db_url: Option<String>,
62+
) -> Self {
5663
let default = Self::default();
5764
Self {
5865
port: port.unwrap_or(default.port),
5966
host: host.unwrap_or(default.host),
6067
rpc_url: rpc_url.unwrap_or(default.rpc_url),
68+
db_url: db_url.unwrap_or(default.db_url),
6169
}
6270
}
6371
}
@@ -101,6 +109,8 @@ impl Default for ConnectionConfig {
101109
port: 8000,
102110
host: "127.0.0.1".to_string(),
103111
rpc_url: "https://fullnode.testnet.sui.io:443/".to_string(),
112+
db_url: env::var("PG_DB_URL")
113+
.expect("PG_DB_URL must be set if db_url not provided in config"),
104114
}
105115
}
106116
}

crates/sui-graphql-rpc/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ async fn main() {
2828
port,
2929
host,
3030
config,
31+
db_url,
3132
} => {
32-
let conn = ConnectionConfig::new(port, host, rpc_url);
33+
let conn = ConnectionConfig::new(port, host, rpc_url, db_url);
3334
let service_config = service_config(config);
3435

3536
println!("Starting server...");

crates/sui-graphql-rpc/src/server/simple_server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::server::builder::ServerBuilder;
1414

1515
use prometheus::Registry;
1616
use std::default::Default;
17-
use std::env;
1817
use std::net::SocketAddr;
1918
use std::sync::Arc;
2019
use sui_json_rpc::name_service::NameServiceConfig;
@@ -31,7 +30,7 @@ pub async fn start_example_server(conn: ConnectionConfig, service_config: Servic
3130
let data_loader = lru_cache_data_loader(&sui_sdk_client_v0).await;
3231

3332
// TODO (wlmyng): Allow users to choose which data sources to back graphql
34-
let db_url = env::var("PG_DB_URL").expect("PG_DB_URL must be set");
33+
let db_url = conn.db_url;
3534
let pg_conn_pool = PgManager::new(db_url, None)
3635
.map_err(|e| {
3736
println!("Failed to create pg connection pool: {}", e);

0 commit comments

Comments
 (0)