refactor(handlers): Use rendering actor for page renders
This currently breaks error handling in page render flows. To fix it properly, the database actor should return failable futures instead of `Result<T>` wrapped in a future.
This commit is contained in:
		
							parent
							
								
									fe7e5e48b5
								
							
						
					
					
						commit
						1cce37446b
					
				
					 2 changed files with 31 additions and 46 deletions
				
			
		| 
						 | 
					@ -5,7 +5,7 @@
 | 
				
			||||||
//! the tera templates stored in the `/templates` directory in the
 | 
					//! the tera templates stored in the `/templates` directory in the
 | 
				
			||||||
//! project root.
 | 
					//! project root.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use actix::prelude::{Addr, Syn};
 | 
					use actix::prelude::*;
 | 
				
			||||||
use actix_web;
 | 
					use actix_web;
 | 
				
			||||||
use actix_web::*;
 | 
					use actix_web::*;
 | 
				
			||||||
use actix_web::middleware::{Started, Middleware, RequestSession};
 | 
					use actix_web::middleware::{Started, Middleware, RequestSession};
 | 
				
			||||||
| 
						 | 
					@ -15,9 +15,12 @@ use futures::Future;
 | 
				
			||||||
use models::*;
 | 
					use models::*;
 | 
				
			||||||
use oidc::*;
 | 
					use oidc::*;
 | 
				
			||||||
use tera;
 | 
					use tera;
 | 
				
			||||||
 | 
					use render::*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ConverseResponse = Box<Future<Item=HttpResponse, Error=ConverseError>>;
 | 
					type ConverseResponse = Box<Future<Item=HttpResponse, Error=ConverseError>>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const HTML: &'static str = "text/html";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Represents the state carried by the web server actors.
 | 
					/// Represents the state carried by the web server actors.
 | 
				
			||||||
pub struct AppState {
 | 
					pub struct AppState {
 | 
				
			||||||
    /// Address of the database actor
 | 
					    /// Address of the database actor
 | 
				
			||||||
| 
						 | 
					@ -26,68 +29,47 @@ pub struct AppState {
 | 
				
			||||||
    /// Address of the OIDC actor
 | 
					    /// Address of the OIDC actor
 | 
				
			||||||
    pub oidc: Addr<Syn, OidcExecutor>,
 | 
					    pub oidc: Addr<Syn, OidcExecutor>,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Compiled templates
 | 
					    /// Address of the rendering actor
 | 
				
			||||||
    pub tera: tera::Tera,
 | 
					    pub renderer: Addr<Syn, Renderer>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// This function renders an overview of threads into the default
 | 
					// impl AppState {
 | 
				
			||||||
/// thread list template.
 | 
					//     fn render_ok<M>(self, msg: M) -> ConverseResponse
 | 
				
			||||||
fn render_threads(tpl: &tera::Tera, threads: Vec<Thread>) -> Result<HttpResponse> {
 | 
					//         where M: Send + Message, Renderer: Handler<M> {
 | 
				
			||||||
    let mut ctx = tera::Context::new();
 | 
					//         self.renderer.send(msg);
 | 
				
			||||||
    ctx.add("threads", &threads);
 | 
					//         unimplemented!()
 | 
				
			||||||
    let body = tpl.render("index.html", &ctx)?;
 | 
					//     }
 | 
				
			||||||
    Ok(HttpResponse::Ok().content_type("text/html").body(body))
 | 
					// }
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn forum_index(state: State<AppState>) -> ConverseResponse {
 | 
					pub fn forum_index(state: State<AppState>) -> ConverseResponse {
 | 
				
			||||||
    state.db.send(ListThreads)
 | 
					    state.db.send(ListThreads)
 | 
				
			||||||
 | 
					        .and_then(move |res| state.renderer.send(IndexPage { threads: res.unwrap() }))
 | 
				
			||||||
        .from_err()
 | 
					        .from_err()
 | 
				
			||||||
        .and_then(move |res| match res {
 | 
					        .map(|res| HttpResponse::Ok().content_type(HTML).body(res.unwrap()))
 | 
				
			||||||
            Ok(threads) => Ok(render_threads(&state.tera, threads)?),
 | 
					 | 
				
			||||||
            Err(err) => {
 | 
					 | 
				
			||||||
                error!("Error loading threads: {}", err);
 | 
					 | 
				
			||||||
                Ok(HttpResponse::InternalServerError().into())
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        })
 | 
					 | 
				
			||||||
        .responder()
 | 
					        .responder()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// This function renders a single forum thread into the default
 | 
					 | 
				
			||||||
/// thread view.
 | 
					 | 
				
			||||||
fn render_thread(tpl: &tera::Tera, thread: Thread, posts: Vec<Post>)
 | 
					 | 
				
			||||||
                 -> Result<HttpResponse> {
 | 
					 | 
				
			||||||
    let mut ctx = tera::Context::new();
 | 
					 | 
				
			||||||
    ctx.add("thread", &thread);
 | 
					 | 
				
			||||||
    ctx.add("posts", &posts);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let body = tpl.render("thread.html", &ctx)?;
 | 
					 | 
				
			||||||
    Ok(HttpResponse::Ok()
 | 
					 | 
				
			||||||
        .content_type("text/html")
 | 
					 | 
				
			||||||
        .body(body))
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/// This handler retrieves and displays a single forum thread.
 | 
					/// This handler retrieves and displays a single forum thread.
 | 
				
			||||||
pub fn forum_thread(state: State<AppState>, thread_id: Path<i32>) -> ConverseResponse {
 | 
					pub fn forum_thread(state: State<AppState>, thread_id: Path<i32>) -> ConverseResponse {
 | 
				
			||||||
    let id = thread_id.into_inner();
 | 
					    let id = thread_id.into_inner();
 | 
				
			||||||
    state.db.send(GetThread(id))
 | 
					    state.db.send(GetThread(id))
 | 
				
			||||||
        .from_err()
 | 
					        .and_then(move |res| {
 | 
				
			||||||
        .and_then(move |res| match res {
 | 
					            let u = res.unwrap();
 | 
				
			||||||
            Ok((thread, posts)) => Ok(render_thread(&state.tera, thread, posts)?),
 | 
					            state.renderer.send(ThreadPage {
 | 
				
			||||||
            Err(err) => {
 | 
					                thread: u.0,
 | 
				
			||||||
                error!("Error loading thread {}: {}", id, err);
 | 
					                posts: u.1,
 | 
				
			||||||
                Ok(HttpResponse::InternalServerError().into())
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .from_err()
 | 
				
			||||||
 | 
					        .map(|res| HttpResponse::Ok().content_type(HTML).body(res.unwrap()))
 | 
				
			||||||
        .responder()
 | 
					        .responder()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// This handler presents the user with the "New Thread" form.
 | 
					/// This handler presents the user with the "New Thread" form.
 | 
				
			||||||
pub fn new_thread(state: State<AppState>) -> Result<HttpResponse> {
 | 
					pub fn new_thread(state: State<AppState>) -> ConverseResponse {
 | 
				
			||||||
    let ctx = tera::Context::new();
 | 
					    state.renderer.send(NewThreadPage).from_err()
 | 
				
			||||||
    let body = state.tera.render("new-thread.html", &ctx)?;
 | 
					        .map(|res| HttpResponse::Ok().content_type(HTML).body(res.unwrap()))
 | 
				
			||||||
    Ok(HttpResponse::Ok()
 | 
					        .responder()
 | 
				
			||||||
       .content_type("text/html")
 | 
					 | 
				
			||||||
       .body(body))
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Deserialize)]
 | 
					#[derive(Deserialize)]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										5
									
								
								todo.org
									
										
									
									
									
								
							
							
						
						
									
										5
									
								
								todo.org
									
										
									
									
									
								
							| 
						 | 
					@ -1,4 +1,7 @@
 | 
				
			||||||
 | 
					* DONE Pin *-versions in cargo.toml
 | 
				
			||||||
* TODO Configurable number of DB workers
 | 
					* TODO Configurable number of DB workers
 | 
				
			||||||
* TODO Pin *-versions in cargo.toml
 | 
					 | 
				
			||||||
* TODO Match certain types of Diesel errors (esp. for "not found")
 | 
					* TODO Match certain types of Diesel errors (esp. for "not found")
 | 
				
			||||||
* TODO Sketch out categories vs. tags system
 | 
					* TODO Sketch out categories vs. tags system
 | 
				
			||||||
 | 
					* TODO Markdown support
 | 
				
			||||||
 | 
					* TODO Quote button
 | 
				
			||||||
 | 
					* TODO Multiquote buttons
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue