Skip to content

Add json version of search route; #145 #161

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

Closed
wants to merge 1 commit 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
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