Skip to content

Commit

Permalink
Merge pull request #18 from lucab/ups/cors
Browse files Browse the repository at this point in the history
dumnati: add CORS middleware
  • Loading branch information
Luca Bruno authored Jul 9, 2020
2 parents 1dba435 + b7ad9a8 commit 41f5254
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
31 changes: 22 additions & 9 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions dumnati/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ publish = false

[dependencies]
actix = "^0.9.0"
actix-cors = "^0.2"
actix-web = "^2.0.0"
cbloom = "^0.1.3"
chrono = "^0.4.7"
Expand Down
14 changes: 14 additions & 0 deletions dumnati/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod policy;
mod scraper;

use actix::prelude::*;
use actix_cors::CorsFactory;
use actix_web::{web, App, HttpResponse};
use failure::{Error, Fallible};
use prometheus::{Histogram, IntCounter, IntCounterVec, IntGauge, IntGaugeVec};
Expand Down Expand Up @@ -74,6 +75,8 @@ fn main() -> Fallible<()> {
let sys = actix::System::new("dumnati");

// TODO(lucab): figure out all configuration params.
let gb_allowed_origins = vec!["https://builds.coreos.fedoraproject.org"];
let pe_allowed_origins = vec!["https://builds.coreos.fedoraproject.org"];
let streams_cfg = maplit::btreeset!["next", "stable", "testing"];
let mut scrapers = HashMap::with_capacity(streams_cfg.len());
for stream in streams_cfg {
Expand All @@ -94,6 +97,7 @@ fn main() -> Fallible<()> {
let gb_service = service_state.clone();
actix_web::HttpServer::new(move || {
App::new()
.wrap(build_cors_middleware(&gb_allowed_origins))
.data(gb_service.clone())
.route("/v1/graph", web::get().to(gb_serve_graph))
})
Expand All @@ -114,6 +118,7 @@ fn main() -> Fallible<()> {
let pe_service = service_state.clone();
actix_web::HttpServer::new(move || {
App::new()
.wrap(build_cors_middleware(&pe_allowed_origins))
.data(pe_service.clone())
.route("/v1/graph", web::get().to(pe_serve_graph))
})
Expand Down Expand Up @@ -266,6 +271,15 @@ pub(crate) fn pe_record_metrics(data: &AppState, query: &GraphQuery) {
}
}

/// Provide a CORS middleware allowing given origins.
pub(crate) fn build_cors_middleware(allowed_origins: &[&str]) -> CorsFactory {
let mut builder = actix_cors::Cors::new();
for origin in allowed_origins {
builder = builder.allowed_origin(origin);
}
builder.finish()
}

#[derive(Debug, StructOpt)]
pub(crate) struct CliOptions {
/// Path to configuration file.
Expand Down

0 comments on commit 41f5254

Please sign in to comment.