From b7bcd9c5f14a2a2145e81b87bbd2e5a7de4afc0e Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Thu, 29 Aug 2024 23:22:55 -0700 Subject: [PATCH] more routers --- src/main.rs | 119 +++--------------------------------------- src/router/link.rs | 72 +++++++++++++++++++++++++ src/router/records.rs | 55 +++++++++++++++++++ 3 files changed, 135 insertions(+), 111 deletions(-) create mode 100644 src/router/link.rs create mode 100644 src/router/records.rs diff --git a/src/main.rs b/src/main.rs index 2289e4e..573ae17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use axum::{ middleware::{self, Next}, response::{Html, IntoResponse, Redirect}, routing::get, - Json, Router, + Router, }; use axum_extra::TypedHeader; use tower_http::{services::ServeDir, trace::TraceLayer}; @@ -15,6 +15,8 @@ use tower_http::{services::ServeDir, trace::TraceLayer}; use std::{io, net::SocketAddr}; mod router { + pub mod link; + pub mod records; pub mod upload; } mod cache; @@ -24,11 +26,12 @@ mod views; use util::{headers::ForwardedFor, logging, ssr, sweeper}; -use router::*; use state::*; use views::*; -use upload::get_upload_router; +use router::link::get_link_router; +use router::records::get_records_router; +use router::upload::get_upload_router; #[tokio::main] async fn main() -> io::Result<()> { @@ -41,23 +44,13 @@ async fn main() -> io::Result<()> { sweeper::spawn(state.clone()); - // Records views - let record_router = Router::new() - .route("/", get(records)) - .route("/links", get(records_links)); - - // Link pages - let link_router = Router::new() - .route("/:id", get(link).delete(link_delete)) - .route("/:id/remaining", get(remaining)); - // Router Setup let app = Router::new() .route("/", get(welcome)) .route("/download/:id", get(download)) .nest("/upload", get_upload_router()) - .nest("/records", record_router) - .nest("/link", link_router) + .nest("/records", get_records_router()) + .nest("/link", get_link_router()) .with_state(state) .fallback_service(ServeDir::new("dist")) .layer(TraceLayer::new_for_http()) @@ -68,24 +61,6 @@ async fn main() -> io::Result<()> { Ok(()) } -async fn remaining( - State(state): State, - axum::extract::Path(id): axum::extract::Path, -) -> impl IntoResponse { - let records = state.records.lock().await; - if let Some(record) = records.get(&id) { - let downloads_remaining = record.downloads_remaining(); - let plural = if downloads_remaining > 1 { "s" } else { "" }; - let out = format!( - "You have {} download{} remaining!", - downloads_remaining, plural - ); - Html(out) - } else { - Html("?".to_string()) - } -} - async fn welcome() -> impl IntoResponse { let cat_fact = views::get_cat_fact().await; Html(ssr::render(move || { @@ -93,84 +68,6 @@ async fn welcome() -> impl IntoResponse { })) } -async fn records(State(state): State) -> impl IntoResponse { - Json(state.records.lock().await.clone()) -} - -// This function is to remain ugly until that time in which I properly hide -// this behind some kind of authentication -async fn records_links(State(state): State) -> impl IntoResponse { - let records = state.records.lock().await.clone(); - Html(ssr::render(move || { - leptos::view! { - -
-
-
    - {records - .keys() - .map(|key| { - leptos::view! { - - } - }) - .collect::>()} -
-
-
-
- } - })) -} - -async fn link( - axum::extract::Path(id): axum::extract::Path, - State(mut state): State, -) -> Result, Redirect> { - { - let mut records = state.records.lock().await; - - if let Some(record) = records - .get_mut(&id) - .filter(|record| record.can_be_downloaded()) - { - return Ok(Html(ssr::render({ - let record = record.clone(); - || { - leptos::view! { } - } - }))); - } - } - - // TODO: This.... - state.remove_record(&id).await.unwrap(); - - Err(Redirect::to("/404.html")) -} - -async fn link_delete( - axum::extract::Path(id): axum::extract::Path, - State(mut state): State, -) -> Result, (StatusCode, String)> { - state - .remove_record(&id) - .await - .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?; - - Ok(Html("".to_string())) -} - async fn log_source( ConnectInfo(addr): ConnectInfo, forwarded_for: Option>, diff --git a/src/router/link.rs b/src/router/link.rs new file mode 100644 index 0000000..c76c449 --- /dev/null +++ b/src/router/link.rs @@ -0,0 +1,72 @@ +use axum::{ + extract::State, + response::{Html, IntoResponse, Redirect}, + routing::get, + Router, +}; +use reqwest::StatusCode; + +use crate::{util::ssr, AppState, AsyncRemoveRecord, DownloadLinkPage}; + +pub fn get_link_router() -> Router { + // Link pages + Router::new() + .route("/:id", get(link).delete(link_delete)) + .route("/:id/remaining", get(remaining)) +} + +async fn link( + axum::extract::Path(id): axum::extract::Path, + State(mut state): State, +) -> Result, Redirect> { + { + let mut records = state.records.lock().await; + + if let Some(record) = records + .get_mut(&id) + .filter(|record| record.can_be_downloaded()) + { + return Ok(Html(ssr::render({ + let record = record.clone(); + || { + leptos::view! { } + } + }))); + } + } + + // TODO: This.... + state.remove_record(&id).await.unwrap(); + + Err(Redirect::to("/404.html")) +} + +async fn link_delete( + axum::extract::Path(id): axum::extract::Path, + State(mut state): State, +) -> Result, (StatusCode, String)> { + state + .remove_record(&id) + .await + .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?; + + Ok(Html("".to_string())) +} + +async fn remaining( + State(state): State, + axum::extract::Path(id): axum::extract::Path, +) -> impl IntoResponse { + let records = state.records.lock().await; + if let Some(record) = records.get(&id) { + let downloads_remaining = record.downloads_remaining(); + let plural = if downloads_remaining > 1 { "s" } else { "" }; + let out = format!( + "You have {} download{} remaining!", + downloads_remaining, plural + ); + Html(out) + } else { + Html("?".to_string()) + } +} diff --git a/src/router/records.rs b/src/router/records.rs new file mode 100644 index 0000000..4d5e559 --- /dev/null +++ b/src/router/records.rs @@ -0,0 +1,55 @@ +use axum::{ + extract::State, + response::{Html, IntoResponse}, + routing::get, + Json, Router, +}; + +use crate::{util::ssr, AppState, HtmxPage}; + +pub fn get_records_router() -> Router { + // Records views + Router::new() + .route("/", get(records)) + .route("/links", get(records_links)) +} + +pub(crate) async fn records(State(state): State) -> impl IntoResponse { + Json(state.records.lock().await.clone()) +} + +// This function is to remain ugly until that time in which I properly hide +// this behind some kind of authentication +pub async fn records_links(State(state): State) -> impl IntoResponse { + let records = state.records.lock().await.clone(); + Html(ssr::render(move || { + leptos::view! { + +
+
+
    + {records + .keys() + .map(|key| { + leptos::view! { + + } + }) + .collect::>()} +
+
+
+
+ } + })) +}