From 1698ef9c60cdcc35e8b9d4911c96b19681ee1c74 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Thu, 1 Jun 2023 00:03:00 -0400 Subject: [PATCH] Add maud for frontend and move api to sub module --- Cargo.lock | 25 +++++++++++++++++++++++++ Cargo.toml | 1 + src/handlers/{ => api}/entries.rs | 0 src/handlers/{ => api}/entry.rs | 0 src/handlers/{ => api}/feed.rs | 0 src/handlers/{ => api}/feeds.rs | 0 src/handlers/api/mod.rs | 4 ++++ src/handlers/home.rs | 19 +++++++++++++++++++ src/handlers/mod.rs | 6 ++---- src/main.rs | 13 +++++++------ 10 files changed, 58 insertions(+), 10 deletions(-) rename src/handlers/{ => api}/entries.rs (100%) rename src/handlers/{ => api}/entry.rs (100%) rename src/handlers/{ => api}/feed.rs (100%) rename src/handlers/{ => api}/feeds.rs (100%) create mode 100644 src/handlers/api/mod.rs create mode 100644 src/handlers/home.rs diff --git a/Cargo.lock b/Cargo.lock index 053fd4c..b5f0de3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,6 +258,7 @@ dependencies = [ "chrono", "dotenvy", "feed-rs", + "maud", "reqwest", "serde", "serde_with", @@ -950,6 +951,30 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40" +[[package]] +name = "maud" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0bab19cef8a7fe1c18a43e881793bfc9d4ea984befec3ae5bd0415abf3ecf00" +dependencies = [ + "axum-core", + "http", + "itoa", + "maud_macros", +] + +[[package]] +name = "maud_macros" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0be95d66c3024ffce639216058e5bae17a83ecaf266ffc6e4d060ad447c9eed2" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "md-5" version = "0.10.5" diff --git a/Cargo.toml b/Cargo.toml index b997e24..d299e23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ axum = "0.6" chrono = { version = "0.4", features = ["serde"] } dotenvy = "0.15" feed-rs = "1.3" +maud = { version = "0.25", features = ["axum"] } reqwest = { version = "0.11", features = ["json"] } serde = { version = "1", features = ["derive"] } serde_with = "3" diff --git a/src/handlers/entries.rs b/src/handlers/api/entries.rs similarity index 100% rename from src/handlers/entries.rs rename to src/handlers/api/entries.rs diff --git a/src/handlers/entry.rs b/src/handlers/api/entry.rs similarity index 100% rename from src/handlers/entry.rs rename to src/handlers/api/entry.rs diff --git a/src/handlers/feed.rs b/src/handlers/api/feed.rs similarity index 100% rename from src/handlers/feed.rs rename to src/handlers/api/feed.rs diff --git a/src/handlers/feeds.rs b/src/handlers/api/feeds.rs similarity index 100% rename from src/handlers/feeds.rs rename to src/handlers/api/feeds.rs diff --git a/src/handlers/api/mod.rs b/src/handlers/api/mod.rs new file mode 100644 index 0000000..bbbde54 --- /dev/null +++ b/src/handlers/api/mod.rs @@ -0,0 +1,4 @@ +pub mod feed; +pub mod feeds; +pub mod entry; +pub mod entries; diff --git a/src/handlers/home.rs b/src/handlers/home.rs new file mode 100644 index 0000000..15f13f1 --- /dev/null +++ b/src/handlers/home.rs @@ -0,0 +1,19 @@ +use axum::extract::State; +use maud::{html, Markup}; +use sqlx::PgPool; + +use crate::error::Result; +use crate::models::entry::get_entries; + +pub async fn get(State(pool): State) -> Result { + let entries = get_entries(&pool).await?; + Ok(html! { + h1 { "crawlnicle" } + ul { + @for entry in entries { + @let title = entry.title.unwrap_or_else(|| "Untitled".to_string()); + li { (title) } + } + } + }) +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index bbbde54..fd63afd 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,4 +1,2 @@ -pub mod feed; -pub mod feeds; -pub mod entry; -pub mod entries; +pub mod api; +pub mod home; diff --git a/src/main.rs b/src/main.rs index 48d39bc..5f6c572 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,12 +25,13 @@ async fn main() -> anyhow::Result<()> { sqlx::migrate!().run(&pool).await?; let app = Router::new() - .route("/v1/feeds", get(handlers::feeds::get)) - .route("/v1/feed", post(handlers::feed::post)) - .route("/v1/feed/:id", get(handlers::feed::get)) - .route("/v1/entries", get(handlers::entries::get)) - .route("/v1/entry", post(handlers::entry::post)) - .route("/v1/entry/:id", get(handlers::entry::get)) + .route("/api/v1/feeds", get(handlers::api::feeds::get)) + .route("/api/v1/feed", post(handlers::api::feed::post)) + .route("/api/v1/feed/:id", get(handlers::api::feed::get)) + .route("/api/v1/entries", get(handlers::api::entries::get)) + .route("/api/v1/entry", post(handlers::api::entry::post)) + .route("/api/v1/entry/:id", get(handlers::api::entry::get)) + .route("/", get(handlers::home::get)) .with_state(pool) .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()));