From e9df91ff16a2362fd0bb1cb097cf908706c9a282 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 11 Jul 2023 14:09:09 -0400 Subject: [PATCH] move route handler functions to new routes module --- src/main.rs | 55 +++++++-------------------------------------------- src/routes.rs | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 48 deletions(-) create mode 100644 src/routes.rs diff --git a/src/main.rs b/src/main.rs index dc13ef2..af37ae5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,57 +2,16 @@ use std::env; use std::path::{Path, PathBuf}; use actix_files::Files; -use actix_web::web::Redirect; -use actix_web::{web, App, Either, HttpRequest, HttpResponse, HttpServer, Responder}; +use actix_web::{web, App, HttpServer}; use anyhow::Context; mod config; mod markdown; +mod routes; mod site; mod util; mod watcher; -fn not_found() -> HttpResponse { - HttpResponse::NotFound().body("not found") -} - -#[actix_web::route("/", method = "GET", method = "HEAD")] -async fn latest_posts(data: web::Data) -> impl Responder { - log::debug!("GET / -> latest_posts()"); - data.serve_latest_post() -} - -#[actix_web::route("/tag/{tag}", method = "GET", method = "HEAD")] -async fn latest_posts_by_tag(path: web::Path<(String,)>, data: web::Data) -> impl Responder { - let tag = path.into_inner().0; - log::debug!("GET /tag/{0} -> latest_posts_by_tag(), tag = {0}", tag); - data.serve_posts_by_tag(&tag) -} - -#[actix_web::route("/archive", method = "GET", method = "HEAD")] -async fn posts_archive(data: web::Data) -> impl Responder { - log::debug!("GET /archive -> posts_archive()"); - data.serve_posts_archive() -} - -#[actix_web::route("/rss", method = "GET", method = "HEAD")] -async fn rss_feed(data: web::Data) -> impl Responder { - log::debug!("GET /rss -> rss_feed()"); - data.serve_rss_feed() -} - -async fn site_content( - req: HttpRequest, - data: web::Data, -) -> Result, site::SiteError> { - log::debug!("GET {} -> fallback to site_content()", req.path()); - if let Some(response) = data.serve_content_by_url(&req)? { - Ok(response) - } else { - Ok(Either::Left(not_found())) - } -} - fn spawn_watcher( watch_paths: Vec, pages_config_path: PathBuf, @@ -180,12 +139,12 @@ async fn main() -> anyhow::Result<()> { App::new() // .app_data(data.clone()) .wrap(actix_web::middleware::NormalizePath::trim()) - .service(latest_posts) - .service(latest_posts_by_tag) - .service(posts_archive) - .service(rss_feed) + .service(routes::latest_posts) + .service(routes::latest_posts_by_tag) + .service(routes::posts_archive) + .service(routes::rss_feed) .service(Files::new("/", &server_config.static_files_path)) - .default_service(web::get().to(site_content)) + .default_service(web::get().to(routes::site_content)) }) .bind((server_config.bind_addr.clone(), server_config.bind_port)) .with_context(|| format!("Binding HTTP server on {}:{}", server_config.bind_addr, server_config.bind_port))? diff --git a/src/routes.rs b/src/routes.rs new file mode 100644 index 0000000..0dfa461 --- /dev/null +++ b/src/routes.rs @@ -0,0 +1,45 @@ +use actix_web::web::Redirect; +use actix_web::{web, Either, HttpRequest, HttpResponse, Responder}; + +use crate::site; + +fn not_found() -> HttpResponse { + HttpResponse::NotFound().body("not found") +} + +#[actix_web::route("/", method = "GET", method = "HEAD")] +pub async fn latest_posts(data: web::Data) -> impl Responder { + log::debug!("GET / -> latest_posts()"); + data.serve_latest_post() +} + +#[actix_web::route("/tag/{tag}", method = "GET", method = "HEAD")] +pub async fn latest_posts_by_tag(path: web::Path<(String,)>, data: web::Data) -> impl Responder { + let tag = path.into_inner().0; + log::debug!("GET /tag/{0} -> latest_posts_by_tag(), tag = {0}", tag); + data.serve_posts_by_tag(&tag) +} + +#[actix_web::route("/archive", method = "GET", method = "HEAD")] +pub async fn posts_archive(data: web::Data) -> impl Responder { + log::debug!("GET /archive -> posts_archive()"); + data.serve_posts_archive() +} + +#[actix_web::route("/rss", method = "GET", method = "HEAD")] +pub async fn rss_feed(data: web::Data) -> impl Responder { + log::debug!("GET /rss -> rss_feed()"); + data.serve_rss_feed() +} + +pub async fn site_content( + req: HttpRequest, + data: web::Data, +) -> Result, site::SiteError> { + log::debug!("GET {} -> fallback to site_content()", req.path()); + if let Some(response) = data.serve_content_by_url(&req)? { + Ok(response) + } else { + Ok(Either::Left(not_found())) + } +}