From 929b38e8ae4d61340cbd70352663af8f6f2418cf Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 5 Apr 2021 22:09:31 +0200 Subject: refactor(web/converse): Refactor first handlers to rouille This commit starts the refactoring process towards dropping actix (and tokio, ...). It builds, but at this commit, Converse does *not* work. I decided to commit to avoid more ridiculous diffs. Included changes: * Added dependency on rouille. * Refactored DbExecutor (formerly actix actor) to simply be a type with a few methods. Most actor messages still exist as they are being referred to by handlers. * Started refactoring two of the handlers (and their related renderer functions) into Rouille's call scheme. Important note: Rouille does not have safe session management out of the box, and it will need to be implemented as this progresses. Change-Id: I3e3f203e0705e561e1a3392e8f75dbe273d5fa81 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2861 Tested-by: BuildkiteCI Reviewed-by: tazjin --- web/converse/src/handlers.rs | 53 ++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 26 deletions(-) (limited to 'web/converse/src/handlers.rs') diff --git a/web/converse/src/handlers.rs b/web/converse/src/handlers.rs index c4448c748365..0759cec5c146 100644 --- a/web/converse/src/handlers.rs +++ b/web/converse/src/handlers.rs @@ -30,12 +30,14 @@ use actix_web::middleware::identity::RequestIdentity; use actix_web::middleware::{Started, Middleware}; use actix_web; use crate::db::*; -use crate::errors::ConverseError; +use crate::errors::{ConverseResult, ConverseError}; use futures::Future; use crate::models::*; use crate::oidc::*; use crate::render::*; +use rouille::{Request, Response}; + type ConverseResponse = Box>; const HTML: &'static str = "text/html"; @@ -54,15 +56,14 @@ pub struct AppState { pub renderer: Addr, } -pub fn forum_index(state: State) -> ConverseResponse { - state.db.send(ListThreads) - .flatten() - .and_then(move |res| state.renderer.send(IndexPage { - threads: res - }).from_err()) - .flatten() - .map(|res| HttpResponse::Ok().content_type(HTML).body(res)) - .responder() +/// Serve the forum's index page. +pub fn forum_index_rouille(db: &DbExecutor) -> ConverseResult { + let threads = db.list_threads()?; + Ok(Response::html(index_page(threads)?)) +} + +pub fn forum_index(_: State) -> ConverseResponse { + unimplemented!() } /// Returns the ID of the currently logged in user. If there is no ID @@ -78,23 +79,23 @@ pub fn get_user_id(req: &HttpRequest) -> i32 { } } -/// This handler retrieves and displays a single forum thread. -pub fn forum_thread(state: State, - req: HttpRequest, - thread_id: Path) -> ConverseResponse { - let id = thread_id.into_inner(); - let user = get_user_id(&req); +pub fn get_user_id_rouille(_req: &Request) -> i32 { + // TODO(tazjin): Implement session support in rouille somehow. + ANONYMOUS +} - state.db.send(GetThread(id)) - .flatten() - .and_then(move |res| state.renderer.send(ThreadPage { - current_user: user, - thread: res.0, - posts: res.1, - }).from_err()) - .flatten() - .map(|res| HttpResponse::Ok().content_type(HTML).body(res)) - .responder() +pub fn forum_thread_rouille(req: &Request, db: &DbExecutor, thread_id: i32) + -> ConverseResult { + let user = get_user_id_rouille(&req); + let thread = db.get_thread(thread_id)?; + Ok(Response::html(thread_page(user, thread.0, thread.1)?)) +} + +/// This handler retrieves and displays a single forum thread. +pub fn forum_thread(_: State, + _: HttpRequest, + _: Path) -> ConverseResponse { + unimplemented!() } /// This handler presents the user with the "New Thread" form. -- cgit 1.4.1