Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Add basic example to page content renderer #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ before_script:
- .travis/download.sh deepwell
- .travis/download.sh deepwell-rpc
- .travis/download.sh ftml-rpc
- .travis/download.sh ftml
# Add rustfmt
- rustup component add rustfmt
# Add clippy
Expand Down
7 changes: 4 additions & 3 deletions src/route/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ macro_rules! try_io {
match $result {
Ok(object) => object,
Err(error) => {
let error = deepwell_core::error::Error::ServiceTransport(error).to_sendable();
use deepwell_core::error::Error;
let error = Error::ServiceTransport(error).to_sendable();

return HttpResponse::BadGateway().json(error);
}
}
};
}

macro_rules! try_io_result {
macro_rules! try_io_option {
($result:expr) => {
match $result {
Ok(object) => object,
Err(error) => {
let error = deepwell_core::error::Error::ServiceTransport(error).to_sendable();

return Ok(HttpResponse::BadGateway().json(error));
return Some(HttpResponse::BadGateway().json(error));
}
}
};
Expand Down
20 changes: 10 additions & 10 deletions src/route/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ pub async fn get_deepwell_page(
wiki_id: WikiId,
slug: &str,
deepwell: &web::Data<DeepwellPool>,
) -> std::result::Result<HttpResponse, ()> {
) -> Option<HttpResponse> {
debug!("Retrieving page by WikiId {} and slug {}", wiki_id, slug);
let result = deepwell
.claim()
.await
.get_page_contents(wiki_id.clone(), slug.to_string())
.await;

match try_io_result!(result) {
match try_io_option!(result) {
Ok(Some(page)) => {
// now run FTML on it
let mut contents = match String::from_utf8(page.to_vec()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we'll need to check if it's safe to switch from Box<[u8]> to String. I'll check if wikidot has any non-UTF-8 pages.

Ok(v) => v,
Err(e) => return Ok(HttpResponse::InternalServerError().json(format!("{:?}", e))),
Err(e) => return Some(HttpResponse::InternalServerError().json(format!("{:?}", e))),
};

// TODO: something that uses Deepwell
Expand All @@ -85,7 +85,7 @@ pub async fn get_deepwell_page(

let mut output = match renderer.transform(&mut contents, page_info, &remote_handle) {
Ok(o) => o,
Err(e) => return Ok(HttpResponse::InternalServerError().json(format!("{:?}", e))),
Err(e) => return Some(HttpResponse::InternalServerError().json(format!("{:?}", e))),
};

// TODO: use jinja/other templater to put this text into a better template
Expand All @@ -101,13 +101,13 @@ pub async fn get_deepwell_page(
buffer.push_str(&output.html);
buffer.push_str("</body></html>\n");

Ok(HttpResponse::Ok().json(buffer))
Some(HttpResponse::Ok().json(buffer))
}
Ok(None) => Err(()),
Ok(None) => None,
Err(e) => {
warn!("Failed to retrieve page contents: {}", e);

Ok(HttpResponse::InternalServerError().json(e))
Some(HttpResponse::InternalServerError().json(e))
}
}
}
Expand All @@ -118,8 +118,8 @@ pub async fn get_deepwell_page_wrapped(
deepwell: web::Data<DeepwellPool>,
) -> HttpResponse {
match get_deepwell_page(wiki_id, slug, &deepwell).await {
Ok(o) => o,
Err(()) => get_deepwell_page(wiki_id, "_404", &deepwell).await.unwrap(), // todo: make this safer
Some(o) => o,
None => get_deepwell_page(wiki_id, "_404", &deepwell).await.unwrap(), // todo: make this safer
Copy link
Member

@emmiegit emmiegit Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In cases where you have

match item {
    Some(x) => x,
    None => something_else(n),
}

you can do

item.unwrap_or_else(|| something_else(n));

instead

(or

item.unwrap_or(10)

for constants or cheap evaluations)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern doesn't seem to work, since the None pattern here requires an async evaluation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mhmm that is true. we should just work on getting the page handlers set up so we can call that instead.

}
}

Expand All @@ -142,7 +142,7 @@ pub async fn page_main(req: HttpRequest, deepwell: web::Data<DeepwellPool>) -> H
info!("GET / [{}]", host.unwrap_or("none"));

let page_req = PageRequest {
slug: "",
slug: "main",
categories: Vec::new(),
arguments: HashMap::new(),
};
Expand Down