Skip to content

Commit

Permalink
feat: add bucket as env var
Browse files Browse the repository at this point in the history
  • Loading branch information
EvolveArt committed Dec 15, 2023
1 parent a45e6bd commit e6d40e5
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ PORT=8080
LOCALSTACK_ENDPOINT=http://localhost:4566
BINARY_BASE_PATH=
APIBARA_POSTGRES_CONNECTION_STRING=
APIBARA_ETCD_URL=http://localhost:2379
INDEXER_SERVICE_BUCKET=
3 changes: 2 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
APIBARA_AUTH_TOKEN: ${{ secrets.APIBARA_AUTH_TOKEN }}
APIBARA_ETCD_URL: http://localhost:2379
INDEXER_SERVICE_BUCKET: indexer-service

services:
postgres:
Expand Down Expand Up @@ -86,7 +87,7 @@ jobs:
- name: Create S3 Bucket and SQS Queues
run: |
awslocal s3api create-bucket --bucket indexer-service --region eu-west-3 --create-bucket-configuration LocationConstraint=eu-west-3
awslocal s3api create-bu7ket --bucket indexer-service --region eu-west-3 --create-bucket-configuration LocationConstraint=eu-west-3
awslocal s3api put-object --bucket indexer-service --key apibara-scripts/
awslocal s3api list-buckets
awslocal sqs create-queue --queue-name indexer-service-start-indexer
Expand Down
1 change: 0 additions & 1 deletion src/constants/s3.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub const INDEXER_SERVICE_BUCKET: &str = "indexer-service";
pub const INDEXER_SERVICE_SCRIPTS_FOLDER: &str = "apibara-scripts";
5 changes: 3 additions & 2 deletions src/handlers/indexers/create_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use diesel_async::{AsyncConnection, RunQueryDsl};
use uuid::Uuid;

use crate::config::config;
use crate::constants::s3::INDEXER_SERVICE_BUCKET;
use crate::domain::models::indexer::{IndexerError, IndexerModel, IndexerStatus, IndexerType};
use crate::handlers::indexers::utils::get_s3_script_key;
use crate::infra::db::schema::indexers;
use crate::infra::errors::InfraError;
use crate::infra::repositories::indexer_repository::{self, IndexerDb};
use crate::publishers::indexers::publish_start_indexer;
use crate::utils::env::get_environment_variable;
use crate::AppState;

#[derive(Default)]
Expand Down Expand Up @@ -93,6 +93,7 @@ pub async fn create_indexer(
};

let config = config().await;
let bucket_name = get_environment_variable("INDEXER_SERVICE_BUCKET");

let connection = &mut state.pool.get().await.expect("Failed to get a connection from the pool");
let created_indexer = connection
Expand All @@ -109,7 +110,7 @@ pub async fn create_indexer(
config
.s3_client()
.put_object()
.bucket(INDEXER_SERVICE_BUCKET)
.bucket(bucket_name)
.key(get_s3_script_key(id))
.body(create_indexer_request.data.into())
.send()
Expand Down
6 changes: 4 additions & 2 deletions src/handlers/indexers/start_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use axum::extract::State;
use uuid::Uuid;

use crate::config::config;
use crate::constants::s3::INDEXER_SERVICE_BUCKET;
use crate::domain::models::indexer::{IndexerError, IndexerStatus};
use crate::handlers::indexers::indexer_types::get_indexer_handler;
use crate::handlers::indexers::utils::{get_s3_script_key, get_script_tmp_directory};
use crate::infra::repositories::indexer_repository::{
IndexerFilter, IndexerRepository, Repository, UpdateIndexerStatusAndProcessIdDb,
};
use crate::publishers::indexers::publish_start_indexer;
use crate::utils::env::get_environment_variable;
use crate::utils::PathExtractor;
use crate::AppState;

Expand All @@ -38,10 +38,12 @@ pub async fn start_indexer(id: Uuid, attempt: u32) -> Result<(), IndexerError> {
_ => return Err(IndexerError::InvalidIndexerStatus(indexer_model.status)),
}

let bucket_name = get_environment_variable("INDEXER_SERVICE_BUCKET");

let data = config
.s3_client()
.get_object()
.bucket(INDEXER_SERVICE_BUCKET)
.bucket(bucket_name)
.key(get_s3_script_key(id))
.send()
.await
Expand Down
5 changes: 3 additions & 2 deletions src/tests/server/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use mpart_async::client::MultipartRequest;
use rstest::rstest;

use crate::config::config;
use crate::constants::s3::INDEXER_SERVICE_BUCKET;
use crate::constants::sqs::START_INDEXER_QUEUE;
use crate::domain::models::indexer::{IndexerModel, IndexerStatus, IndexerType};
use crate::domain::models::types::AxumErrorResponse;
Expand All @@ -17,6 +16,7 @@ use crate::tests::common::utils::{
};
use crate::tests::server::common::setup_server;
use crate::types::sqs::StartIndexerRequest;
use crate::utils::env::get_environment_variable;

#[rstest]
#[tokio::test]
Expand All @@ -41,7 +41,8 @@ async fn create_postgres_indexer(#[future] setup_server: SocketAddr) {
assert_eq!(body.indexer_type, IndexerType::Postgres);

// check if the file exists on S3
assert_s3_contains_key(INDEXER_SERVICE_BUCKET, get_s3_script_key(body.id).as_str()).await;
let bucket_name = get_environment_variable("INDEXER_SERVICE_BUCKET");
assert_s3_contains_key(&bucket_name, get_s3_script_key(body.id).as_str()).await;

// check if the message is present on the queue
let request = StartIndexerRequest { id: body.id, attempt_no: 1 };
Expand Down
5 changes: 3 additions & 2 deletions src/tests/server/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use mpart_async::client::MultipartRequest;
use rstest::rstest;

use crate::config::config;
use crate::constants::s3::INDEXER_SERVICE_BUCKET;
use crate::constants::sqs::START_INDEXER_QUEUE;
use crate::domain::models::indexer::{IndexerModel, IndexerStatus, IndexerType};
use crate::domain::models::types::AxumErrorResponse;
Expand All @@ -17,6 +16,7 @@ use crate::tests::common::utils::{
};
use crate::tests::server::common::setup_server;
use crate::types::sqs::StartIndexerRequest;
use crate::utils::env::get_environment_variable;

#[rstest]
#[tokio::test]
Expand All @@ -41,7 +41,8 @@ async fn create_webhook_indexer(#[future] setup_server: SocketAddr) {
assert_eq!(body.indexer_type, IndexerType::Webhook);

// check if the file exists on S3
assert_s3_contains_key(INDEXER_SERVICE_BUCKET, get_s3_script_key(body.id).as_str()).await;
let bucket_name = get_environment_variable("INDEXER_SERVICE_BUCKET");
assert_s3_contains_key(&bucket_name, get_s3_script_key(body.id).as_str()).await;

// check if the message is present on the queue
let request = StartIndexerRequest { id: body.id, attempt_no: 1 };
Expand Down

0 comments on commit e6d40e5

Please sign in to comment.