Skip to content

Commit 8912f11

Browse files
committed
Storage: Make cdn_prefix available for all config variants
1 parent 9711273 commit 8912f11

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

src/storage.rs

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ const CACHE_CONTROL_README: &str = "public,max-age=604800";
3232

3333
type StdPath = std::path::Path;
3434

35+
#[derive(Debug)]
36+
pub struct StorageConfig {
37+
backend: StorageBackend,
38+
cdn_prefix: Option<String>,
39+
}
40+
3541
#[derive(Debug)]
3642
#[allow(clippy::large_enum_variant)]
37-
pub enum StorageConfig {
43+
pub enum StorageBackend {
3844
S3 { default: S3Config, index: S3Config },
3945
LocalFileSystem { path: PathBuf },
4046
InMemory,
@@ -46,12 +52,14 @@ pub struct S3Config {
4652
region: Option<String>,
4753
access_key: String,
4854
secret_key: SecretString,
49-
cdn_prefix: Option<String>,
5055
}
5156

5257
impl StorageConfig {
5358
pub fn in_memory() -> Self {
54-
Self::InMemory
59+
Self {
60+
backend: StorageBackend::InMemory,
61+
cdn_prefix: None,
62+
}
5563
}
5664

5765
pub fn from_environment() -> Self {
@@ -70,18 +78,21 @@ impl StorageConfig {
7078
region,
7179
access_key: access_key.clone(),
7280
secret_key: secret_key.clone(),
73-
cdn_prefix,
7481
};
7582

7683
let index = S3Config {
7784
bucket: index_bucket,
7885
region: index_region,
7986
access_key,
8087
secret_key,
81-
cdn_prefix: None,
8288
};
8389

84-
return Self::S3 { default, index };
90+
let backend = StorageBackend::S3 { default, index };
91+
92+
return Self {
93+
backend,
94+
cdn_prefix,
95+
};
8596
}
8697

8798
let current_dir = std::env::current_dir()
@@ -90,16 +101,22 @@ impl StorageConfig {
90101

91102
let path = current_dir.join("local_uploads");
92103

93-
Self::LocalFileSystem { path }
104+
let backend = StorageBackend::LocalFileSystem { path };
105+
106+
Self {
107+
backend,
108+
cdn_prefix: None,
109+
}
94110
}
95111
}
96112

97113
pub struct Storage {
114+
cdn_prefix: Option<String>,
115+
98116
store: Box<dyn ObjectStore>,
99117
crate_upload_store: Box<dyn ObjectStore>,
100118
readme_upload_store: Box<dyn ObjectStore>,
101119
db_dump_upload_store: Box<dyn ObjectStore>,
102-
cdn_prefix: String,
103120

104121
index_store: Box<dyn ObjectStore>,
105122
index_upload_store: Box<dyn ObjectStore>,
@@ -111,8 +128,10 @@ impl Storage {
111128
}
112129

113130
pub fn from_config(config: &StorageConfig) -> Self {
114-
match config {
115-
StorageConfig::S3 { default, index } => {
131+
let cdn_prefix = config.cdn_prefix.clone();
132+
133+
match &config.backend {
134+
StorageBackend::S3 { default, index } => {
116135
let options = ClientOptions::default();
117136
let store = build_s3(default, options);
118137

@@ -126,20 +145,16 @@ impl Storage {
126145
ClientOptions::default().with_default_content_type(CONTENT_TYPE_DB_DUMP);
127146
let db_dump_upload_store = build_s3(default, options);
128147

129-
let cdn_prefix = match default.cdn_prefix.as_ref() {
130-
None => panic!("Missing S3_CDN environment variable"),
131-
Some(cdn_prefix) if !cdn_prefix.starts_with("https://") => {
132-
format!("https://{cdn_prefix}")
133-
}
134-
Some(cdn_prefix) => cdn_prefix.clone(),
135-
};
136-
137148
let options = ClientOptions::default();
138149
let index_store = build_s3(index, options);
139150

140151
let options = client_options(CONTENT_TYPE_INDEX, CACHE_CONTROL_INDEX);
141152
let index_upload_store = build_s3(index, options);
142153

154+
if cdn_prefix.is_none() {
155+
panic!("Missing S3_CDN environment variable");
156+
}
157+
143158
Self {
144159
store: Box::new(store),
145160
crate_upload_store: Box::new(crate_upload_store),
@@ -151,7 +166,7 @@ impl Storage {
151166
}
152167
}
153168

154-
StorageConfig::LocalFileSystem { path } => {
169+
StorageBackend::LocalFileSystem { path } => {
155170
warn!(?path, "Using local file system for file storage");
156171

157172
let index_path = path.join("index");
@@ -176,13 +191,13 @@ impl Storage {
176191
crate_upload_store: Box::new(store.clone()),
177192
readme_upload_store: Box::new(store.clone()),
178193
db_dump_upload_store: Box::new(store),
179-
cdn_prefix: "/".into(),
194+
cdn_prefix,
180195
index_store: Box::new(index_store.clone()),
181196
index_upload_store: Box::new(index_store),
182197
}
183198
}
184199

185-
StorageConfig::InMemory => {
200+
StorageBackend::InMemory => {
186201
warn!("Using in-memory file storage");
187202
let store = ArcStore::new(InMemory::new());
188203

@@ -191,7 +206,7 @@ impl Storage {
191206
crate_upload_store: Box::new(store.clone()),
192207
readme_upload_store: Box::new(store.clone()),
193208
db_dump_upload_store: Box::new(store.clone()),
194-
cdn_prefix: "/".into(),
209+
cdn_prefix,
195210
index_store: Box::new(PrefixStore::new(store.clone(), "index")),
196211
index_upload_store: Box::new(PrefixStore::new(store, "index")),
197212
}
@@ -216,7 +231,13 @@ impl Storage {
216231
}
217232

218233
fn with_cdn_prefix(&self, path: &Path) -> String {
219-
format!("{}{}", self.cdn_prefix, path)
234+
match self.cdn_prefix.as_ref() {
235+
Some(cdn_prefix) if !cdn_prefix.starts_with("https://") => {
236+
format!("https://{cdn_prefix}/{path}")
237+
}
238+
Some(cdn_prefix) => format!("{cdn_prefix}/{path}"),
239+
None => format!("/{path}"),
240+
}
220241
}
221242

222243
#[instrument(skip(self))]

0 commit comments

Comments
 (0)