Skip to content

Commit a297e74

Browse files
committed
web: use a shared instance of Storage
1 parent ecd6860 commit a297e74

File tree

5 files changed

+43
-36
lines changed

5 files changed

+43
-36
lines changed

src/db/file.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ use crate::storage::{CompressionAlgorithms, Storage};
1111
use serde_json::Value;
1212
use std::path::{Path, PathBuf};
1313

14-
pub(crate) use crate::storage::Blob;
15-
16-
pub(crate) fn get_path(pool: Pool, path: &str, max_size: usize) -> Result<Blob> {
17-
Storage::new(pool).get(path, max_size)
18-
}
19-
2014
/// Store all files in a directory and return [[mimetype, filename]] as Json
2115
///
2216
/// If there is an S3 Client configured, store files into an S3 bucket;

src/test/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod fakes;
22

33
use crate::db::{Pool, PoolConnection};
44
use crate::storage::s3::TestS3;
5+
use crate::storage::Storage;
56
use crate::web::Server;
67
use crate::BuildQueue;
78
use crate::Config;
@@ -96,6 +97,7 @@ pub(crate) struct TestEnvironment {
9697
build_queue: OnceCell<Arc<BuildQueue>>,
9798
config: OnceCell<Arc<Config>>,
9899
db: OnceCell<TestDatabase>,
100+
storage: OnceCell<Arc<Storage>>,
99101
frontend: OnceCell<TestFrontend>,
100102
s3: OnceCell<TestS3>,
101103
}
@@ -112,6 +114,7 @@ impl TestEnvironment {
112114
build_queue: OnceCell::new(),
113115
config: OnceCell::new(),
114116
db: OnceCell::new(),
117+
storage: OnceCell::new(),
115118
frontend: OnceCell::new(),
116119
s3: OnceCell::new(),
117120
}
@@ -154,14 +157,21 @@ impl TestEnvironment {
154157
.clone()
155158
}
156159

160+
pub(crate) fn storage(&self) -> Arc<Storage> {
161+
self.storage
162+
.get_or_init(|| Arc::new(Storage::new(self.db().pool())))
163+
.clone()
164+
}
165+
157166
pub(crate) fn db(&self) -> &TestDatabase {
158167
self.db
159168
.get_or_init(|| TestDatabase::new(&self.config()).expect("failed to initialize the db"))
160169
}
161170

162171
pub(crate) fn frontend(&self) -> &TestFrontend {
163-
self.frontend
164-
.get_or_init(|| TestFrontend::new(self.db(), self.config(), self.build_queue()))
172+
self.frontend.get_or_init(|| {
173+
TestFrontend::new(self.db(), self.config(), self.build_queue(), self.storage())
174+
})
165175
}
166176

167177
pub(crate) fn s3(&self) -> &TestS3 {
@@ -229,7 +239,12 @@ pub(crate) struct TestFrontend {
229239
}
230240

231241
impl TestFrontend {
232-
fn new(db: &TestDatabase, config: Arc<Config>, build_queue: Arc<BuildQueue>) -> Self {
242+
fn new(
243+
db: &TestDatabase,
244+
config: Arc<Config>,
245+
build_queue: Arc<BuildQueue>,
246+
storage: Arc<Storage>,
247+
) -> Self {
233248
Self {
234249
server: Server::start(
235250
Some("127.0.0.1:0"),

src/web/file.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
//! Database based file handler
22
3-
use crate::db::Pool;
4-
use crate::{db, error::Result, Config};
3+
use crate::storage::{Storage, Blob};
4+
use crate::{error::Result, Config};
55
use iron::{status, Handler, IronError, IronResult, Request, Response};
66

77
#[derive(Debug)]
8-
pub(crate) struct File(pub(crate) db::file::Blob);
8+
pub(crate) struct File(pub(crate) Blob);
99

1010
impl File {
1111
/// Gets file from database
12-
pub fn from_path(pool: Pool, path: &str, config: &Config) -> Result<File> {
12+
pub fn from_path(storage: &Storage, path: &str, config: &Config) -> Result<File> {
1313
let max_size = if path.ends_with(".html") {
1414
config.max_file_size_html
1515
} else {
1616
config.max_file_size
1717
};
1818

19-
Ok(File(db::file::get_path(pool, path, max_size)?))
19+
Ok(File(storage.get(path, max_size)?))
2020
}
2121

2222
/// Consumes File and creates a iron response
@@ -57,9 +57,9 @@ pub struct DatabaseFileHandler;
5757
impl Handler for DatabaseFileHandler {
5858
fn handle(&self, req: &mut Request) -> IronResult<Response> {
5959
let path = req.url.path().join("/");
60-
let pool = extension!(req, Pool);
60+
let storage = extension!(req, Storage);
6161
let config = extension!(req, Config);
62-
if let Ok(file) = File::from_path(pool.clone(), &path, &config) {
62+
if let Ok(file) = File::from_path(&storage, &path, &config) {
6363
Ok(file.serve())
6464
} else {
6565
Err(IronError::new(
@@ -85,7 +85,7 @@ mod tests {
8585
db.fake_release().create()?;
8686

8787
let mut file = File::from_path(
88-
db.pool(),
88+
&env.storage(),
8989
"rustdoc/fake-package/1.0.0/fake-package/index.html",
9090
&env.config(),
9191
)
@@ -128,7 +128,7 @@ mod tests {
128128

129129
let file = |path| {
130130
File::from_path(
131-
db.pool(),
131+
&env.storage(),
132132
&format!("rustdoc/dummy/0.1.0/{}", path),
133133
&env.config(),
134134
)

src/web/rustdoc.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
crate_details::CrateDetails, error::Nope, file::File, match_version, metrics,
88
page::WebPage, redirect_base, MatchSemver,
99
},
10-
Config,
10+
Config, Storage,
1111
};
1212
use iron::{
1313
headers::{CacheControl, CacheDirective, Expires, HttpDate},
@@ -104,12 +104,12 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
104104
// this URL is actually from a crate-internal path, serve it there instead
105105
return rustdoc_html_server_handler(req);
106106
} else {
107-
let pool = extension!(req, Pool);
107+
let storage = extension!(req, Storage);
108108
let config = extension!(req, Config);
109109

110110
let path = req.url.path();
111111
let path = path.join("/");
112-
match File::from_path(pool.clone(), &path, &config) {
112+
match File::from_path(&storage, &path, &config) {
113113
Ok(f) => return Ok(f.serve()),
114114
Err(..) => return Err(IronError::new(Nope::ResourceNotFound, status::NotFound)),
115115
}
@@ -218,6 +218,7 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
218218
let pool = extension!(req, Pool);
219219
let conn = pool.get()?;
220220
let config = extension!(req, Config);
221+
let storage = extension!(req, Storage);
221222
let mut req_path = req.url.path();
222223

223224
// Remove the name and version from the path
@@ -297,14 +298,14 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
297298
}
298299

299300
// Attempt to load the file from the database
300-
let file = if let Ok(file) = File::from_path(pool.clone(), &path, &config) {
301+
let file = if let Ok(file) = File::from_path(&storage, &path, &config) {
301302
file
302303
} else {
303304
// If it fails, we try again with /index.html at the end
304305
path.push_str("/index.html");
305306
req_path.push("index.html");
306307

307-
File::from_path(pool.clone(), &path, &config)
308+
File::from_path(&storage, &path, &config)
308309
.map_err(|_| IronError::new(Nope::ResourceNotFound, status::NotFound))?
309310
};
310311

@@ -351,7 +352,7 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
351352
"/{}/{}/{}",
352353
name,
353354
latest_version,
354-
path_for_version(&latest_path, &krate.doc_targets, pool.clone(), &config)
355+
path_for_version(&latest_path, &krate.doc_targets, &storage, &config)
355356
)
356357
} else {
357358
format!("/crate/{}/{}", name, latest_version)
@@ -400,11 +401,11 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
400401
fn path_for_version(
401402
req_path: &[&str],
402403
known_platforms: &[String],
403-
pool: Pool,
404+
storage: &Storage,
404405
config: &Config,
405406
) -> String {
406407
// Simple case: page exists in the latest version, so just change the version number
407-
if File::from_path(pool, &req_path.join("/"), config).is_ok() {
408+
if File::from_path(storage, &req_path.join("/"), config).is_ok() {
408409
// NOTE: this adds 'index.html' if it wasn't there before
409410
return req_path[3..].join("/");
410411
}
@@ -442,6 +443,7 @@ pub fn target_redirect_handler(req: &mut Request) -> IronResult<Response> {
442443

443444
let pool = extension!(req, Pool);
444445
let conn = pool.get()?;
446+
let storage = extension!(req, Storage);
445447
let config = extension!(req, Config);
446448
let base = redirect_base(req);
447449

@@ -467,12 +469,7 @@ pub fn target_redirect_handler(req: &mut Request) -> IronResult<Response> {
467469
file_path
468470
};
469471

470-
let path = path_for_version(
471-
&file_path,
472-
&crate_details.doc_targets,
473-
pool.clone(),
474-
&config,
475-
);
472+
let path = path_for_version(&file_path, &crate_details.doc_targets, &storage, &config);
476473
let url = format!(
477474
"{base}/{name}/{version}/{path}",
478475
base = base,
@@ -572,10 +569,10 @@ impl Handler for SharedResourceHandler {
572569
let filename = path.last().unwrap(); // unwrap is fine: vector is non-empty
573570
let suffix = filename.split('.').last().unwrap(); // unwrap is fine: split always works
574571
if ["js", "css", "woff", "svg"].contains(&suffix) {
575-
let pool = extension!(req, Pool);
572+
let storage = extension!(req, Storage);
576573
let config = extension!(req, Config);
577574

578-
if let Ok(file) = File::from_path(pool.clone(), filename, &config) {
575+
if let Ok(file) = File::from_path(&storage, filename, &config) {
579576
return Ok(file.serve());
580577
}
581578
}

src/web/source.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
db::Pool,
55
impl_webpage,
66
web::{error::Nope, file::File as DbFile, page::WebPage, MetaData},
7-
Config,
7+
Config, Storage,
88
};
99
use iron::{status::Status, IronError, IronResult, Request, Response};
1010
use postgres::Connection;
@@ -186,12 +186,13 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
186186

187187
let pool = extension!(req, Pool);
188188
let conn = pool.get()?;
189+
let storage = extension!(req, Storage);
189190
let config = extension!(req, Config);
190191

191192
// try to get actual file first
192193
// skip if request is a directory
193194
let file = if !file_path.ends_with('/') {
194-
DbFile::from_path(pool.clone(), &file_path, &config).ok()
195+
DbFile::from_path(&storage, &file_path, &config).ok()
195196
} else {
196197
None
197198
};

0 commit comments

Comments
 (0)