Skip to content

Commit

Permalink
Add AWS lambda function (#3)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz authored Aug 28, 2024
1 parent 3ad9a70 commit 6bb67e3
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 6 deletions.
163 changes: 163 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 @@ -26,6 +26,7 @@ figment = { version = "0.10.19", features = ["yaml", "env"] }
hmac = "0.12.1"
hex = "0.4.3"
http = "1.1.0"
lambda_http = "0.13.0"
octorust = "0.8.0-rc.1"
pem = "3.0.4"
serde = { version = "1.0.209", features = ["derive"] }
Expand Down
6 changes: 6 additions & 0 deletions dco2-aws-lambda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ edition.workspace = true
rust-version.workspace = true

[dependencies]
anyhow = { workspace = true }
dco2 = { path = "../dco2" }
dco2-server = { path = "../dco2-server" }
figment = { workspace = true }
lambda_http = { workspace = true }
tokio = { workspace = true }
28 changes: 27 additions & 1 deletion dco2-aws-lambda/src/main.rs
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
fn main() {}
use anyhow::Context;
use dco2::github::{self, GHClientOctorust};
use dco2_server::handlers::setup_router;
use figment::{providers::Env, Figment};
use lambda_http::{run, tracing, Error};
use std::{env::set_var, sync::Arc};

#[tokio::main]
async fn main() -> Result<(), Error> {
// Do not include stage name in path
set_var("AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH", "true");

// Setup logging
set_var("AWS_LAMBDA_LOG_FORMAT", "json");
tracing::init_default_subscriber();

// Setup GitHub client
let cfg: github::AppConfig = Figment::new()
.merge(Env::prefixed("DCO2_"))
.extract()
.context("error setting up configuration")?;
let gh_client = Arc::new(GHClientOctorust::new(&cfg).context("error setting up github client")?);

// Start lambda runtime
let router = setup_router(gh_client, &cfg.webhook_secret);
run(router).await
}
5 changes: 3 additions & 2 deletions dco2-server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use hmac::{Hmac, Mac};
use sha2::Sha256;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
use tracing::{debug, error, instrument};
use tracing::{error, info, instrument};

/// Router's state.
#[derive(Clone, FromRef)]
Expand Down Expand Up @@ -82,12 +82,13 @@ async fn event(
error!(?err, "error processing event");
return Err((StatusCode::INTERNAL_SERVER_ERROR, String::new()));
}
debug!("event processed successfully");
info!("event processed successfully");

Ok(())
}

/// Verify that the signature provided in the webhook request is valid.
#[allow(clippy::missing_errors_doc)]
pub fn verify_signature(secret: &[u8], headers: &HeaderMap, body: &[u8]) -> Result<()> {
if let Some(signature) = headers
.get(SIGNATURE_HEADER)
Expand Down
1 change: 1 addition & 0 deletions dco2-server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod handlers;
6 changes: 3 additions & 3 deletions dco2-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use anyhow::{Context, Result};
use clap::Parser;
use config::{Config, LogFormat};
use dco2::github::GHClientOctorust;
use handlers::setup_router;
use dco2_server::handlers::setup_router;
use std::{path::PathBuf, sync::Arc};
use tokio::{net::TcpListener, signal};
use tracing::{error, info};
use tracing_subscriber::EnvFilter;

mod config;
mod handlers;

#[derive(Debug, Parser)]
#[clap(author, version, about)]
Expand All @@ -39,7 +38,8 @@ async fn main() -> Result<()> {
};

// Setup GitHub client
let gh_client = Arc::new(GHClientOctorust::new(&cfg.github_app)?);
let gh_client = GHClientOctorust::new(&cfg.github_app).context("error setting up github client")?;
let gh_client = Arc::new(gh_client);

// Setup and launch HTTP server
let router = setup_router(gh_client, &cfg.github_app.webhook_secret);
Expand Down

0 comments on commit 6bb67e3

Please sign in to comment.