Compare commits

..

No commits in common. "b1b85e6ec721869e80346fa1f7e25b17ae3b9c93" and "5b370b24dcd6af20d843331e0c9c33080dfc5381" have entirely different histories.

1 changed files with 32 additions and 60 deletions

View File

@ -8,17 +8,11 @@ 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::web::resource;
use actix_web::{App, HttpRequest, HttpResponse, HttpServer, Responder, ResponseError, Result, post, web};
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, ResponseError, Result};
use askama::Template;
use pronouns_today::user_preferences::Preference;
use pronouns_today::{InstanceSettings, Pronoun};
#[cfg(feature = "ogp_images")]
use image::{DynamicImage, ImageOutputFormat};
#[cfg(feature = "ogp_images")]
use ogp_images::render_today;
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate<'a> {
@ -62,54 +56,40 @@ async fn create_link(
.finish())
}
/// 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");
#[get("/")]
async fn default(settings: web::Data<InstanceSettings>) -> Result<impl Responder> {
let pronoun = settings
.select_pronouns(name, prefs)
.map_err(|_| Error::InvlaidPrefString);
(prefs, name, pronoun)
}
async fn handle_basic_request(
settings: web::Data<InstanceSettings>,
req: HttpRequest,
) -> Result<impl Responder> {
let (_, name, pronoun) = get_request_info(&settings, &req);
.select_pronouns(None, None)
.map_err(|_| Error::InvlaidPrefString)?;
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(render_page(pronoun?, &settings, name.map(str::to_owned))))
.body(render_page(&pronoun, &settings, None)))
}
#[cfg(feature = "ogp_images")]
async fn handle_thumbnail_request(
#[get("/{prefs}")]
async fn only_prefs(
web::Path(prefs): web::Path<String>,
settings: web::Data<InstanceSettings>,
req: HttpRequest,
) -> Result<impl Responder> {
let (_, name, pronoun) = get_request_info(&settings, &req);
let mut data: Vec<u8> = Vec::with_capacity(15_000);
let image = DynamicImage::ImageRgb8(render_today(pronoun?, name.unwrap_or("")));
image.write_to(&mut data, ImageOutputFormat::Png)
.expect("Error encoding thumbnail to PNG");
let pronoun = settings
.select_pronouns(None, Some(&prefs))
.map_err(|_| Error::InvlaidPrefString)?;
Ok(HttpResponse::Ok()
.content_type("image/png")
.body(data))
.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)?;
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(render_page(&pronoun, &settings, Some(name))))
}
async fn not_found() -> impl Responder {
@ -123,21 +103,13 @@ async fn main() -> std::io::Result<()> {
env_logger::init();
HttpServer::new(|| {
let logger = Logger::default();
let app = App::new()
App::new()
.data(InstanceSettings::default())
.wrap(logger)
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim));
#[cfg(feature = "ogp_images")]
let app = app
.service(resource("/thumb.png") .to(handle_thumbnail_request))
.service(resource("/{prefs}/thumb.png") .to(handle_thumbnail_request))
.service(resource("/{name}/{prefs}/thumb.png").to(handle_thumbnail_request));
app
.service(resource("/") .to(handle_basic_request))
.service(resource("/{prefs}") .to(handle_basic_request))
.service(resource("/{name}/{prefs}").to(handle_basic_request))
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
.service(prefs_and_name)
.service(only_prefs)
.service(default)
.service(create_link)
.default_service(web::to(not_found))
})