Skip to content

Commit

Permalink
feat: Add Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
MrExplode committed Oct 21, 2023
1 parent 71b770e commit 3660e98
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM rust:1.73-alpine as builder

WORKDIR /app
COPY . .

RUN cargo install --path .

FROM alpine:latest as runner

WORKDIR /app
COPY --from=builder /hooks .

CMD [ "/app/hooks" ]
16 changes: 14 additions & 2 deletions src/docker.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::env;
use std::sync::Arc;

use actix_web::{post, HttpResponse, Responder};
use actix_web::{post, web::Data, HttpResponse, Responder};
use actix_web_httpauth::extractors::bearer::BearerAuth;
use serde_json::json;

use crate::Clients;

const ENV_KEY: &str = "DOCKER_BEARER";

#[post("/docker")]
async fn handle(auth: BearerAuth) -> impl Responder {
async fn handle(auth: BearerAuth, req_body: String, clients: Data<Arc<Clients>>) -> impl Responder {
let env_token = match env::var(ENV_KEY) {
Ok(v) => v,
Err(_) => return HttpResponse::InternalServerError().json(json!({"error": "Missing environment value"})),
Expand All @@ -17,5 +20,14 @@ async fn handle(auth: BearerAuth) -> impl Responder {
return HttpResponse::Unauthorized().finish();
}

let _ = clients
.docker_client
.send(|message| {
message
.username("Docker")
.content(format!("```{}```", req_body).as_str())
})
.await;

HttpResponse::Accepted().finish()
}
16 changes: 14 additions & 2 deletions src/jira.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use std::env;
use std::sync::Arc;

use actix_web::{post, web, HttpResponse, Responder};
use actix_web::{
post,
web::{self, Data},
HttpResponse, Responder,
};
use serde::Deserialize;
use serde_json::json;

use crate::Clients;

const ENV_KEY: &str = "JIRA_TOKEN";

#[derive(Deserialize)]
Expand All @@ -12,7 +19,7 @@ struct Info {
}

#[post("/jira")]
async fn handle(info: web::Query<Info>) -> impl Responder {
async fn handle(info: web::Query<Info>, req_body: String, clients: Data<Arc<Clients>>) -> impl Responder {
let env_token = match env::var(ENV_KEY) {
Ok(v) => v,
Err(_) => return HttpResponse::InternalServerError().json(json!({"error": "Missing environment value"})),
Expand All @@ -22,5 +29,10 @@ async fn handle(info: web::Query<Info>) -> impl Responder {
return HttpResponse::Unauthorized().finish();
}

let _ = clients
.jira_client
.send(|message| message.username("Jira").content(format!("```{}```", req_body).as_str()))
.await;

HttpResponse::Accepted().finish()
}
33 changes: 31 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
mod docker;
mod jira;

use std::env;
use std::sync::Arc;
use std::{env, process};

use actix_web::web::Data;
use actix_web::{App, HttpServer};
use actix_web_httpauth::extractors::bearer;
use webhook::client::WebhookClient;

struct Clients {
jira_client: WebhookClient,
docker_client: WebhookClient,
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "debug");
env_logger::init();

HttpServer::new(|| {
let jira_url = match env::var("JIRA_URL") {
Ok(v) => v,
Err(_) => "".to_string(),
};

let docker_url = match env::var("DOCKER_URL") {
Ok(v) => v,
Err(_) => "".to_string(),
};

if jira_url.is_empty() || docker_url.is_empty() {
println!("Missing webhook url");
process::exit(-1);
}

let data = Arc::new(Clients {
jira_client: WebhookClient::new(&jira_url),
docker_client: WebhookClient::new(&docker_url),
});

HttpServer::new(move || {
App::new()
.app_data(bearer::Config::default())
.app_data(Data::new(data.clone()))
.service(jira::handle)
.service(docker::handle)
})
Expand Down

0 comments on commit 3660e98

Please sign in to comment.