Skip to content
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

RFC: Server-side rendering #1173

Closed
wants to merge 5 commits into from
Closed
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
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ conduit-static = "0.8"
conduit-git-http-backend = "0.8"
civet = "0.9"

handlebars = "0.29.1"

[dev-dependencies]
conduit-test = "0.8"
hyper = "0.11"
Expand Down
22 changes: 12 additions & 10 deletions app/styles/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ body {
@include align-items(center);
}

.ember-application > div {
body > div {
width: 960px;
@media only screen and (max-width: 960px) {
width: 100%;
Expand Down Expand Up @@ -99,19 +99,13 @@ body {
}

.current-user-links {
display: none;
left: auto;
right: 0;
min-width: 200px;

&.open { display: block; }
}
#doc-links {
display: none;
left: auto;
min-width: 150px;

&.open { display: block; }
}
form.search {
@include display-flex;
Expand Down Expand Up @@ -266,9 +260,13 @@ pre {
button.dropdown, a.dropdown {
color: inherit;
cursor: pointer;
.arrow { font-size: 50%; display: inline-block; vertical-align: middle; }
.arrow {
font-size: 50%;
display: inline-block;
vertical-align: middle;
}
.arrow::after { content: "▼"; }
&.active .arrow::after { content: "▲"; }
.dropdown-container:hover & .arrow::after { content: "▲"; }
}

ul.dropdown {
Expand Down Expand Up @@ -300,12 +298,16 @@ ul.dropdown {
}
}
li.last { border-top: 1px solid $gray-border; }
&.open {

display: none;

.dropdown-container:hover & {
display: block;
visibility: visible;
opacity: 1;
}
}

.dropdown-container {
display: inline-block;
position: relative;
Expand Down
3 changes: 2 additions & 1 deletion app/styles/crate.scss
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@
.cur, .total { color: $main-color; font-weight: bold; }

.dropdown-container { font-size: 85%; }
a.dropdown {
button.dropdown, a.dropdown {
background-color: $main-bg-dark;
padding: 10px;
display: inline-block;
@include border-radius(5px);
border-color: transparent;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn main() {
.unwrap_or(8888)
};
let threads = if config.env == Env::Development {
1
4
} else {
50
};
Expand Down
29 changes: 17 additions & 12 deletions src/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ impl<'a> NewCategory<'a> {

/// Handles the `GET /categories` route.
pub fn index(req: &mut Request) -> CargoResult<Response> {
Ok(req.json(&index_json(req)?))
}

pub fn index_json(req: &Request) -> CargoResult<R> {
let conn = req.db_conn()?;
let (offset, limit) = req.pagination(10, 100)?;
let query = req.query();
Expand All @@ -197,20 +201,21 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
// Query for the total count of categories
let total = Category::count_toplevel(&conn)?;

#[derive(Serialize)]
struct R {
categories: Vec<EncodableCategory>,
meta: Meta,
}
#[derive(Serialize)]
struct Meta {
total: i64,
}

Ok(req.json(&R {
Ok(R {
categories: categories,
meta: Meta { total: total },
}))
})
}

#[derive(Serialize, Debug)]
pub struct R {
categories: Vec<EncodableCategory>,
meta: Meta,
}
#[derive(Serialize, Debug)]
#[allow(missing_copy_implementations)]
pub struct Meta {
total: i64,
}

/// Handles the `GET /categories/:category_id` route.
Expand Down
71 changes: 18 additions & 53 deletions src/dist.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,26 @@
//! This module implements middleware to serve the compiled emberjs
//! frontend
//! This module implements middleware to serve the compiled, static assets.
use std::error::Error;

use conduit::{Handler, Request, Response};
use conduit_static::Static;
use conduit_middleware::AroundMiddleware;
use conduit_static;
use conduit_middleware::Middleware;

use util::RequestProxy;
#[allow(missing_debug_implementations, missing_copy_implementations)]
pub struct Static;

// Can't derive debug because of Handler and Static.
#[allow(missing_debug_implementations)]
pub struct Middleware {
handler: Option<Box<Handler>>,
dist: Static,
}

impl Default for Middleware {
fn default() -> Middleware {
Middleware {
handler: None,
dist: Static::new("dist"),
}
}
}

impl AroundMiddleware for Middleware {
fn with_handler(&mut self, handler: Box<Handler>) {
self.handler = Some(handler);
}
}

impl Handler for Middleware {
fn call(&self, req: &mut Request) -> Result<Response, Box<Error + Send>> {
// First, attempt to serve a static file. If we're missing a static
// file, then keep going.
match self.dist.call(req) {
Ok(ref resp) if resp.status.0 == 404 => {}
ret => return ret,
}

// Second, if we're requesting html, then we've only got one page so
// serve up that page. Otherwise proxy on to the rest of the app.
let wants_html = req.headers()
.find("Accept")
.map(|accept| accept.iter().any(|s| s.contains("html")))
.unwrap_or(false);
// If the route starts with /api, just assume they want the API
// response. Someone is either debugging or trying to download a crate.
let is_api_path = req.path().starts_with("/api");
if wants_html && !is_api_path {
self.dist.call(&mut RequestProxy {
other: req,
path: Some("/index.html"),
method: None,
})
} else {
self.handler.as_ref().unwrap().call(req)
impl Middleware for Static {
fn after(&self, req: &mut Request, resp: Result<Response, Box<Error+Send>>)
-> Result<Response, Box<Error+Send>>
{
match resp {
Ok(resp) => {
if resp.status.0 == 404 {
conduit_static::Static::new("dist").call(req)
} else {
Ok(resp)
}
}
Err(resp) => Err(resp)
}
}
}
12 changes: 12 additions & 0 deletions src/html/_categories.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ul>
{{#each categories as |category|}}
<li>
<a href="/categories/{{category.slug}}" class="name">
<span>{{category.category}} ({{format_num category.crates_cnt}})</span>
<div class="arrow-in-list">
{{embed_svg "right-arrow"}}
</div>
</a>
</li>
{{/each}}
</ul>
12 changes: 12 additions & 0 deletions src/html/_crates.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ul>
{{#each crates as |crate|}}
<li>
<a href="/crates/{{crate.id}}" class="name">
<span>{{crate.name}} ({{crate.max_version}})</span>
<div class="arrow-in-list">
{{embed_svg "right-arrow"}}
</div>
</a>
</li>
{{/each}}
</ul>
12 changes: 12 additions & 0 deletions src/html/_keywords.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ul>
{{#each keywords as |keyword|}}
<li>
<a href="/keywords/{{keyword.slug}}" class="name">
<span>{{keyword.id}} ({{format_num keyword.crates_cnt}})</span>
<div class="arrow-in-list">
{{embed_svg "right-arrow"}}
</div>
</a>
</li>
{{/each}}
</ul>
Loading