Skip to content

Commit fa216e3

Browse files
Merge #951
951: Serve locally uploaded crates from a different folder r=carols10cents Fix #934, serve locally uploaded crates from a different location since `dist/` gets recreated each time the front end restarts.
2 parents 4cbac26 + d3671b7 commit fa216e3

File tree

9 files changed

+59
-22
lines changed

9 files changed

+59
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/.sass-cache
1414
/connect.lock
1515
/coverage/*
16+
/local_uploads
1617
/libpeerconnection.log
1718
npm-debug.log
1819
yarn-error.log

docs/ARCHITECTURE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ These files are mostly only relevant when running crates.io's code in developmen
9393
* `.env.sample` - Example environment file checked into the repository
9494
* `.git/` - The git repository; not available in all deployments (e.g. Heroku)
9595
* `.gitignore` - Configures git to ignore certain files and folders
96+
* `local_uploads/` - Serves crates and readmes that are published to the
97+
local development environment
9698
* `script/init-local-index.sh` - Creates registry repositories used during development
9799
* `tmp/` - Temporary files created during development; when deployed on Heroku this is the only
98100
writable directory - (ignored in `.gitignore`)

docs/CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ sense as a name for this flag](https://github.com/rust-lang/cargo/issues/3797).
409409
410410
Note that when you're running crates.io in development mode without the S3
411411
variables set (which is what we've done in these setup steps), the crate files
412-
will be stored in `dist/local_uploads/crates` and served from there when a
413-
crate is downloaded. This directory gets cleared out if you stop and restart
414-
the frontend. If you try to install a crate from your local crates.io and
415-
`cargo` can't find the crate files, that's probably why.
412+
will be stored in `local_uploads/crates` and served from there when a
413+
crate is downloaded. If you try to install a crate from your local crates.io and
414+
`cargo` can't find the crate files, it is probably because this directory does not
415+
exist.
416416
417417
##### Downloading a crate from your local crates.io
418418

src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ impl Default for Config {
111111
// If we don't set the `S3_BUCKET` variable, we'll use a development-only
112112
// uploader that makes it possible to run and publish to a locally-running
113113
// crates.io instance without needing to set up an account and a bucket in S3.
114-
println!("Using local uploader, crate files will be in the dist directory");
114+
println!(
115+
"Using local uploader, crate files will be in the local_uploads directory"
116+
);
115117
Uploader::Local
116118
}
117119
}

src/dist.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! This module implements middleware to serve the compiled emberjs
2+
//! frontend
13
use std::error::Error;
24

35
use conduit::{Request, Response, Handler};

src/krate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,7 @@ fn parse_new_headers(req: &mut Request) -> CargoResult<(upload::NewCrate, User)>
10921092
}
10931093

10941094
/// Handles the `GET /crates/:crate_id/:version/download` route.
1095+
/// This returns a URL to the location where the crate is stored.
10951096
pub fn download(req: &mut Request) -> CargoResult<Response> {
10961097
let crate_name = &req.params()["crate_id"];
10971098
let version = &req.params()["version"];

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub mod user;
9696
pub mod util;
9797
pub mod version;
9898

99+
mod local_upload;
99100
mod pagination;
100101

101102
/// Used for setting different values depending on whether the app is being run in production,
@@ -217,6 +218,7 @@ pub fn middleware(app: Arc<App>) -> MiddlewareBuilder {
217218
if env == Env::Development {
218219
// DebugMiddleware is defined below to print logs for each request.
219220
m.add(DebugMiddleware);
221+
m.around(local_upload::Middleware::default());
220222
}
221223

222224
if env != Env::Test {

src/local_upload.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! This module implements middleware to serve crates and readmes
2+
//! from the `local_uploads/` directory. This is only used in
3+
//! development environments.
4+
use std::error::Error;
5+
6+
use conduit::{Request, Response, Handler};
7+
use conduit_static::Static;
8+
use conduit_middleware::AroundMiddleware;
9+
10+
// Can't derive debug because of Handler and Static.
11+
#[allow(missing_debug_implementations)]
12+
pub struct Middleware {
13+
handler: Option<Box<Handler>>,
14+
local_uploads: Static,
15+
}
16+
17+
impl Default for Middleware {
18+
fn default() -> Middleware {
19+
Middleware {
20+
handler: None,
21+
local_uploads: Static::new("local_uploads"),
22+
}
23+
}
24+
}
25+
26+
impl AroundMiddleware for Middleware {
27+
fn with_handler(&mut self, handler: Box<Handler>) {
28+
self.handler = Some(handler);
29+
}
30+
}
31+
32+
impl Handler for Middleware {
33+
fn call(&self, req: &mut Request) -> Result<Response, Box<Error + Send>> {
34+
match self.local_uploads.call(req) {
35+
Ok(ref resp) if resp.status.0 == 404 => {}
36+
ret => return ret,
37+
}
38+
39+
self.handler.as_ref().unwrap().call(req)
40+
}
41+
}

src/uploaders.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ impl Uploader {
5050
Uploader::crate_path(crate_name, version)
5151
))
5252
}
53-
Uploader::Local => {
54-
Some(format!(
55-
"/local_uploads/{}",
56-
Uploader::crate_path(crate_name, version)
57-
))
58-
}
53+
Uploader::Local => Some(format!("/{}", Uploader::crate_path(crate_name, version))),
5954
Uploader::NoOp => None,
6055
}
6156
}
@@ -73,12 +68,7 @@ impl Uploader {
7368
Uploader::readme_path(crate_name, version)
7469
))
7570
}
76-
Uploader::Local => {
77-
Some(format!(
78-
"/local_uploads/{}",
79-
Uploader::readme_path(crate_name, version)
80-
))
81-
}
71+
Uploader::Local => Some(format!("/{}", Uploader::readme_path(crate_name, version))),
8272
Uploader::NoOp => None,
8373
}
8474
}
@@ -136,11 +126,7 @@ impl Uploader {
136126
Ok((Some(String::from(path)), cksum))
137127
}
138128
Uploader::Local => {
139-
let filename = env::current_dir()
140-
.unwrap()
141-
.join("dist")
142-
.join("local_uploads")
143-
.join(path);
129+
let filename = env::current_dir().unwrap().join("local_uploads").join(path);
144130
let dir = filename.parent().unwrap();
145131
fs::create_dir_all(dir)?;
146132
let mut file = File::create(&filename)?;

0 commit comments

Comments
 (0)