Skip to content

Commit cec933f

Browse files
pietroalbiniJoshua Nelson
authored andcommitted
storage: choose the backend through the configuration
1 parent b975499 commit cec933f

File tree

4 files changed

+49
-55
lines changed

4 files changed

+49
-55
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ services:
1818
environment:
1919
CRATESFYI_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide
2020
CRATESFYI_DATABASE_URL: postgresql://cratesfyi:password@db
21+
DOCSRS_STORAGE_BACKEND: s3
2122
S3_ENDPOINT: http://s3:9000
2223
AWS_ACCESS_KEY_ID: cratesfyi
2324
AWS_SECRET_ACCESS_KEY: secret_key

src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::storage::StorageBackendKind;
12
use failure::{bail, format_err, Error, Fail, ResultExt};
23
use std::env::VarError;
34
use std::path::PathBuf;
@@ -16,6 +17,9 @@ pub struct Config {
1617
pub(crate) max_pool_size: u32,
1718
pub(crate) min_pool_idle: u32,
1819

20+
// Storage params
21+
pub(crate) storage_backend: StorageBackendKind,
22+
1923
// S3 params
2024
pub(crate) s3_bucket: String,
2125
#[cfg(test)]
@@ -48,6 +52,8 @@ impl Config {
4852
max_pool_size: env("DOCSRS_MAX_POOL_SIZE", 90)?,
4953
min_pool_idle: env("DOCSRS_MIN_POOL_IDLE", 10)?,
5054

55+
storage_backend: env("DOCSRS_STORAGE_BACKEND", StorageBackendKind::Database)?,
56+
5157
s3_bucket: env("DOCSRS_S3_BUCKET", "rust-docs-rs".to_string())?,
5258
// DO NOT CONFIGURE THIS THROUGH AN ENVIRONMENT VARIABLE!
5359
// Accidentally turning this on outside of the test suite might cause data loss in the

src/storage/mod.rs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<PathBuf>, Error> {
6767
Ok(files)
6868
}
6969

70+
#[derive(Debug, failure::Fail)]
71+
#[fail(display = "invalid storage backend")]
72+
pub(crate) struct InvalidStorageBackendError;
73+
74+
#[derive(Debug)]
75+
pub(crate) enum StorageBackendKind {
76+
Database,
77+
S3,
78+
}
79+
80+
impl std::str::FromStr for StorageBackendKind {
81+
type Err = InvalidStorageBackendError;
82+
83+
fn from_str(input: &str) -> Result<Self, Self::Err> {
84+
match input {
85+
"database" => Ok(StorageBackendKind::Database),
86+
"s3" => Ok(StorageBackendKind::S3),
87+
_ => Err(InvalidStorageBackendError),
88+
}
89+
}
90+
}
91+
7092
enum StorageBackend {
7193
Database(DatabaseBackend),
7294
S3(Box<S3Backend>),
@@ -78,29 +100,17 @@ pub struct Storage {
78100

79101
impl Storage {
80102
pub fn new(pool: Pool, metrics: Arc<Metrics>, config: &Config) -> Result<Self, Error> {
81-
let backend = if let Some(c) = s3::s3_client() {
82-
StorageBackend::S3(Box::new(S3Backend::new(c, metrics, config)?))
83-
} else {
84-
StorageBackend::Database(DatabaseBackend::new(pool, metrics))
85-
};
86-
Ok(Storage { backend })
87-
}
88-
89-
#[cfg(test)]
90-
pub(crate) fn temp_new_s3(metrics: Arc<Metrics>, config: &Config) -> Result<Self, Error> {
91103
Ok(Storage {
92-
backend: StorageBackend::S3(Box::new(S3Backend::new(
93-
s3::s3_client().unwrap(),
94-
metrics,
95-
config,
96-
)?)),
97-
})
98-
}
99-
100-
#[cfg(test)]
101-
pub(crate) fn temp_new_db(pool: Pool, metrics: Arc<Metrics>) -> Result<Self, Error> {
102-
Ok(Storage {
103-
backend: StorageBackend::Database(DatabaseBackend::new(pool, metrics)),
104+
backend: match config.storage_backend {
105+
StorageBackendKind::Database => {
106+
StorageBackend::Database(DatabaseBackend::new(pool, metrics))
107+
}
108+
StorageBackendKind::S3 => StorageBackend::S3(Box::new(S3Backend::new(
109+
s3::s3_client().unwrap(),
110+
metrics,
111+
config,
112+
)?)),
113+
},
104114
})
105115
}
106116

@@ -564,18 +574,21 @@ mod backend_tests {
564574

565575
macro_rules! backend_tests {
566576
(
567-
backends($env:ident) { $($backend:ident => $create:expr,)* }
577+
backends { $($backend:ident => $config:expr,)* }
568578
tests $tests:tt
569579
tests_with_metrics $tests_with_metrics:tt
570580
) => {
571581
$(
572582
mod $backend {
573583
use crate::test::TestEnvironment;
574-
use crate::storage::Storage;
584+
use crate::storage::{Storage, StorageBackendKind};
575585
use std::sync::Arc;
576586

577-
fn get_storage($env: &TestEnvironment) -> Arc<Storage> {
578-
$create
587+
fn get_storage(env: &TestEnvironment) -> Arc<Storage> {
588+
env.override_config(|config| {
589+
config.storage_backend = $config;
590+
});
591+
env.storage()
579592
}
580593

581594
backend_tests!(@tests $tests);
@@ -606,9 +619,9 @@ mod backend_tests {
606619
}
607620

608621
backend_tests! {
609-
backends(env) {
610-
s3 => env.s3(),
611-
database => env.temp_storage_db(),
622+
backends {
623+
s3 => StorageBackendKind::S3,
624+
database => StorageBackendKind::Database,
612625
}
613626

614627
tests {

src/test/mod.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ pub(crate) struct TestEnvironment {
9898
storage: OnceCell<Arc<Storage>>,
9999
metrics: OnceCell<Arc<Metrics>>,
100100
frontend: OnceCell<TestFrontend>,
101-
s3: OnceCell<Arc<Storage>>,
102-
storage_db: OnceCell<Arc<Storage>>,
103101
}
104102

105103
pub(crate) fn init_logger() {
@@ -119,8 +117,6 @@ impl TestEnvironment {
119117
storage: OnceCell::new(),
120118
metrics: OnceCell::new(),
121119
frontend: OnceCell::new(),
122-
s3: OnceCell::new(),
123-
storage_db: OnceCell::new(),
124120
}
125121
}
126122

@@ -203,28 +199,6 @@ impl TestEnvironment {
203199
self.frontend.get_or_init(|| TestFrontend::new(&*self))
204200
}
205201

206-
pub(crate) fn s3(&self) -> Arc<Storage> {
207-
self.s3
208-
.get_or_init(|| {
209-
Arc::new(
210-
Storage::temp_new_s3(self.metrics(), &*self.config())
211-
.expect("failed to initialize the storage"),
212-
)
213-
})
214-
.clone()
215-
}
216-
217-
pub(crate) fn temp_storage_db(&self) -> Arc<Storage> {
218-
self.storage_db
219-
.get_or_init(|| {
220-
Arc::new(
221-
Storage::temp_new_db(self.db().pool(), self.metrics())
222-
.expect("failed to initialize the storage"),
223-
)
224-
})
225-
.clone()
226-
}
227-
228202
pub(crate) fn fake_release(&self) -> fakes::FakeRelease {
229203
fakes::FakeRelease::new(self.db(), self.storage())
230204
}

0 commit comments

Comments
 (0)