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 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 .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
23 changes: 22 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ deadpool = { version = "0.5", features = ["unmanaged"] }
deepwell-core = { path = "../deepwell/deepwell-core" }
deepwell-rpc = { path = "../deepwell-rpc" }
dns-lookup = "1"
ftml = { path = "../ftml" }
Copy link
Member

Choose a reason for hiding this comment

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

You'll need to set travis to download it in order for builds to pass.

ftml-rpc = { path = "../ftml-rpc" }
futures = "0.3"
lazy_static = "1"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern crate deadpool;
extern crate deepwell_core;
extern crate deepwell_rpc;
extern crate dns_lookup;
extern crate ftml;
extern crate ftml_rpc;

#[macro_use]
Expand Down
3 changes: 0 additions & 3 deletions src/route/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ mod prelude {
pub use ftml_rpc::Api as _;
}

#[macro_use]
mod macros;

mod auth;
mod misc;
mod page;
Expand Down
17 changes: 16 additions & 1 deletion src/route/api/macros.rs → src/route/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* route/api/macros.rs
* route/macros.rs
*
* thaumiel - Wikidot-like web server to provide pages, forums, and other services
* Copyright (C) 2019-2020 Ammon Smith
Expand All @@ -23,6 +23,7 @@ macro_rules! try_io {
match $result {
Ok(object) => object,
Err(error) => {
use deepwell_core::error::Error;
let error = Error::ServiceTransport(error).to_sendable();

return HttpResponse::BadGateway().json(error);
Expand All @@ -31,6 +32,20 @@ macro_rules! try_io {
};
}

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

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

Copy link
Member

Choose a reason for hiding this comment

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

Why is this necessary? (see below in its usage)

macro_rules! try_resp {
($result:expr) => {
match $result {
Expand Down
3 changes: 3 additions & 0 deletions src/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ mod prelude {
pub type HttpResult = StdResult<HttpResponse, ActixError>;
}

#[macro_use]
mod macros;

mod account;
mod api;
mod files;
Expand Down
112 changes: 102 additions & 10 deletions src/route/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,129 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use super::prelude::*;
use crate::{remote::DeepwellPool, route::prelude::*};
use deepwell_core::prelude::*;
use ftml::{
data::User as FtmlUser,
handle::{NullHandle, RemoteHandle},
prefilter,
prelude::*,
RemoteError, RemoteResult,
};
use std::collections::HashMap;
use wikidot_path::Request as PageRequest;

// Public route methods

/* TODO: Temporary Measure
*
* Until we can figure out a good way of divining the WikiId from the wiki request,
* just assume that these functions are retrieving the wiki at WikiId 1.
*/
lazy_static! {
static ref TEMP_WIKI_ID: WikiId = WikiId::from_raw(1);
}

/// Return an HttpResult for getting a page by its slug, using deepwell.
pub async fn get_deepwell_page(
wiki_id: WikiId,
slug: &str,
deepwell: &web::Data<DeepwellPool>,
) -> 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_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 Some(HttpResponse::InternalServerError().json(format!("{:?}", e))),
};

// TODO: something that uses Deepwell
let remote_handle = NullHandle;
let renderer = HtmlRender::new(&remote_handle);

// TODO: also get page metadata
let page_info = PageInfo {
title: "SCP-XXXX",
alt_title: Some("Template Page"),
header: None,
subheader: None,
rating: 1000,
tags: vec![
"scp",
],
};

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

// TODO: use jinja/other templater to put this text into a better template
let mut buffer = String::from("<html><head>");

for meta in &output.meta {
meta.render(&mut buffer).unwrap(); // TODO: let's not unwrap this
}

buffer.push_str("<style>");
buffer.push_str(&output.style);
buffer.push_str("</style></head><body>");
buffer.push_str(&output.html);
buffer.push_str("</body></html>\n");

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

Some(HttpResponse::InternalServerError().json(e))
}
}
}

pub async fn get_deepwell_page_wrapped(
wiki_id: WikiId,
slug: &str,
deepwell: web::Data<DeepwellPool>,
) -> HttpResponse {
match get_deepwell_page(wiki_id, slug, &deepwell).await {
Some(o) => o,
None => get_deepwell_page(wiki_id, "_404", &deepwell).await.unwrap(),
}
}

/// Route handling for pages, with arguments or not.
pub async fn page_get(req: HttpRequest) -> HttpResult {
pub async fn page_get(req: HttpRequest, deepwell: web::Data<DeepwellPool>) -> HttpResult {
let host = get_host(&req);
let path = req.uri().path();

info!("GET page {} [{}]", path, host.unwrap_or("none"));

let _page_req = PageRequest::parse(path);
let page_req = PageRequest::parse(path);

// TODO retrieve page from client
Ok(HttpResponse::NotImplemented().finish())
Ok(get_deepwell_page_wrapped(*TEMP_WIKI_ID, page_req.slug, deepwell).await)
}

/// Route for root, which is the same as whatever the `main` page is.
pub async fn page_main(req: HttpRequest) -> HttpResult {
pub async fn page_main(req: HttpRequest, deepwell: web::Data<DeepwellPool>) -> HttpResult {
let host = get_host(&req);

info!("GET / [{}]", host.unwrap_or("none"));

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

// TODO get page request
Ok(HttpResponse::NotImplemented().finish())
Ok(get_deepwell_page_wrapped(*TEMP_WIKI_ID, page_req.slug, deepwell).await)
Copy link
Member

Choose a reason for hiding this comment

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

Slug needs to changed from "", which is invalid. We need to fetch what the root page is based on wiki settings.

}