Skip to content

Commit

Permalink
Handle CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
h33p committed Jul 22, 2024
1 parent d9a4410 commit 5e0e182
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

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

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pierport"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
rust-version = "1.76"
authors = ["Aurimas Blažulionis <[email protected]>"]
Expand Down Expand Up @@ -43,12 +43,15 @@ tokio = { version = "1", features = ["full"], optional = true }
tokio-util = { version = "0.7", optional = true }
jsonwebtoken = { version = "9", optional = true }
reqwest = { version = "0.11", features = ["stream", "rustls-tls", "json"], default-features = false, optional = true }
toml = { version = "0.5", optional = true}
toml = { version = "0.5", optional = true }
http = { version = "0.2", optional = true }
tower-http = { version = "0.4", features = ["cors"], optional = true }
tower = { version = "0.4", optional = true }

# See https://github.com/rust-lang/jobserver-rs/issues/79
[build-dependencies]
jobserver = { version = "=0.1.28", default-features = false }

[features]
default = ["bin-deps"]
bin-deps = ["clap", "anyhow/backtrace", "tokio", "tokio-util", "jsonwebtoken", "reqwest", "toml"]
bin-deps = ["clap", "anyhow/backtrace", "tokio", "tokio-util", "jsonwebtoken", "reqwest", "toml", "http", "tower-http", "tower"]
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ use axum::{
routing::{get, post},
Router,
};
use http::Method;
use hyper::{Request, Response, StatusCode};
use reqwest::Client;
use tower::ServiceBuilder;
use tower_http::cors::{Any, CorsLayer};

static CS: Lazy<Mutex<()>> = Lazy::new(Default::default);

Expand Down Expand Up @@ -171,12 +174,23 @@ async fn main() -> anyhow::Result<()> {
}
});

let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any)
.allow_headers([
hyper::header::AUTHORIZATION,
hyper::header::CONTENT_LENGTH,
header::SIZE,
])
.allow_credentials(true);

// TODO: should we include pierport/v1/ here?
let app = Router::new()
.route("/capabilities", get(capabilities))
.route("/import/~:patp/:id", get(import_status))
.route("/import/~:patp", post(import))
.layer(DefaultBodyLimit::max(config.upload_limit))
.layer(ServiceBuilder::new().layer(cors))
.with_state((config.clone(), jwt_key, sessions));

axum::Server::bind(&config.bind)
Expand Down

0 comments on commit 5e0e182

Please sign in to comment.