Skip to content

Commit 1965b14

Browse files
authored
Merge pull request #981 from Kixiron/vending-machine
Vendor FontAwesome and Pure-CSS
2 parents 2acd7ee + 59e856f commit 1965b14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+7879
-86
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
target
55
*.css
66
*.css.map
7+
!vendor/**/*.css
78
.sass-cache
89
.vagrant
910
.rustwide

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@ cargo run -- daemon --registry-watcher=disabled
210210
cargo run -- queue add <CRATE> <VERSION>
211211
```
212212

213+
### Updating vendored sources
214+
215+
The instructions & links for updating Font Awesome can be found [on their website](https://fontawesome.com/how-to-use/on-the-web/using-with/sass). Similarly, Pure-CSS also [explains on theirs](https://purecss.io/start/).
216+
217+
When updating Font Awesome, make sure to change `$fa-font-path` in `scss/_variables.scss` (it should be at the top of the file) to `../-/static`. This will point font awesome at the correct path from which to request font and icon resources.
218+
<!--
219+
TODO: Whenever scss modules are avaliable, use [scss modules](https://sass-lang.com/documentation/at-rules/use#configuration)
220+
instead of manually editing the `_variables.scss` file. Something like this should work:
221+
```scss
222+
@use "fontawesome" with (
223+
$fa-font-path: "../-/static"
224+
);
225+
```
226+
-->
227+
213228
### Contact
214229

215230
Docs.rs is run and maintained by the [docs.rs team](https://www.rust-lang.org/governance/teams/dev-tools#docs-rs).

build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fn main() {
2222
println!("cargo:rerun-if-changed=templates/style/_navbar.scss");
2323
println!("cargo:rerun-if-changed=templates/menu.js");
2424
println!("cargo:rerun-if-changed=templates/index.js");
25+
println!("cargo:rerun-if-changed=vendor/");
2526
// TODO: are these right?
2627
println!("cargo:rerun-if-changed=.git/HEAD");
2728
println!("cargo:rerun-if-changed=.git/index");
@@ -63,7 +64,11 @@ fn compile_sass() -> Result<(), Box<dyn Error>> {
6364
let mut context = Context::new_file(format!("{}/base.scss", STYLE_DIR))?;
6465
context.set_options(Options {
6566
output_style: OutputStyle::Compressed,
66-
include_paths: vec![STYLE_DIR.to_owned()],
67+
include_paths: vec![
68+
STYLE_DIR.to_owned(),
69+
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/fontawesome/scss").to_owned(),
70+
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned(),
71+
],
6772
..Default::default()
6873
});
6974

dockerfiles/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ RUN find src -name "*.rs" -exec touch {} \;
5050
COPY templates/style templates/style
5151
COPY templates/index.js templates/
5252
COPY templates/menu.js templates/
53+
COPY vendor vendor/
5354

5455
RUN cargo build --release
5556

@@ -75,6 +76,7 @@ COPY --from=build /build/target/release/cratesfyi /usr/local/bin
7576
COPY static /opt/docsrs/prefix/public_html
7677
COPY templates /opt/docsrs/templates
7778
COPY dockerfiles/entrypoint.sh /opt/docsrs/
79+
COPY vendor vendor/
7880

7981
WORKDIR /opt/docsrs
8082
ENTRYPOINT ["/opt/docsrs/entrypoint.sh"]

src/storage/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Storage {
190190

191191
#[cfg(test)]
192192
pub(crate) fn store_blobs(&self, blobs: Vec<Blob>) -> Result<(), Error> {
193-
self.store_inner(blobs.into_iter().map(|blob| Ok(blob)))
193+
self.store_inner(blobs.into_iter().map(Ok))
194194
}
195195

196196
fn store_inner(
@@ -276,7 +276,7 @@ mod test {
276276
crate::test::init_logger();
277277
let files = get_file_list(env::current_dir().unwrap());
278278
assert!(files.is_ok());
279-
assert!(files.unwrap().len() > 0);
279+
assert!(!files.unwrap().is_empty());
280280

281281
let files = get_file_list(env::current_dir().unwrap().join("Cargo.toml")).unwrap();
282282
assert_eq!(files[0], std::path::Path::new("Cargo.toml"));
@@ -375,7 +375,7 @@ mod backend_tests {
375375
compression: None,
376376
};
377377

378-
storage.store_blobs(vec![small_blob.clone(), big_blob.clone()])?;
378+
storage.store_blobs(vec![small_blob.clone(), big_blob])?;
379379

380380
let blob = storage.get("small-blob.bin", MAX_SIZE)?;
381381
assert_eq!(blob.content.len(), small_blob.content.len());
@@ -480,7 +480,7 @@ mod backend_tests {
480480
mime: "text/rust".into(),
481481
content,
482482
path: format!("{}.rs", i),
483-
date_updated: now.clone(),
483+
date_updated: now,
484484
compression: None,
485485
}
486486
})

src/web/crate_details.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ mod tests {
696696
.create()?;
697697

698698
let details = CrateDetails::new(&mut db.conn(), "foo", "0.0.1").unwrap();
699-
let mut owners = details.owners.clone();
699+
let mut owners = details.owners;
700700
owners.sort();
701701
assert_eq!(
702702
owners,

src/web/metrics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ mod tests {
117117
("/", "/"),
118118
("/crate/hexponent/0.2.0", "/crate/:name/:version"),
119119
("/crate/rcc/0.0.0", "/crate/:name/:version"),
120-
("/index.js", "static resource"),
121-
("/menu.js", "static resource"),
120+
("/-/static/index.js", "static resource"),
121+
("/-/static/menu.js", "static resource"),
122122
("/opensearch.xml", "static resource"),
123123
("/releases", "/releases"),
124124
("/releases/feed", "static resource"),
@@ -131,7 +131,7 @@ mod tests {
131131
("/releases/recent/1", "/releases/recent/:page"),
132132
("/robots.txt", "static resource"),
133133
("/sitemap.xml", "static resource"),
134-
("/style.css", "static resource"),
134+
("/-/static/style.css", "static resource"),
135135
];
136136

137137
wrapper(|env| {

src/web/mod.rs

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ mod routes;
8787
mod rustdoc;
8888
mod sitemap;
8989
mod source;
90+
mod statics;
9091

9192
use crate::{impl_webpage, Context};
9293
use chrono::{DateTime, Utc};
@@ -110,9 +111,6 @@ use std::{borrow::Cow, env, fmt, net::SocketAddr, path::PathBuf, sync::Arc, time
110111

111112
/// Duration of static files for staticfile and DatabaseFileHandler (in seconds)
112113
const STATIC_FILE_CACHE_DURATION: u64 = 60 * 60 * 24 * 30 * 12; // 12 months
113-
const STYLE_CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
114-
const MENU_JS: &str = include_str!(concat!(env!("OUT_DIR"), "/menu.js"));
115-
const INDEX_JS: &str = include_str!(concat!(env!("OUT_DIR"), "/index.js"));
116114
const OPENSEARCH_XML: &[u8] = include_bytes!("opensearch.xml");
117115

118116
const DEFAULT_BIND: &str = "0.0.0.0:3000";
@@ -495,35 +493,6 @@ fn redirect_base(req: &Request) -> String {
495493
}
496494
}
497495

498-
fn style_css_handler(_: &mut Request) -> IronResult<Response> {
499-
let mut response = Response::with((status::Ok, STYLE_CSS));
500-
let cache = vec![
501-
CacheDirective::Public,
502-
CacheDirective::MaxAge(STATIC_FILE_CACHE_DURATION as u32),
503-
];
504-
505-
response
506-
.headers
507-
.set(ContentType("text/css".parse().unwrap()));
508-
response.headers.set(CacheControl(cache));
509-
510-
Ok(response)
511-
}
512-
513-
fn load_js(file_path_str: &'static str) -> IronResult<Response> {
514-
let mut response = Response::with((status::Ok, file_path_str));
515-
let cache = vec![
516-
CacheDirective::Public,
517-
CacheDirective::MaxAge(STATIC_FILE_CACHE_DURATION as u32),
518-
];
519-
response
520-
.headers
521-
.set(ContentType("application/javascript".parse().unwrap()));
522-
response.headers.set(CacheControl(cache));
523-
524-
Ok(response)
525-
}
526-
527496
fn opensearch_xml_handler(_: &mut Request) -> IronResult<Response> {
528497
let mut response = Response::with((status::Ok, OPENSEARCH_XML));
529498
let cache = vec![
@@ -539,26 +508,6 @@ fn opensearch_xml_handler(_: &mut Request) -> IronResult<Response> {
539508
Ok(response)
540509
}
541510

542-
fn ico_handler(req: &mut Request) -> IronResult<Response> {
543-
if let Some(&"favicon.ico") = req.url.path().last() {
544-
// if we're looking for exactly "favicon.ico", we need to defer to the handler that loads
545-
// from `public_html`, so return a 404 here to make the main handler carry on
546-
Err(IronError::new(
547-
error::Nope::ResourceNotFound,
548-
status::NotFound,
549-
))
550-
} else {
551-
// if we're looking for something like "favicon-20190317-1.35.0-nightly-c82834e2b.ico",
552-
// redirect to the plain one so that the above branch can trigger with the correct filename
553-
let url = ctry!(
554-
req,
555-
Url::parse(&format!("{}/favicon.ico", redirect_base(req))),
556-
);
557-
558-
Ok(redirect(url))
559-
}
560-
}
561-
562511
/// MetaData used in header
563512
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
564513
pub(crate) struct MetaData {

src/web/routes.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use super::metrics::RequestRecorder;
2-
use crate::web::{INDEX_JS, MENU_JS};
32
use iron::middleware::Handler;
4-
use iron::Request;
53
use router::Router;
64
use std::collections::HashSet;
75

@@ -12,12 +10,10 @@ pub(super) const DOC_RUST_LANG_ORG_REDIRECTS: &[&str] =
1210
pub(super) fn build_routes() -> Routes {
1311
let mut routes = Routes::new();
1412

15-
routes.static_resource("/style.css", super::style_css_handler);
16-
routes.static_resource("/index.js", |_: &mut Request| super::load_js(INDEX_JS));
17-
routes.static_resource("/menu.js", |_: &mut Request| super::load_js(MENU_JS));
1813
routes.static_resource("/robots.txt", super::sitemap::robots_txt_handler);
1914
routes.static_resource("/sitemap.xml", super::sitemap::sitemap_handler);
2015
routes.static_resource("/opensearch.xml", super::opensearch_xml_handler);
16+
routes.static_resource("/-/static/:file", super::statics::static_handler);
2117

2218
routes.internal_page("/", super::releases::home_page);
2319

src/web/rustdoc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
125125
{
126126
// route .ico files into their dedicated handler so that docs.rs's favicon is always
127127
// displayed
128-
return super::ico_handler(req);
128+
return super::statics::ico_handler(req);
129129
}
130130

131131
let router = extension!(req, Router);

0 commit comments

Comments
 (0)