move trivial routes into separate module; refactor

This commit is contained in:
iliana etaoin 2023-04-06 14:04:24 -07:00
parent 65dd3252c9
commit 9242cbb09e
2 changed files with 83 additions and 74 deletions

View File

@ -3,12 +3,13 @@
#![warn(clippy::pedantic)] #![warn(clippy::pedantic)]
#![allow(clippy::needless_pass_by_value, clippy::no_effect_underscore_binding)] #![allow(clippy::needless_pass_by_value, clippy::no_effect_underscore_binding)]
mod trivial;
use askama::Template; use askama::Template;
use reqwest::{Client, StatusCode, Url}; use reqwest::{Client, StatusCode, Url};
use rocket::form::{Form, FromForm};
use rocket::http::{ContentType, Header, Status}; use rocket::http::{ContentType, Header, Status};
use rocket::response::{self, status::NoContent, Debug, Redirect, Responder}; use rocket::response::{self, Debug, Responder};
use rocket::{get, post, routes, uri, Request, Response, State}; use rocket::{get, routes, Request, Response, State};
use serde::Deserialize; use serde::Deserialize;
use std::io::Cursor; use std::io::Cursor;
use std::str::FromStr; use std::str::FromStr;
@ -26,14 +27,14 @@ fn rocket() -> _ {
rocket::build().manage(client).mount( rocket::build().manage(client).mount(
"/", "/",
routes![ routes![
code,
copy_js,
css,
favicon_ico,
index,
instance, instance,
instance_form, trivial::code,
robots_txt trivial::copy_js,
trivial::css,
trivial::favicon_ico,
trivial::index,
trivial::instance_form,
trivial::robots_txt,
], ],
) )
} }
@ -51,31 +52,6 @@ impl<T: Template> Responder<'_, 'static> for Html<T> {
} }
} }
#[derive(Template)]
#[template(path = "index.html")]
struct Index;
#[get("/")]
fn index() -> Html<Index> {
Html(Index)
}
#[derive(FromForm)]
struct InstanceForm<'a> {
instance: &'a str,
show_all: bool,
show_animated: bool,
}
#[post("/", data = "<form>")]
fn instance_form(form: Form<InstanceForm<'_>>) -> Redirect {
Redirect::to(uri!(instance(
form.instance,
form.show_all.then_some(true),
form.show_animated.then_some(true),
)))
}
#[get("/<instance>?<show_all>&<show_animated>")] #[get("/<instance>?<show_all>&<show_animated>")]
async fn instance( async fn instance(
client: &State<Client>, client: &State<Client>,
@ -147,42 +123,3 @@ async fn instance(
.await?; .await?;
Ok((ContentType::HTML, output)) Ok((ContentType::HTML, output))
} }
#[derive(Responder)]
struct Code {
zip: &'static [u8],
content_type: ContentType,
disposition: Header<'static>,
}
#[get("/code")]
fn code() -> Code {
Code {
zip: include_bytes!(concat!(env!("OUT_DIR"), "/source.zip")),
content_type: ContentType::ZIP,
disposition: Header::new(
"content-disposition",
r#"attachment; filename="emojos.in.zip""#,
),
}
}
#[get("/static/site.css")]
fn css() -> (ContentType, &'static str) {
(ContentType::CSS, include_str!("site.css"))
}
#[get("/static/copy.js")]
fn copy_js() -> (ContentType, &'static str) {
(ContentType::JavaScript, include_str!("copy.js"))
}
#[get("/favicon.ico")]
fn favicon_ico() -> NoContent {
NoContent
}
#[get("/robots.txt")]
fn robots_txt() -> NoContent {
NoContent
}

72
src/trivial.rs Normal file
View File

@ -0,0 +1,72 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::Html;
use askama::Template;
use rocket::form::{Form, FromForm};
use rocket::http::{Header, Status};
use rocket::response::content::{RawCss, RawJavaScript};
use rocket::response::{status::NoContent, Redirect, Responder};
use rocket::{get, post, uri};
#[derive(Template)]
#[template(path = "index.html")]
pub(crate) struct Index;
#[get("/")]
pub(crate) fn index() -> Html<Index> {
Html(Index)
}
#[derive(FromForm)]
pub(crate) struct InstanceForm<'a> {
instance: &'a str,
show_all: bool,
show_animated: bool,
}
#[post("/", data = "<form>")]
pub(crate) fn instance_form(form: Form<InstanceForm<'_>>) -> Redirect {
Redirect::to(uri!(crate::instance(
form.instance,
form.show_all.then_some(true),
form.show_animated.then_some(true),
)))
}
#[derive(Responder)]
#[response(content_type = "application/zip")]
pub(crate) struct Code {
zip: &'static [u8],
disposition: Header<'static>,
}
#[get("/code")]
pub(crate) fn code() -> Code {
Code {
zip: include_bytes!(concat!(env!("OUT_DIR"), "/source.zip")),
disposition: Header::new(
"content-disposition",
r#"attachment; filename="emojos.in.zip""#,
),
}
}
#[get("/static/site.css")]
pub(crate) fn css() -> RawCss<&'static [u8]> {
RawCss(include_bytes!("site.css"))
}
#[get("/static/copy.js")]
pub(crate) fn copy_js() -> RawJavaScript<&'static [u8]> {
RawJavaScript(include_bytes!("copy.js"))
}
#[get("/favicon.ico")]
pub(crate) fn favicon_ico() -> Status {
Status::NotFound
}
#[get("/robots.txt")]
pub(crate) fn robots_txt() -> NoContent {
NoContent
}