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

chore: configure cors for webapp #1865

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Feat: update api to collaboratively revert a dlc-channel
- Feat: Allow continuing from an offered dlc channel state (offered, settle offered and collab close offered)
- Feat: add a new project `webapp`. Eventually this will have the same functionality as our app (and more) and can be run on a self-hosted server
- Chore: In Webapp API allow requests from any origin (CORS)

## [1.7.4] - 2023-12-20

Expand Down
2 changes: 1 addition & 1 deletion webapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ serde_json = "1"
time = "0.3"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tower = { version = "0.4", features = ["util"] }
tower-http = { version = "0.5", features = ["fs", "trace"] }
tower-http = { version = "0.5", features = ["fs", "trace", "cors"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
19 changes: 15 additions & 4 deletions webapp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ use axum::response::Response;
use axum::routing::get;
use axum::routing::post;
use axum::Router;
use bitcoin::Network;
use rust_embed::RustEmbed;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tower_http::classify::ServerErrorsFailureClass;
use tower_http::cors::CorsLayer;
use tower_http::trace::TraceLayer;
use tracing::level_filters::LevelFilter;
use tracing::Span;
Expand Down Expand Up @@ -80,13 +82,13 @@ async fn main() -> Result<()> {
let (rx, tx) = AppSubscribers::new().await;
native::event::subscribe(tx);

serve(using_serve_dir(Arc::new(rx)), 3001).await?;
serve(using_serve_dir(Arc::new(rx), network), 3001).await?;

Ok(())
}

fn using_serve_dir(subscribers: Arc<AppSubscribers>) -> Router {
Router::new()
fn using_serve_dir(subscribers: Arc<AppSubscribers>, network: Network) -> Router {
let router = Router::new()
.route("/", get(index_handler))
.route("/api/version", get(version))
.route("/api/balance", get(get_balance))
Expand Down Expand Up @@ -116,7 +118,16 @@ fn using_serve_dir(subscribers: Arc<AppSubscribers>) -> Router {
},
),
)
.with_state(subscribers)
.with_state(subscribers);

if matches!(
network,
Network::Regtest | Network::Signet | Network::Testnet
) {
router.layer(CorsLayer::permissive())
} else {
router
}
}

// We use static route matchers ("/" and "/index.html") to serve our home
Expand Down
Loading