Refactor request handling

This commit is contained in:
Emi Simpson 2021-10-25 14:13:39 -04:00
parent 5b370b24dc
commit 01ec3137a2
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

View file

@ -8,7 +8,7 @@ use actix_web::dev::HttpResponseBuilder;
use actix_web::http::{header, StatusCode};
use actix_web::middleware::normalize::TrailingSlash;
use actix_web::middleware::{self, Logger};
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, ResponseError, Result};
use actix_web::{App, HttpRequest, HttpResponse, HttpServer, Responder, ResponseError, Result, post, web};
use askama::Template;
use pronouns_today::user_preferences::Preference;
use pronouns_today::{InstanceSettings, Pronoun};
@ -56,40 +56,36 @@ async fn create_link(
.finish())
}
#[get("/")]
async fn default(settings: web::Data<InstanceSettings>) -> Result<impl Responder> {
/// Determine some basic information about a request
///
/// Determines the name and prefstring properties, if available, and also computes the pronoun that
/// should be responded using. This method is designed to facilitate creating the basic response
/// pages.
///
/// Both arguments should be passed directly from the caller, and the return values are, in order,
/// the prefstring, if available, the user's name, if available, and the computed pronouns.
fn get_request_info<'s, 'r>(
settings: &'s web::Data<InstanceSettings>,
req: &'r HttpRequest,
) -> (Option<&'r str>, Option<&'r str>, Result<&'s Pronoun, Error>) {
let prefs = req.match_info().get("prefs");
let name = req.match_info().get("name");
let pronoun = settings
.select_pronouns(None, None)
.map_err(|_| Error::InvlaidPrefString)?;
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(render_page(&pronoun, &settings, None)))
.select_pronouns(name, prefs)
.map_err(|_| Error::InvlaidPrefString);
(prefs, name, pronoun)
}
#[get("/{prefs}")]
async fn only_prefs(
web::Path(prefs): web::Path<String>,
async fn handle_basic_request(
settings: web::Data<InstanceSettings>,
req: HttpRequest,
) -> Result<impl Responder> {
let pronoun = settings
.select_pronouns(None, Some(&prefs))
.map_err(|_| Error::InvlaidPrefString)?;
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(render_page(&pronoun, &settings, None)))
}
#[get("/{name}/{prefs}")]
async fn prefs_and_name(
web::Path((name, prefs)): web::Path<(String, String)>,
settings: web::Data<InstanceSettings>,
) -> Result<impl Responder> {
let pronoun = settings
.select_pronouns(Some(&name), Some(&prefs))
.map_err(|_| Error::InvlaidPrefString)?;
let (_, name, pronoun) = get_request_info(&settings, &req);
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(render_page(&pronoun, &settings, Some(name))))
.body(render_page(pronoun?, &settings, name.map(str::to_owned))))
}
async fn not_found() -> impl Responder {
@ -107,9 +103,9 @@ async fn main() -> std::io::Result<()> {
.data(InstanceSettings::default())
.wrap(logger)
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
.service(prefs_and_name)
.service(only_prefs)
.service(default)
.service(resource("/") .to(handle_basic_request))
.service(resource("/{prefs}") .to(handle_basic_request))
.service(resource("/{name}/{prefs}").to(handle_basic_request))
.service(create_link)
.default_service(web::to(not_found))
})