Skip to content

Commit 79af685

Browse files
Merge #1152
1152: Add support for a CDN in front of crates/readmes r=carols10cents This should hopefully allow us to configure a CDN on Heroku (namely CloudFront) to distribute crates/readmes through.
2 parents c797fbb + f503cfe commit 79af685

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl Default for Config {
6969
env("S3_SECRET_KEY"),
7070
&api_protocol,
7171
),
72+
cdn: env::var("S3_CDN").ok(),
7273
proxy: None,
7374
}
7475
}
@@ -89,6 +90,7 @@ impl Default for Config {
8990
env::var("S3_SECRET_KEY").unwrap_or_default(),
9091
&api_protocol,
9192
),
93+
cdn: env::var("S3_CDN").ok(),
9294
proxy: None,
9395
}
9496
}
@@ -108,6 +110,7 @@ impl Default for Config {
108110
env::var("S3_SECRET_KEY").unwrap_or_default(),
109111
&api_protocol,
110112
),
113+
cdn: env::var("S3_CDN").ok(),
111114
proxy: None,
112115
}
113116
} else {

src/tests/all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ fn app() -> (
143143
&api_protocol,
144144
),
145145
proxy: Some(proxy),
146+
cdn: None,
146147
};
147148

148149
let config = cargo_registry::Config {

src/uploaders.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub enum Uploader {
2020
/// For test usage with a proxy.
2121
S3 {
2222
bucket: s3::Bucket,
23+
cdn: Option<String>,
2324
proxy: Option<String>,
2425
},
2526

@@ -45,11 +46,18 @@ impl Uploader {
4546
/// It returns `None` if the current `Uploader` is `NoOp`.
4647
pub fn crate_location(&self, crate_name: &str, version: &str) -> Option<String> {
4748
match *self {
48-
Uploader::S3 { ref bucket, .. } => Some(format!(
49-
"https://{}/{}",
50-
bucket.host(),
51-
Uploader::crate_path(crate_name, version)
52-
)),
49+
Uploader::S3 {
50+
ref bucket,
51+
ref cdn,
52+
..
53+
} => {
54+
let host = match *cdn {
55+
Some(ref s) => s.clone(),
56+
None => bucket.host(),
57+
};
58+
let path = Uploader::crate_path(crate_name, version);
59+
Some(format!("https://{}/{}", host, path))
60+
}
5361
Uploader::Local => Some(format!("/{}", Uploader::crate_path(crate_name, version))),
5462
Uploader::NoOp => None,
5563
}
@@ -61,11 +69,18 @@ impl Uploader {
6169
/// It returns `None` if the current `Uploader` is `NoOp`.
6270
pub fn readme_location(&self, crate_name: &str, version: &str) -> Option<String> {
6371
match *self {
64-
Uploader::S3 { ref bucket, .. } => Some(format!(
65-
"https://{}/{}",
66-
bucket.host(),
67-
Uploader::readme_path(crate_name, version)
68-
)),
72+
Uploader::S3 {
73+
ref bucket,
74+
ref cdn,
75+
..
76+
} => {
77+
let host = match *cdn {
78+
Some(ref s) => s.clone(),
79+
None => bucket.host(),
80+
};
81+
let path = Uploader::readme_path(crate_name, version);
82+
Some(format!("https://{}/{}", host, path))
83+
}
6984
Uploader::Local => Some(format!("/{}", Uploader::readme_path(crate_name, version))),
7085
Uploader::NoOp => None,
7186
}

0 commit comments

Comments
 (0)