Skip to content

Commit

Permalink
Refactor tmp blob dir creation after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreae committed Aug 31, 2023
1 parent b220600 commit 71208cc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion aquadoggo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ sqlx = { version = "0.6.1", features = [
"sqlite",
"runtime-tokio-rustls",
] }
tempfile = "3.7.0"
thiserror = "1.0.39"
tokio = { version = "1.28.2", features = [
"macros",
Expand Down Expand Up @@ -109,6 +110,5 @@ rstest = "0.15.0"
rstest_reuse = "0.3.0"
serde_bytes = "0.11.12"
serde_json = "1.0.85"
tempfile = "3.7.0"
tower = "0.4.13"
tower-service = "0.3.2"
12 changes: 6 additions & 6 deletions aquadoggo/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::path::PathBuf;

use p2panda_rs::schema::SchemaId;

use crate::network::NetworkConfiguration;

/// Data directory name.
const DATA_DIR_NAME: &str = "aquadoggo";

/// Filename of default sqlite database.
const DEFAULT_SQLITE_NAME: &str = "aquadoggo-node.sqlite3";

/// Blobs directory
pub const BLOBS_DIR_NAME: &str = "blobs";

Expand All @@ -32,6 +28,9 @@ pub struct Configuration {
/// _not_ recommended for production settings.
pub allow_schema_ids: AllowList<SchemaId>,

/// Path to blobs directory.
pub blob_dir: Option<PathBuf>,

/// URL / connection string to PostgreSQL or SQLite database.
pub database_url: String,

Expand Down Expand Up @@ -62,6 +61,7 @@ impl Default for Configuration {
fn default() -> Self {
Self {
allow_schema_ids: AllowList::Wildcard,
blob_dir: None,
database_url: "sqlite::memory:".into(),
database_max_connections: 32,
http_port: 2020,
Expand Down
12 changes: 3 additions & 9 deletions aquadoggo/src/http/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use tower_http::cors::{Any, CorsLayer};
use tower_http::services::ServeDir;

use crate::bus::ServiceSender;
use crate::config::BLOBS_DIR_NAME;
use crate::context::Context;
use crate::graphql::GraphQLSchemaManager;
use crate::http::api::{handle_graphql_playground, handle_graphql_query};
Expand Down Expand Up @@ -66,15 +65,10 @@ pub async fn http_service(
let graphql_schema_manager =
GraphQLSchemaManager::new(context.store.clone(), tx, context.schema_provider.clone()).await;

let blob_dir_path = context
.config
.base_path
.as_ref()
.expect("Base path not set")
.join(BLOBS_DIR_NAME);
let blob_dir_path = context.config.blob_dir.as_ref().expect("Base path not set");

// Introduce a new context for all HTTP routes
let http_context = HttpServiceContext::new(graphql_schema_manager, blob_dir_path);
let http_context = HttpServiceContext::new(graphql_schema_manager, blob_dir_path.to_owned());

// Start HTTP server with given port and re-attempt with random port if it was taken already
let builder = if let Ok(builder) = axum::Server::try_bind(&http_address) {
Expand Down Expand Up @@ -111,9 +105,9 @@ mod tests {
use serde_json::json;
use tokio::sync::broadcast;

use crate::config::BLOBS_DIR_NAME;
use crate::graphql::GraphQLSchemaManager;
use crate::http::context::HttpServiceContext;
use crate::http::service::BLOBS_DIR_NAME;
use crate::schema::SchemaProvider;
use crate::test_utils::TestClient;
use crate::test_utils::{test_runner, TestNode};
Expand Down
15 changes: 5 additions & 10 deletions aquadoggo/src/materializer/tasks/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use p2panda_rs::operation::OperationValue;
use p2panda_rs::schema::SchemaId;
use p2panda_rs::storage_provider::traits::{DocumentStore, OperationStore};

use crate::config::{BLOBS_DIR_NAME, BLOBS_SYMLINK_DIR_NAME};
use crate::config::BLOBS_SYMLINK_DIR_NAME;
use crate::context::Context;
use crate::db::types::StorageDocument;
use crate::db::SqlStore;
Expand Down Expand Up @@ -78,14 +78,12 @@ pub async fn blob_task(context: Context, input: TaskInput) -> TaskResult<TaskInp
.unwrap();

// Compose, and when needed create, the path for the blob file.
let base_path = match &context.config.base_path {
let base_path = match &context.config.blob_dir {
Some(base_path) => base_path,
None => return Err(TaskError::Critical("No base path configured".to_string())),
};

let blob_dir = base_path
.join(BLOBS_DIR_NAME)
.join(blob_document.id().as_str());
let blob_dir = base_path.join(blob_document.id().as_str());

fs::create_dir_all(&blob_dir).map_err(|err| TaskError::Critical(err.to_string()))?;
let blob_view_path = blob_dir.join(blob_document.view_id().to_string());
Expand All @@ -101,7 +99,6 @@ pub async fn blob_task(context: Context, input: TaskInput) -> TaskResult<TaskInp
info!("Creating symlink from document id to current view");

let link_path = base_path
.join(BLOBS_DIR_NAME)
.join(BLOBS_SYMLINK_DIR_NAME)
.join(blob_document.id().as_str());

Expand Down Expand Up @@ -185,7 +182,7 @@ mod tests {
use p2panda_rs::test_utils::fixtures::key_pair;
use rstest::rstest;

use crate::config::{BLOBS_DIR_NAME, BLOBS_SYMLINK_DIR_NAME};
use crate::config::BLOBS_SYMLINK_DIR_NAME;
use crate::materializer::tasks::blob_task;
use crate::materializer::TaskInput;
use crate::test_utils::{add_document, test_runner, TestNode};
Expand Down Expand Up @@ -244,9 +241,8 @@ mod tests {
let document_id: DocumentId = blob_view_id.to_string().parse().unwrap();

// Construct the expected path to the blob view file.
let base_path = node.context.config.base_path.as_ref().unwrap();
let base_path = node.context.config.blob_dir.as_ref().unwrap();
let blob_path = base_path
.join(BLOBS_DIR_NAME)
.join(document_id.as_str())
.join(blob_view_id.to_string());

Expand All @@ -259,7 +255,6 @@ mod tests {

// Construct the expected path to the blob symlink file location.
let blob_path = base_path
.join(BLOBS_DIR_NAME)
.join(BLOBS_SYMLINK_DIR_NAME)
.join(document_id.as_str());

Expand Down
14 changes: 12 additions & 2 deletions aquadoggo/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::fs;

use anyhow::Result;
use p2panda_rs::identity::KeyPair;

use crate::bus::ServiceMessage;
use crate::config::Configuration;
use crate::config::{Configuration, BLOBS_DIR_NAME, BLOBS_SYMLINK_DIR_NAME};
use crate::context::Context;
use crate::db::SqlStore;
use crate::db::{connection_pool, create_database, run_pending_migrations, Pool};
Expand Down Expand Up @@ -45,7 +47,7 @@ pub struct Node {
impl Node {
/// Start p2panda node with your configuration. This method can be used to run the node within
/// other applications.
pub async fn start(key_pair: KeyPair, config: Configuration) -> Self {
pub async fn start(key_pair: KeyPair, mut config: Configuration) -> Self {
// Initialize database and get connection pool
let pool = initialize_db(&config)
.await
Expand All @@ -62,6 +64,14 @@ impl Node {
let schema_provider =
SchemaProvider::new(application_schema, config.allow_schema_ids.clone());

// Create tmp dirs for blob storage.
//
// @TODO: implement configuring this path for persistent storage.
let tmp_dir = tempfile::TempDir::new().unwrap();
let blob_dir_path = tmp_dir.path().join(BLOBS_DIR_NAME);
fs::create_dir_all(blob_dir_path.join(BLOBS_SYMLINK_DIR_NAME)).unwrap();
config.blob_dir = Some(blob_dir_path);

// Create service manager with shared data between services
let context = Context::new(store, key_pair, config, schema_provider);
let mut manager =
Expand Down
13 changes: 7 additions & 6 deletions aquadoggo/src/test_utils/runner.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::panic;
use std::sync::Arc;
use std::{fs, panic};

use futures::Future;
use p2panda_rs::identity::KeyPair;
use tokio::runtime::Builder;
use tokio::sync::Mutex;

use crate::config::{BLOBS_DIR_NAME, BLOBS_SYMLINK_DIR_NAME};
use crate::context::Context;
use crate::db::Pool;
use crate::db::SqlStore;
Expand Down Expand Up @@ -65,11 +66,8 @@ impl TestNodeManager {
// Initialise test store using pool.
let store = SqlStore::new(pool.clone());

// Construct tempfile directory for the test runner.
let tmp_dir = tempfile::TempDir::new().unwrap();

// Construct node config supporting any schema.
let cfg = Configuration::new(Some(tmp_dir.path().to_path_buf())).unwrap();
let cfg = Configuration::default();

// Construct the actual test node
let test_node = TestNode {
Expand Down Expand Up @@ -106,9 +104,12 @@ pub fn test_runner<F: AsyncTestFn + Send + Sync + 'static>(test: F) {

// Construct tempfile directory for the test runner.
let tmp_dir = tempfile::TempDir::new().unwrap();
let blob_dir_path = tmp_dir.path().join(BLOBS_DIR_NAME);
fs::create_dir_all(blob_dir_path.join(BLOBS_SYMLINK_DIR_NAME)).unwrap();

// Construct node config supporting any schema.
let cfg = Configuration::new(Some(tmp_dir.path().to_path_buf())).unwrap();
let mut cfg = Configuration::default();
cfg.blob_dir = Some(blob_dir_path);

// Construct the actual test node
let node = TestNode {
Expand Down
1 change: 1 addition & 0 deletions aquadoggo_cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ impl TryFrom<Configuration> for NodeConfiguration {

Ok(NodeConfiguration {
allow_schema_ids,
blob_dir: None,
database_url: value.database_url,
database_max_connections: value.database_max_connections,
http_port: value.http_port,
Expand Down

0 comments on commit 71208cc

Please sign in to comment.