Skip to content

Commit

Permalink
Provide dependencies from main function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirch committed Sep 3, 2023
1 parent 9f9cbb7 commit 13b2052
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
17 changes: 13 additions & 4 deletions src/finish_payment.rs
@@ -1,3 +1,4 @@
use aws_sdk_dynamodb::Client;
use lambda_http::{service_fn, Body, Error, Request, Response};
use serverless_payments::{
environment::STRIPE_WEBHOOK_SECRET, payment::PaymentStatus,
Expand All @@ -8,7 +9,10 @@ use tracing_subscriber::FmtSubscriber;

const SIGNATURE_HEADER_KEY: &str = "Stripe-Signature";

async fn handler(event: Request) -> Result<Response<Body>, Error> {
async fn handler(
payments_repository: &PaymentsRepository,
event: Request,
) -> Result<Response<Body>, Error> {
let signature = get_header(&event, SIGNATURE_HEADER_KEY)?;
let webhook_secret = std::env::var(STRIPE_WEBHOOK_SECRET).map_err(|e| e.to_string())?;

Expand Down Expand Up @@ -55,8 +59,7 @@ async fn handler(event: Request) -> Result<Response<Body>, Error> {
}
};

let payment_repository = PaymentsRepository::get().await;
payment_repository
payments_repository
.update_payment_status(&payment_id, payment_status)
.await?;

Expand All @@ -71,6 +74,12 @@ async fn main() -> Result<(), Error> {
.without_time()
.with_target(false)
.init();
lambda_http::run(service_fn(handler)).await?;

let aws_config = aws_config::load_from_env().await;
let dynamodb_client = Client::new(&aws_config);

let payments_repository = PaymentsRepository::new(dynamodb_client);

lambda_http::run(service_fn(|request| handler(&payments_repository, request))).await?;
Ok(())
}
24 changes: 18 additions & 6 deletions src/initiate_payment.rs
@@ -1,3 +1,4 @@
use aws_sdk_dynamodb::Client;
use lambda_http::{service_fn, Body, Error, Request, Response};
use serde::{Deserialize, Serialize};
use serverless_payments::{
Expand All @@ -20,8 +21,12 @@ pub struct InitiatePaymentResponse {
pub redirect_url: String,
}

#[tracing::instrument]
async fn handler(event: Request) -> Result<Response<Body>, Error> {
#[tracing::instrument(skip(payments_repository, payment_client))]
async fn handler(
payments_repository: &PaymentsRepository,
payment_client: &PaymentClient,
event: Request,
) -> Result<Response<Body>, Error> {
// Get the payment request from the event
let payment_request: InitiatePaymentRequest = get_body(&event)?;

Expand All @@ -33,12 +38,9 @@ async fn handler(event: Request) -> Result<Response<Body>, Error> {
PaymentStatus::Pending,
);

// Send the event to Stripe
let payment_client = PaymentClient::new();
// Get the redirect URL from the `initiate payment` process
let redirect_url = payment_client.initiate_payment(&payment).await?;
// Get the singleton instance of the payments repository
let payments_repository = PaymentsRepository::get().await;
// Save the data in DynamoDB
payments_repository.insert_payment(payment).await?;

Expand All @@ -57,6 +59,16 @@ async fn main() -> Result<(), Error> {
.without_time()
.with_target(false)
.init();
lambda_http::run(service_fn(handler)).await?;

let aws_config = aws_config::load_from_env().await;
let dynamodb_client = Client::new(&aws_config);

let payments_repository = PaymentsRepository::new(dynamodb_client);
let payment_client = PaymentClient::new();

lambda_http::run(service_fn(|request| {
handler(&payments_repository, &payment_client, request)
}))
.await?;
Ok(())
}
15 changes: 1 addition & 14 deletions src/payments_repository.rs
@@ -1,31 +1,18 @@
use aws_sdk_dynamodb::{types::AttributeValue, Client};
use tokio::sync::OnceCell;

use crate::{
environment::PAYMENTS_TABLE,
payment::{Payment, PaymentStatus},
};

pub static PAYMENTS_REPOSITORY: OnceCell<PaymentsRepository> = OnceCell::const_new();

#[derive(Clone)]
pub struct PaymentsRepository {
client: Client,
table_name: String,
}

impl PaymentsRepository {
pub async fn get() -> PaymentsRepository {
PAYMENTS_REPOSITORY
.get_or_init(|| async {
let client = Client::new(&aws_config::load_from_env().await);
PaymentsRepository::new(client)
})
.await
.clone()
}

fn new(client: Client) -> Self {
pub fn new(client: Client) -> Self {
let table_name = std::env::var(PAYMENTS_TABLE)
.unwrap_or_else(|_| panic!("{} variable not set", PAYMENTS_TABLE));

Expand Down

0 comments on commit 13b2052

Please sign in to comment.