Skip to content

Vendor FontAwesome and Pure-CSS #981

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 5 commits into from
Aug 21, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
target
*.css
*.css.map
!vendor/**/*.css
.sass-cache
.vagrant
.rustwide
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ cargo run -- daemon --registry-watcher=disabled
cargo run -- queue add <CRATE> <VERSION>
```

### Updating vendored sources

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/).

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.
<!--
TODO: Whenever scss modules are avaliable, use [scss modules](https://sass-lang.com/documentation/at-rules/use#configuration)
instead of manually editing the `_variables.scss` file. Something like this should work:
```scss
@use "fontawesome" with (
$fa-font-path: "../-/static"
);
```
-->

### Contact

Docs.rs is run and maintained by the [docs.rs team](https://www.rust-lang.org/governance/teams/dev-tools#docs-rs).
Expand Down
7 changes: 6 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn main() {
println!("cargo:rerun-if-changed=templates/style/_navbar.scss");
println!("cargo:rerun-if-changed=templates/menu.js");
println!("cargo:rerun-if-changed=templates/index.js");
println!("cargo:rerun-if-changed=vendor/");
// TODO: are these right?
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");
Expand Down Expand Up @@ -63,7 +64,11 @@ fn compile_sass() -> Result<(), Box<dyn Error>> {
let mut context = Context::new_file(format!("{}/base.scss", STYLE_DIR))?;
context.set_options(Options {
output_style: OutputStyle::Compressed,
include_paths: vec![STYLE_DIR.to_owned()],
include_paths: vec![
STYLE_DIR.to_owned(),
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/fontawesome/scss").to_owned(),
concat!(env!("CARGO_MANIFEST_DIR"), "/vendor/pure-css/css").to_owned(),
],
..Default::default()
});

Expand Down
2 changes: 2 additions & 0 deletions dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ RUN find src -name "*.rs" -exec touch {} \;
COPY templates/style templates/style
COPY templates/index.js templates/
COPY templates/menu.js templates/
COPY vendor vendor/

RUN cargo build --release

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

WORKDIR /opt/docsrs
ENTRYPOINT ["/opt/docsrs/entrypoint.sh"]
Expand Down
8 changes: 4 additions & 4 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Storage {

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

fn store_inner(
Expand Down Expand Up @@ -276,7 +276,7 @@ mod test {
crate::test::init_logger();
let files = get_file_list(env::current_dir().unwrap());
assert!(files.is_ok());
assert!(files.unwrap().len() > 0);
assert!(!files.unwrap().is_empty());

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

storage.store_blobs(vec![small_blob.clone(), big_blob.clone()])?;
storage.store_blobs(vec![small_blob.clone(), big_blob])?;

let blob = storage.get("small-blob.bin", MAX_SIZE)?;
assert_eq!(blob.content.len(), small_blob.content.len());
Expand Down Expand Up @@ -480,7 +480,7 @@ mod backend_tests {
mime: "text/rust".into(),
content,
path: format!("{}.rs", i),
date_updated: now.clone(),
date_updated: now,
compression: None,
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ mod tests {
.create()?;

let details = CrateDetails::new(&mut db.conn(), "foo", "0.0.1").unwrap();
let mut owners = details.owners.clone();
let mut owners = details.owners;
owners.sort();
assert_eq!(
owners,
Expand Down
6 changes: 3 additions & 3 deletions src/web/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ mod tests {
("/", "/"),
("/crate/hexponent/0.2.0", "/crate/:name/:version"),
("/crate/rcc/0.0.0", "/crate/:name/:version"),
("/index.js", "static resource"),
("/menu.js", "static resource"),
("/-/static/index.js", "static resource"),
("/-/static/menu.js", "static resource"),
("/opensearch.xml", "static resource"),
("/releases", "/releases"),
("/releases/feed", "static resource"),
Expand All @@ -131,7 +131,7 @@ mod tests {
("/releases/recent/1", "/releases/recent/:page"),
("/robots.txt", "static resource"),
("/sitemap.xml", "static resource"),
("/style.css", "static resource"),
("/-/static/style.css", "static resource"),
];

wrapper(|env| {
Expand Down
53 changes: 1 addition & 52 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mod routes;
mod rustdoc;
mod sitemap;
mod source;
mod statics;

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

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

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

fn style_css_handler(_: &mut Request) -> IronResult<Response> {
let mut response = Response::with((status::Ok, STYLE_CSS));
let cache = vec![
CacheDirective::Public,
CacheDirective::MaxAge(STATIC_FILE_CACHE_DURATION as u32),
];

response
.headers
.set(ContentType("text/css".parse().unwrap()));
response.headers.set(CacheControl(cache));

Ok(response)
}

fn load_js(file_path_str: &'static str) -> IronResult<Response> {
let mut response = Response::with((status::Ok, file_path_str));
let cache = vec![
CacheDirective::Public,
CacheDirective::MaxAge(STATIC_FILE_CACHE_DURATION as u32),
];
response
.headers
.set(ContentType("application/javascript".parse().unwrap()));
response.headers.set(CacheControl(cache));

Ok(response)
}

fn opensearch_xml_handler(_: &mut Request) -> IronResult<Response> {
let mut response = Response::with((status::Ok, OPENSEARCH_XML));
let cache = vec![
Expand All @@ -539,26 +508,6 @@ fn opensearch_xml_handler(_: &mut Request) -> IronResult<Response> {
Ok(response)
}

fn ico_handler(req: &mut Request) -> IronResult<Response> {
if let Some(&"favicon.ico") = req.url.path().last() {
// if we're looking for exactly "favicon.ico", we need to defer to the handler that loads
// from `public_html`, so return a 404 here to make the main handler carry on
Err(IronError::new(
error::Nope::ResourceNotFound,
status::NotFound,
))
} else {
// if we're looking for something like "favicon-20190317-1.35.0-nightly-c82834e2b.ico",
// redirect to the plain one so that the above branch can trigger with the correct filename
let url = ctry!(
req,
Url::parse(&format!("{}/favicon.ico", redirect_base(req))),
);

Ok(redirect(url))
}
}

/// MetaData used in header
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub(crate) struct MetaData {
Expand Down
6 changes: 1 addition & 5 deletions src/web/routes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::metrics::RequestRecorder;
use crate::web::{INDEX_JS, MENU_JS};
use iron::middleware::Handler;
use iron::Request;
use router::Router;
use std::collections::HashSet;

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
{
// route .ico files into their dedicated handler so that docs.rs's favicon is always
// displayed
return super::ico_handler(req);
return super::statics::ico_handler(req);
}

let router = extension!(req, Router);
Expand Down
Loading