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::http::{header, StatusCode};
use actix_web::middleware::normalize::TrailingSlash; use actix_web::middleware::normalize::TrailingSlash;
use actix_web::middleware::{self, Logger}; use actix_web::middleware::{self, Logger};
use actix_web::web::resource; 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 askama::Template;
use pronouns_today::user_preferences::Preference; use pronouns_today::user_preferences::Preference;
use pronouns_today::{InstanceSettings, Pronoun}; use pronouns_today::{InstanceSettings, Pronoun};
#[cfg(feature = "ogp_images")]
use image::{DynamicImage, ImageOutputFormat};
#[cfg(feature = "ogp_images")]
use ogp_images::render_today;
#[derive(Template)] #[derive(Template)]
#[template(path = "index.html")] #[template(path = "index.html")]
struct IndexTemplate<'a> { struct IndexTemplate<'a> {
@ -62,54 +56,40 @@ async fn create_link(
.finish()) .finish())
} }
/// Determine some basic information about a request #[get("/")]
/// async fn default(settings: web::Data<InstanceSettings>) -> Result<impl Responder> {
/// 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 let pronoun = settings
.select_pronouns(name, prefs) .select_pronouns(None, None)
.map_err(|_| Error::InvlaidPrefString); .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);
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8") .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")] #[get("/{prefs}")]
async fn handle_thumbnail_request( async fn only_prefs(
web::Path(prefs): web::Path<String>,
settings: web::Data<InstanceSettings>, settings: web::Data<InstanceSettings>,
req: HttpRequest,
) -> Result<impl Responder> { ) -> Result<impl Responder> {
let pronoun = settings
let (_, name, pronoun) = get_request_info(&settings, &req); .select_pronouns(None, Some(&prefs))
.map_err(|_| Error::InvlaidPrefString)?;
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");
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("image/png") .content_type("text/html; charset=utf-8")
.body(data)) .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 { async fn not_found() -> impl Responder {
@ -123,21 +103,13 @@ async fn main() -> std::io::Result<()> {
env_logger::init(); env_logger::init();
HttpServer::new(|| { HttpServer::new(|| {
let logger = Logger::default(); let logger = Logger::default();
let app = App::new() App::new()
.data(InstanceSettings::default()) .data(InstanceSettings::default())
.wrap(logger) .wrap(logger)
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim)); .wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
.service(prefs_and_name)
#[cfg(feature = "ogp_images")] .service(only_prefs)
let app = app .service(default)
.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))
.service(create_link) .service(create_link)
.default_service(web::to(not_found)) .default_service(web::to(not_found))
}) })