Compare commits
2 Commits
5b370b24dc
...
b1b85e6ec7
Author | SHA1 | Date |
---|---|---|
Emi Simpson | b1b85e6ec7 | |
Emi Simpson | 01ec3137a2 |
|
@ -8,11 +8,17 @@ 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::web::resource;
|
||||
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};
|
||||
|
||||
#[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> {
|
||||
|
@ -56,40 +62,54 @@ 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)?;
|
||||
|
||||
let (_, name, pronoun) = get_request_info(&settings, &req);
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(render_page(&pronoun, &settings, None)))
|
||||
.body(render_page(pronoun?, &settings, name.map(str::to_owned))))
|
||||
}
|
||||
|
||||
#[get("/{name}/{prefs}")]
|
||||
async fn prefs_and_name(
|
||||
web::Path((name, prefs)): web::Path<(String, String)>,
|
||||
#[cfg(feature = "ogp_images")]
|
||||
async fn handle_thumbnail_request(
|
||||
settings: web::Data<InstanceSettings>,
|
||||
req: HttpRequest,
|
||||
) -> Result<impl Responder> {
|
||||
let pronoun = settings
|
||||
.select_pronouns(Some(&name), Some(&prefs))
|
||||
.map_err(|_| Error::InvlaidPrefString)?;
|
||||
|
||||
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");
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(render_page(&pronoun, &settings, Some(name))))
|
||||
.content_type("image/png")
|
||||
.body(data))
|
||||
}
|
||||
|
||||
async fn not_found() -> impl Responder {
|
||||
|
@ -103,13 +123,21 @@ async fn main() -> std::io::Result<()> {
|
|||
env_logger::init();
|
||||
HttpServer::new(|| {
|
||||
let logger = Logger::default();
|
||||
App::new()
|
||||
let app = App::new()
|
||||
.data(InstanceSettings::default())
|
||||
.wrap(logger)
|
||||
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
|
||||
.service(prefs_and_name)
|
||||
.service(only_prefs)
|
||||
.service(default)
|
||||
.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))
|
||||
.service(create_link)
|
||||
.default_service(web::to(not_found))
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue