Skip to content

Commit

Permalink
add cors for faucet (MystenLabs#2650)
Browse files Browse the repository at this point in the history
* cors for faucet

* hakari
  • Loading branch information
longbowlu authored Jun 22, 2022
1 parent bb9da39 commit 7d52d39
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
11 changes: 7 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/sui-faucet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ tracing-subscriber = { version = "0.3.11", features = ["time", "registry", "env-
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.80"
tower = { version = "0.4.12", features = ["util", "timeout", "load-shed", "limit"] }
tower-http = { version = "0.3.4", features = ["cors"] }
http = { version = "0.2.8" }

sui = { path = "../sui" }
sui-json-rpc-api = { path = "../sui-json-rpc-api" }
sui-types = { path = "../sui-types" }
sui-core = { path = "../sui-core" }
sui-config = { path = "../sui-config" }
workspace-hack = { path = "../workspace-hack"}
telemetry-subscribers = { git = "https://github.com/MystenLabs/mysten-infra", rev = "ff5c1d69057fe93be658377462ca2875a57a0223" }

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
1 change: 1 addition & 0 deletions crates/sui-faucet/src/faucet/simple_faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl SimpleFaucet {
// Ok to unwrap() since `get_gas_objects` guarantees gas
.map(|q| GasCoin::try_from(&q.1).unwrap())
.collect::<Vec<GasCoin>>();
info!("Coins held: {:?}", coins);
coins.sort_by_key(|a| a.value());

if coins.len() < 2 {
Expand Down
26 changes: 20 additions & 6 deletions crates/sui-faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use axum::{
BoxError, Extension, Json, Router,
};
use clap::Parser;
use http::Method;
use std::{
borrow::Cow,
net::{IpAddr, Ipv4Addr, SocketAddr},
Expand All @@ -19,7 +20,8 @@ use sui::wallet_commands::{WalletCommands, WalletContext};
use sui_config::{sui_config_dir, SUI_WALLET_CONFIG};
use sui_faucet::{Faucet, FaucetRequest, FaucetResponse, SimpleFaucet};
use tower::ServiceBuilder;
use tracing::info;
use tower_http::cors::{Any, CorsLayer};
use tracing::{info, warn};

// TODO: Increase this once we use multiple gas objects
const CONCURRENCY_LIMIT: usize = 1;
Expand Down Expand Up @@ -59,7 +61,9 @@ struct AppState<F = SimpleFaucet> {
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// initialize tracing
tracing_subscriber::fmt::init();
let _guard = telemetry_subscribers::TelemetryConfig::new(env!("CARGO_BIN_NAME"))
.with_env()
.init();

let context = create_wallet_context().await?;

Expand All @@ -78,12 +82,19 @@ async fn main() -> Result<(), anyhow::Error> {
config,
});

// TODO: restrict access if needed
let cors = CorsLayer::new()
.allow_methods(vec![Method::GET, Method::POST])
.allow_headers(Any)
.allow_origin(Any);

let app = Router::new()
.route("/", get(health))
.route("/gas", post(request_gas))
.layer(
ServiceBuilder::new()
.layer(HandleErrorLayer::new(handle_error))
.layer(cors)
.buffer(request_buffer_size)
.concurrency_limit(CONCURRENCY_LIMIT)
.timeout(Duration::from_secs(timeout_in_seconds))
Expand Down Expand Up @@ -122,10 +133,13 @@ async fn request_gas(
};
match result {
Ok(v) => (StatusCode::CREATED, Json(FaucetResponse::from(v))),
Err(v) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(FaucetResponse::from(v)),
),
Err(v) => {
warn!("Failed to request gas: {:?}", v);
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(FaucetResponse::from(v)),
)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/workspace-hack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ toml_edit = { version = "0.13" }
tonic = { version = "0.7", features = ["async-trait", "axum", "channel", "codegen", "h2", "hyper", "hyper-timeout", "prost", "prost-derive", "prost1", "rustls-pemfile", "tls", "tokio", "tokio-rustls", "tower", "tracing-futures", "transport"] }
tonic-health = { version = "0.6", features = ["transport"] }
tower = { version = "0.4", features = ["__common", "balance", "buffer", "discover", "filter", "full", "futures-core", "futures-util", "hdrhistogram", "hedge", "indexmap", "limit", "load", "load-shed", "log", "make", "pin-project", "pin-project-lite", "rand", "ready-cache", "reconnect", "retry", "slab", "spawn-ready", "steer", "timeout", "tokio", "tokio-util", "tracing", "util"] }
tower-http = { version = "0.3", features = ["map-response-body", "tower", "util"] }
tower-http = { version = "0.3", features = ["cors", "map-response-body", "tower", "util"] }
tower-layer = { version = "0.3", default-features = false }
tower-service = { version = "0.3", default-features = false }
tracing = { version = "0.1", features = ["attributes", "log", "std", "tracing-attributes"] }
Expand Down Expand Up @@ -1047,7 +1047,7 @@ tonic-build-ca01ad9e24f5d932 = { package = "tonic-build", version = "0.7", featu
tonic-health = { version = "0.6", features = ["transport"] }
toolchain_find = { version = "0.2", default-features = false }
tower = { version = "0.4", features = ["__common", "balance", "buffer", "discover", "filter", "full", "futures-core", "futures-util", "hdrhistogram", "hedge", "indexmap", "limit", "load", "load-shed", "log", "make", "pin-project", "pin-project-lite", "rand", "ready-cache", "reconnect", "retry", "slab", "spawn-ready", "steer", "timeout", "tokio", "tokio-util", "tracing", "util"] }
tower-http = { version = "0.3", features = ["map-response-body", "tower", "util"] }
tower-http = { version = "0.3", features = ["cors", "map-response-body", "tower", "util"] }
tower-layer = { version = "0.3", default-features = false }
tower-service = { version = "0.3", default-features = false }
tracing = { version = "0.1", features = ["attributes", "log", "std", "tracing-attributes"] }
Expand Down

0 comments on commit 7d52d39

Please sign in to comment.