Skip to content

Commit

Permalink
Add json version of search route; rust-lang#145
Browse files Browse the repository at this point in the history
  • Loading branch information
hobofan committed Oct 13, 2017
1 parent 13da7b2 commit 1cd4295
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/web/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use iron::Handler;
use iron::status;
use web::page::Page;
use std::fmt;
use time;

#[derive(Debug, Copy, Clone)]
pub enum Nope {
Expand Down Expand Up @@ -48,7 +49,21 @@ impl Handler for Nope {
Nope::NoResults => {
use params::{Params, Value};
let params = req.get::<Params>().unwrap();
if let Some(&Value::String(ref query)) = params.find(&["query"]) {

if req.url.path().join("/").ends_with(".json") {
use iron::status;
use iron::headers::{Expires, HttpDate, CacheControl, CacheDirective, ContentType,
AccessControlAllowOrigin};

let mut resp = Response::with((status::Ok, "[]"));
resp.headers.set(ContentType("application/json".parse().unwrap()));
resp.headers.set(Expires(HttpDate(time::now())));
resp.headers.set(CacheControl(vec![CacheDirective::NoCache,
CacheDirective::NoStore,
CacheDirective::MustRevalidate]));
resp.headers.set(AccessControlAllowOrigin::Any);
Ok(resp)
} else if let Some(&Value::String(ref query)) = params.find(&["query"]) {
// this used to be a search
Page::new(Vec::<super::releases::Release>::new())
.set_status(status::NotFound)
Expand Down
3 changes: 3 additions & 0 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ impl CratesfyiHandler {
router.get("/releases/search",
releases::search_handler,
"releases_search");
router.get("/releases/search.json",
releases::search_handler,
"releases_search_json");
router.get("/releases/queue",
releases::build_queue_handler,
"releases_queue");
Expand Down
23 changes: 19 additions & 4 deletions src/web/releases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,25 @@ pub fn search_handler(req: &mut Request) -> IronResult<Response> {
.ok_or(IronError::new(Nope::NoResults, status::NotFound))
.and_then(|(_, results)| {
// FIXME: There is no pagination
Page::new(results)
.set("search_query", &query)
.title(&format!("Search results for '{}'", query))
.to_resp("releases")
if req.url.path().join("/").ends_with(".json") {
use iron::status;
use iron::headers::{Expires, HttpDate, CacheControl, CacheDirective, ContentType,
AccessControlAllowOrigin};

let mut resp = Response::with((status::Ok, results.to_json().to_string()));
resp.headers.set(ContentType("application/json".parse().unwrap()));
resp.headers.set(Expires(HttpDate(time::now())));
resp.headers.set(CacheControl(vec![CacheDirective::NoCache,
CacheDirective::NoStore,
CacheDirective::MustRevalidate]));
resp.headers.set(AccessControlAllowOrigin::Any);
Ok(resp)
} else {
Page::new(results)
.set("search_query", &query)
.title(&format!("Search results for '{}'", query))
.to_resp("releases")
}
})
} else {
Err(IronError::new(Nope::NoResults, status::NotFound))
Expand Down

0 comments on commit 1cd4295

Please sign in to comment.