Skip to content

Commit 08d5d1a

Browse files
committed
Add support for a CDN in front of crates/readmes
This should hopefully allow us to configure a CDN on Heroku (namely CloudFront) to distribute crates/readmes through.
1 parent 630e151 commit 08d5d1a

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl Default for Config {
6666
env("S3_SECRET_KEY"),
6767
&api_protocol,
6868
),
69+
cdn: env::var("S3_CDN").ok(),
6970
proxy: None,
7071
}
7172
}
@@ -86,6 +87,7 @@ impl Default for Config {
8687
env::var("S3_SECRET_KEY").unwrap_or_default(),
8788
&api_protocol,
8889
),
90+
cdn: env::var("S3_CDN").ok(),
8991
proxy: None,
9092
}
9193
}
@@ -105,6 +107,7 @@ impl Default for Config {
105107
env::var("S3_SECRET_KEY").unwrap_or_default(),
106108
&api_protocol,
107109
),
110+
cdn: env::var("S3_CDN").ok(),
108111
proxy: None,
109112
}
110113
} else {

src/uploaders.rs

Lines changed: 17 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,14 @@ 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 { ref bucket, ref cdn, .. } => {
50+
let host = match *cdn {
51+
Some(ref s) => s.clone(),
52+
None => bucket.host(),
53+
};
54+
let path = Uploader::crate_path(crate_name, version);
55+
Some(format!("https://{}/{}", host, path))
56+
}
5357
Uploader::Local => Some(format!("/{}", Uploader::crate_path(crate_name, version))),
5458
Uploader::NoOp => None,
5559
}
@@ -61,11 +65,14 @@ impl Uploader {
6165
/// It returns `None` if the current `Uploader` is `NoOp`.
6266
pub fn readme_location(&self, crate_name: &str, version: &str) -> Option<String> {
6367
match *self {
64-
Uploader::S3 { ref bucket, .. } => Some(format!(
65-
"https://{}/{}",
66-
bucket.host(),
67-
Uploader::readme_path(crate_name, version)
68-
)),
68+
Uploader::S3 { ref bucket, .. } => {
69+
let host = match *cdn {
70+
Some(ref s) => s.clone(),
71+
None => bucket.host(),
72+
};
73+
let path = Uploader::readme_path(crate_name, version);
74+
Some(format!("https://{}/{}", host, path))
75+
}
6976
Uploader::Local => Some(format!("/{}", Uploader::readme_path(crate_name, version))),
7077
Uploader::NoOp => None,
7178
}

0 commit comments

Comments
 (0)