feat(main/templates): Add barebones single-thread view
This commit is contained in:
parent
7dca9183c5
commit
6e56f8e729
4 changed files with 43 additions and 3 deletions
28
src/main.rs
28
src/main.rs
|
@ -30,7 +30,7 @@ use diesel::r2d2::{ConnectionManager, Pool};
|
||||||
use std::env;
|
use std::env;
|
||||||
use db::*;
|
use db::*;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use models::Thread;
|
use models::*;
|
||||||
|
|
||||||
/// Represents the state carried by the web server actors.
|
/// Represents the state carried by the web server actors.
|
||||||
struct AppState {
|
struct AppState {
|
||||||
|
@ -63,6 +63,31 @@ fn forum_index(req: HttpRequest<AppState>) -> FutureResponse<HttpResponse> {
|
||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn render_thread(tpl: &tera::Tera, thread: Thread, posts: Vec<Post>) -> HttpResponse {
|
||||||
|
let mut ctx = tera::Context::new();
|
||||||
|
ctx.add("thread", &thread);
|
||||||
|
ctx.add("posts", &posts);
|
||||||
|
|
||||||
|
let body = tpl.render("thread.html", &ctx).expect("Oh no");
|
||||||
|
HttpResponse::Ok()
|
||||||
|
.content_type("text/html")
|
||||||
|
.body(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn forum_thread(req: HttpRequest<AppState>) -> FutureResponse<HttpResponse> {
|
||||||
|
let thread_id = req.match_info().query("id").unwrap();
|
||||||
|
req.state().db.send(GetThread(thread_id))
|
||||||
|
.from_err()
|
||||||
|
.and_then(move |res| match res {
|
||||||
|
Ok((thread, posts)) => Ok(render_thread(&req.state().tera, thread, posts)),
|
||||||
|
Err(err) => {
|
||||||
|
error!("Error loading thread {}: {}", thread_id, err);
|
||||||
|
Ok(HttpResponse::InternalServerError().into())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.responder()
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
|
@ -86,6 +111,7 @@ fn main() {
|
||||||
App::with_state(AppState { db: db_addr.clone(), tera })
|
App::with_state(AppState { db: db_addr.clone(), tera })
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
.route("/", http::Method::GET, &forum_index)
|
.route("/", http::Method::GET, &forum_index)
|
||||||
|
.route("/thread/{id}", http::Method::GET, &forum_thread)
|
||||||
}).bind("127.0.0.1:4567").unwrap().start();
|
}).bind("127.0.0.1:4567").unwrap().start();
|
||||||
|
|
||||||
let _ = sys.run();
|
let _ = sys.run();
|
||||||
|
|
|
@ -8,7 +8,7 @@ pub struct Thread {
|
||||||
pub posted: DateTime<Utc>,
|
pub posted: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable)]
|
#[derive(Queryable, Serialize)]
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub thread: i32,
|
pub thread: i32,
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
<h1>Welcome to Converse</h1>
|
<h1>Welcome to Converse</h1>
|
||||||
<ul>
|
<ul>
|
||||||
{% for thread in threads -%}
|
{% for thread in threads -%}
|
||||||
<li>{{ thread.title }} (posted at {{ thread.posted }})</li>
|
<li><a href="/thread/{{ thread.id }}">{{ thread.title }}</a> (posted at {{ thread.posted }})</li>
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
|
|
14
templates/thread.html
Normal file
14
templates/thread.html
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Converse: {{ thread.title }}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>{{ thread.title }}</h1>
|
||||||
|
|
||||||
|
<p>{{ thread.body }}<br><i>Posted at {{ thread.posted }}</i></p>
|
||||||
|
{% for post in posts -%}
|
||||||
|
<li>{{ post.body }}<br><i>Posted at {{ post.posted }}</i></p>
|
||||||
|
{%- endfor %}
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Reference in a new issue