188 lines
5.8 KiB
Rust
188 lines
5.8 KiB
Rust
pub mod ogp_images;
|
|
pub mod contrast;
|
|
|
|
use std::collections::HashMap;
|
|
use std::fmt::{self, Display};
|
|
|
|
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 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> {
|
|
name: Option<String>,
|
|
pronoun: &'a Pronoun,
|
|
pronouns: Vec<(usize, &'a Pronoun)>,
|
|
}
|
|
|
|
fn render_page(pronoun: &Pronoun, settings: &InstanceSettings, name: Option<String>) -> String {
|
|
IndexTemplate {
|
|
name,
|
|
pronoun,
|
|
pronouns: settings.pronoun_list.iter().enumerate().collect(),
|
|
}
|
|
.render()
|
|
.unwrap()
|
|
}
|
|
|
|
#[post("/")]
|
|
async fn create_link(
|
|
settings: web::Data<InstanceSettings>,
|
|
form: web::Form<HashMap<String, String>>,
|
|
) -> Result<impl Responder> {
|
|
let mut weights = vec![0; settings.pronoun_list.len()];
|
|
for (k, v) in form.iter() {
|
|
if let Ok(i) = k.parse::<usize>() {
|
|
let w = v.parse::<u8>().map_err(|_| Error::InvlaidPrefString)?;
|
|
if i < weights.len() - 1 {
|
|
weights[i] = w;
|
|
}
|
|
}
|
|
}
|
|
let prefs = InstanceSettings::create_preferences(&weights);
|
|
let pref_string = prefs.as_prefstring();
|
|
let url = match form.get("name") {
|
|
Some(name) if !name.is_empty() => format!("/{}/{}", name, pref_string),
|
|
_ => format!("/{}", pref_string),
|
|
};
|
|
Ok(HttpResponse::SeeOther()
|
|
.header(header::LOCATION, url)
|
|
.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");
|
|
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);
|
|
|
|
Ok(HttpResponse::Ok()
|
|
.content_type("text/html; charset=utf-8")
|
|
.body(render_page(pronoun?, &settings, name.map(str::to_owned))))
|
|
}
|
|
|
|
#[cfg(feature = "ogp_images")]
|
|
async fn handle_thumbnail_request(
|
|
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");
|
|
|
|
Ok(HttpResponse::Ok()
|
|
.content_type("image/png")
|
|
.body(data))
|
|
}
|
|
|
|
async fn not_found() -> impl Responder {
|
|
HttpResponse::NotFound()
|
|
.content_type("text/html; charset=utf-8")
|
|
.body(include_str!("../templates/404.html"))
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
env_logger::init();
|
|
HttpServer::new(|| {
|
|
let logger = Logger::default();
|
|
let app = 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))
|
|
.service(create_link)
|
|
.default_service(web::to(not_found))
|
|
})
|
|
.bind("127.0.0.1:8080")?
|
|
.run()
|
|
.await
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "error.html")]
|
|
struct ErrorPage {
|
|
msg: String,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum Error {
|
|
InvlaidPrefString,
|
|
}
|
|
|
|
impl Display for Error {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
let msg = match self {
|
|
&Error::InvlaidPrefString => "This URL contains an invalid pronoun preference string",
|
|
};
|
|
write!(f, "{}", msg)
|
|
}
|
|
}
|
|
|
|
impl ResponseError for Error {
|
|
fn status_code(&self) -> actix_web::http::StatusCode {
|
|
match self {
|
|
&Error::InvlaidPrefString => StatusCode::BAD_REQUEST,
|
|
}
|
|
}
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
HttpResponseBuilder::new(self.status_code())
|
|
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
|
.body(
|
|
ErrorPage {
|
|
msg: self.to_string(),
|
|
}
|
|
.render()
|
|
.unwrap(),
|
|
)
|
|
}
|
|
}
|