Skip to content

Add support for a CDN in front of crates/readmes #1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Default for Config {
env("S3_SECRET_KEY"),
&api_protocol,
),
cdn: env::var("S3_CDN").ok(),
proxy: None,
}
}
Expand All @@ -86,6 +87,7 @@ impl Default for Config {
env::var("S3_SECRET_KEY").unwrap_or_default(),
&api_protocol,
),
cdn: env::var("S3_CDN").ok(),
proxy: None,
}
}
Expand All @@ -105,6 +107,7 @@ impl Default for Config {
env::var("S3_SECRET_KEY").unwrap_or_default(),
&api_protocol,
),
cdn: env::var("S3_CDN").ok(),
proxy: None,
}
} else {
Expand Down
1 change: 1 addition & 0 deletions src/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ fn app() -> (
&api_protocol,
),
proxy: Some(proxy),
cdn: None,
};

let config = cargo_registry::Config {
Expand Down
35 changes: 25 additions & 10 deletions src/uploaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum Uploader {
/// For test usage with a proxy.
S3 {
bucket: s3::Bucket,
cdn: Option<String>,
proxy: Option<String>,
},

Expand All @@ -45,11 +46,18 @@ impl Uploader {
/// It returns `None` if the current `Uploader` is `NoOp`.
pub fn crate_location(&self, crate_name: &str, version: &str) -> Option<String> {
match *self {
Uploader::S3 { ref bucket, .. } => Some(format!(
"https://{}/{}",
bucket.host(),
Uploader::crate_path(crate_name, version)
)),
Uploader::S3 {
ref bucket,
ref cdn,
..
} => {
let host = match *cdn {
Some(ref s) => s.clone(),
None => bucket.host(),
};
let path = Uploader::crate_path(crate_name, version);
Some(format!("https://{}/{}", host, path))
}
Uploader::Local => Some(format!("/{}", Uploader::crate_path(crate_name, version))),
Uploader::NoOp => None,
}
Expand All @@ -61,11 +69,18 @@ impl Uploader {
/// It returns `None` if the current `Uploader` is `NoOp`.
pub fn readme_location(&self, crate_name: &str, version: &str) -> Option<String> {
match *self {
Uploader::S3 { ref bucket, .. } => Some(format!(
"https://{}/{}",
bucket.host(),
Uploader::readme_path(crate_name, version)
)),
Uploader::S3 {
ref bucket,
ref cdn,
..
} => {
let host = match *cdn {
Some(ref s) => s.clone(),
None => bucket.host(),
};
let path = Uploader::readme_path(crate_name, version);
Some(format!("https://{}/{}", host, path))
}
Uploader::Local => Some(format!("/{}", Uploader::readme_path(crate_name, version))),
Uploader::NoOp => None,
}
Expand Down