Skip to content

Commit

Permalink
Add open CORS middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelvalle authored and gillchristian committed Jun 13, 2020
1 parent fc7ddc3 commit e16a780
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
1 change: 0 additions & 1 deletion .threshfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
port = 8080
logs_dir = "./logs"
run_job_on_ping = false

[[projects]]
repository = "test/test"
Expand Down
13 changes: 13 additions & 0 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ actix-files = "0.2.2"
clap = "2.33.1"
jsonwebtoken = "7.1.1"
actix-web-httpauth = "0.4.1"
actix-cors = "0.2.0"
4 changes: 0 additions & 4 deletions sample.threshfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Optional flag to run the job on the ping event
# sent by GitHub when setting up the webhook
run_job_on_ping = false

# Optional config, can be set here or by the CLI options
port = 8080
logs_dir = "./logs"
Expand Down
34 changes: 3 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use actix_cors::Cors;
use actix_web::middleware::Logger;
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, Result};
use actix_web_httpauth::middleware::HttpAuthentication;
Expand Down Expand Up @@ -122,7 +123,7 @@ fn run_project(project: Project, mut log: std::fs::File) {
// TODO: accept flag for Threshfile location
#[post("/webhook")]
async fn webhook(body: web::Json<PushEvent>, ctx: web::Data<Context>) -> impl Responder {
debug!("Github webhook recieved");
debug!("Webhook recieved");

let thresh_file = fs::read_to_string(&ctx.threshfile).expect("Failed reading threshfile file");
let jobs_config: JobsConfig =
Expand Down Expand Up @@ -271,6 +272,7 @@ async fn main() -> std::io::Result<()> {
App::new()
.wrap(Logger::default())
.app_data(context.clone())
.wrap(Cors::new().finish())
.wrap(HttpAuthentication::bearer(auth::validator))
.service(webhook)
.service(get_log)
Expand Down Expand Up @@ -315,34 +317,4 @@ mod test {

assert!(res.status().is_success());
}

#[actix_rt::test]
async fn test_webhook_ping() {
let context = web::Data::new(Context {
threshfile: "./.threshfile".to_owned(),
logs_dir: String::from("./logs"),
secret: String::from("secret"),
});
let mut server =
test::init_service(App::new().app_data(context.clone()).service(webhook)).await;

let payload = r#"
{
"zen": "no es moco de pavo",
"ref": "refs/tags/master",
"repository": {
"name": "test",
"full_name": "test/test"
}
}"#;
let json: Value = serde_json::from_str(payload).unwrap();

let req = test::TestRequest::post()
.uri("/webhook")
.set_json(&json)
.to_request();
let res = test::call_service(&mut server, req).await;

assert!(res.status().is_success());
}
}

0 comments on commit e16a780

Please sign in to comment.