Reorder server endpoints

This commit is contained in:
Joscha 2024-03-30 12:58:24 +01:00
parent 57fc128be0
commit 0392e29538

View file

@ -26,12 +26,12 @@ struct Server {
pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()> { pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()> {
let app = Router::new() let app = Router::new()
.route("/text", post(post_text))
.route("/image", post(post_image).fallback(get_static_file))
.route("/photo", post(post_photo).fallback(get_static_file))
.route("/chat_message", post(post_chat_message))
.route("/calendar", post(post_calendar)) .route("/calendar", post(post_calendar))
.route("/cells", post(post_cells)) .route("/cells", post(post_cells))
.route("/chat_message", post(post_chat_message))
.route("/image", post(post_image).fallback(get_static_file))
.route("/photo", post(post_photo).fallback(get_static_file))
.route("/text", post(post_text))
.fallback(get(get_static_file)) .fallback(get(get_static_file))
.layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB .layer(DefaultBodyLimit::max(32 * 1024 * 1024)) // 32 MiB
.with_state(Server { tx }); .with_state(Server { tx });
@ -41,18 +41,64 @@ pub async fn run(tx: mpsc::Sender<Command>, addr: String) -> anyhow::Result<()>
Ok(()) Ok(())
} }
// /calendar
#[derive(Deserialize)] #[derive(Deserialize)]
struct PostTextForm { struct PostCalendarForm {
text: String, year: i32,
month: u8,
} }
async fn post_text(server: State<Server>, request: Form<PostTextForm>) { async fn post_calendar(server: State<Server>, request: Form<PostCalendarForm>) {
let _ = server let _ = server
.tx .tx
.send(Command::draw(TextDrawing(request.0.text))) .send(Command::draw(CalendarDrawing {
year: request.0.year,
month: request.0.month,
}))
.await; .await;
} }
// /cells
#[derive(Deserialize)]
struct PostCellsForm {
rule: u8,
rows: Option<u32>,
scale: Option<u32>,
}
async fn post_cells(server: State<Server>, request: Form<PostCellsForm>) {
let _ = server
.tx
.send(Command::draw(CellsDrawing {
rule: request.0.rule,
rows: request.0.rows.unwrap_or(32).min(512),
scale: request.0.scale.unwrap_or(4),
}))
.await;
}
// /chat_message
#[derive(Deserialize)]
struct PostChatMessageForm {
username: String,
content: String,
}
async fn post_chat_message(server: State<Server>, request: Form<PostChatMessageForm>) {
let _ = server
.tx
.send(Command::draw(ChatMessageDrawing {
username: request.0.username,
content: request.0.content,
}))
.await;
}
// /image
async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> { async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
let mut image = None; let mut image = None;
let mut bright = false; let mut bright = false;
@ -82,6 +128,8 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
Ok(Redirect::to("image").into_response()) Ok(Redirect::to("image").into_response())
} }
// /photo
async fn post_photo(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> { async fn post_photo(server: State<Server>, mut multipart: Multipart) -> somehow::Result<Response> {
let mut image = None; let mut image = None;
let mut title = None; let mut title = None;
@ -115,52 +163,16 @@ async fn post_photo(server: State<Server>, mut multipart: Multipart) -> somehow:
Ok(Redirect::to("photo").into_response()) Ok(Redirect::to("photo").into_response())
} }
#[derive(Deserialize)] // /text
struct PostChatMessageForm {
username: String,
content: String,
}
async fn post_chat_message(server: State<Server>, request: Form<PostChatMessageForm>) {
let _ = server
.tx
.send(Command::draw(ChatMessageDrawing {
username: request.0.username,
content: request.0.content,
}))
.await;
}
#[derive(Deserialize)] #[derive(Deserialize)]
struct PostCalendarForm { struct PostTextForm {
year: i32, text: String,
month: u8,
} }
async fn post_calendar(server: State<Server>, request: Form<PostCalendarForm>) { async fn post_text(server: State<Server>, request: Form<PostTextForm>) {
let _ = server let _ = server
.tx .tx
.send(Command::draw(CalendarDrawing { .send(Command::draw(TextDrawing(request.0.text)))
year: request.0.year,
month: request.0.month,
}))
.await;
}
#[derive(Deserialize)]
struct PostCellsForm {
rule: u8,
rows: Option<u32>,
scale: Option<u32>,
}
async fn post_cells(server: State<Server>, request: Form<PostCellsForm>) {
let _ = server
.tx
.send(Command::draw(CellsDrawing {
rule: request.0.rule,
rows: request.0.rows.unwrap_or(32).min(512),
scale: request.0.scale.unwrap_or(4),
}))
.await; .await;
} }