diff --git a/services/backup/src/constants.rs b/services/backup/src/constants.rs index c55e2e1b1..fd04940a5 100644 --- a/services/backup/src/constants.rs +++ b/services/backup/src/constants.rs @@ -1,15 +1,34 @@ // Assorted constants pub const MPSC_CHANNEL_BUFFER_CAPACITY: usize = 1; // Configuration defaults pub const DEFAULT_GRPC_SERVER_PORT: u64 = 50051; pub const DEFAULT_LOCALSTACK_URL: &str = "http://localhost:4566"; pub const DEFAULT_BLOB_SERVICE_URL: &str = "http://localhost:50053"; // Environment variable names pub const SANDBOX_ENV_VAR: &str = "COMM_SERVICES_SANDBOX"; pub const LOG_LEVEL_ENV_VAR: &str = tracing_subscriber::filter::EnvFilter::DEFAULT_ENV; + +// DynamoDB constants + +pub const BACKUP_TABLE_NAME: &str = "backup-service-backup"; +pub const BACKUP_TABLE_FIELD_USER_ID: &str = "userID"; +pub const BACKUP_TABLE_FIELD_BACKUP_ID: &str = "backupID"; +pub const BACKUP_TABLE_FIELD_CREATED: &str = "created"; +pub const BACKUP_TABLE_FIELD_RECOVERY_DATA: &str = "recoveryData"; +pub const BACKUP_TABLE_FIELD_COMPACTION_HOLDER: &str = "compactionHolder"; +pub const BACKUP_TABLE_FIELD_ATTACHMENT_HOLDERS: &str = "attachmentHolders"; +pub const BACKUP_TABLE_INDEX_USERID_CREATED: &str = "userID-created-index"; + +pub const LOG_TABLE_NAME: &str = "backup-service-log"; +pub const LOG_TABLE_FIELD_BACKUP_ID: &str = "backupID"; +pub const LOG_TABLE_FIELD_LOG_ID: &str = "logID"; +pub const LOG_TABLE_FIELD_PERSISTED_IN_BLOB: &str = "persistedInBlob"; +pub const LOG_TABLE_FIELD_VALUE: &str = "value"; +pub const LOG_TABLE_FIELD_ATTACHMENT_HOLDERS: &str = "attachmentHolders"; +pub const LOG_TABLE_FIELD_DATA_HASH: &str = "dataHash"; diff --git a/services/backup/src/database.rs b/services/backup/src/database.rs new file mode 100644 index 000000000..2b2ca551e --- /dev/null +++ b/services/backup/src/database.rs @@ -0,0 +1,89 @@ +use chrono::{DateTime, Utc}; +use std::sync::Arc; + +#[derive(Clone, Debug)] +pub struct BackupItem { + pub user_id: String, + pub backup_id: String, + pub created: DateTime, + pub recovery_data: String, + pub compaction_holder: String, + pub attachment_holders: String, +} + +#[derive(Clone, Debug)] +pub struct LogItem { + pub backup_id: String, + pub log_id: String, + pub persisted_in_blob: bool, + pub value: String, + pub attachment_holders: String, + pub data_hash: String, +} + +#[derive(Clone)] +pub struct DatabaseClient { + client: Arc, +} + +impl DatabaseClient { + pub fn new(aws_config: &aws_types::SdkConfig) -> Self { + DatabaseClient { + client: Arc::new(aws_sdk_dynamodb::Client::new(aws_config)), + } + } + + // backup item + pub async fn put_backup_item( + &self, + backup_item: BackupItem, + ) -> Result<(), Error> { + unimplemented!() + } + + pub async fn find_backup_item( + &self, + user_id: &str, + backup_id: &str, + ) -> Result, Error> { + unimplemented!() + } + + pub async fn find_last_backup_item( + &self, + user_id: &str, + ) -> Result, Error> { + unimplemented!() + } + + pub async fn remove_backup_item(&self, backup_id: &str) -> Result<(), Error> { + unimplemented!() + } + + // log item + pub async fn put_log_item(&self, log_item: LogItem) -> Result<(), Error> { + unimplemented!() + } + + pub async fn find_log_item( + &self, + backup_id: &str, + log_id: &str, + ) -> Result, Error> { + unimplemented!() + } + + pub async fn find_log_items_for_backup( + &self, + backup_id: &str, + ) -> Result, Error> { + unimplemented!() + } + + pub async fn remove_log_item(&self, log_id: &str) -> Result<(), Error> { + unimplemented!() + } +} + +// TODO: Replace this with dedicated DB error +type Error = anyhow::Error; diff --git a/services/backup/src/main.rs b/services/backup/src/main.rs index a397a3a6c..80f8d7c11 100644 --- a/services/backup/src/main.rs +++ b/services/backup/src/main.rs @@ -1,47 +1,48 @@ use anyhow::Result; use std::net::SocketAddr; use tonic::transport::Server; use tracing::{info, Level}; use tracing_subscriber::EnvFilter; use crate::service::{BackupServiceServer, MyBackupService}; pub mod blob; pub mod config; pub mod constants; +pub mod database; pub mod service; // re-export this to be available as crate::CONFIG pub use config::CONFIG; fn configure_logging() -> Result<()> { let filter = EnvFilter::builder() .with_default_directive(Level::INFO.into()) .with_env_var(constants::LOG_LEVEL_ENV_VAR) .from_env_lossy(); let subscriber = tracing_subscriber::fmt().with_env_filter(filter).finish(); tracing::subscriber::set_global_default(subscriber)?; Ok(()) } async fn run_grpc_server() -> Result<()> { let addr: SocketAddr = format!("[::]:{}", CONFIG.listening_port).parse()?; let backup_service = MyBackupService::default(); info!("Starting gRPC server listening at {}", addr.to_string()); Server::builder() .add_service(BackupServiceServer::new(backup_service)) .serve(addr) .await?; Ok(()) } #[tokio::main] async fn main() -> Result<()> { config::parse_cmdline_args(); configure_logging()?; run_grpc_server().await }