Skip to content

Commit

Permalink
dumnati: add CORS middleware
Browse files Browse the repository at this point in the history
This will allow JS logic hosted on builds.coreos.fedoraproject.org
to fetch the graph without triggering a browser error.
  • Loading branch information
lucab committed Jul 3, 2020
1 parent abd7d41 commit b7ad9a8
Showing 1 changed file with 14 additions and 0 deletions.
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 b7ad9a8

Please sign in to comment.