Add maud for frontend and move api to sub module

This commit is contained in:
2023-06-01 00:03:00 -04:00
parent 9059894021
commit 1698ef9c60
10 changed files with 58 additions and 10 deletions

4
src/handlers/api/mod.rs Normal file
View File

@@ -0,0 +1,4 @@
pub mod feed;
pub mod feeds;
pub mod entry;
pub mod entries;

19
src/handlers/home.rs Normal file
View File

@@ -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<PgPool>) -> Result<Markup> {
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) }
}
}
})
}

View File

@@ -1,4 +1,2 @@
pub mod feed;
pub mod feeds;
pub mod entry;
pub mod entries;
pub mod api;
pub mod home;

View File

@@ -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()));