diff --git a/src/main.rs b/src/main.rs index daa8a23..2bc1fd7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ // Import Crates -use actix_web::{get, App, HttpResponse, HttpServer}; +use actix_web::{web, get, App, HttpResponse, HttpServer, middleware::NormalizePath}; // Reference local files mod popular; @@ -10,12 +10,10 @@ mod user; mod utils; // Create Services -#[get("/style.css")] async fn style() -> HttpResponse { HttpResponse::Ok().content_type("text/css").body(include_str!("../static/style.css")) } -#[get("/robots.txt")] async fn robots() -> HttpResponse { HttpResponse::Ok().body(include_str!("../static/robots.txt")) } @@ -44,21 +42,24 @@ async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() + // TRAILING SLASH MIDDLEWARE + .wrap(NormalizePath::default()) // GENERAL SERVICES - .service(style) - .service(favicon) - .service(robots) + .route("/style.css/", web::get().to(style)) + .route("/favicon.ico/", web::get().to(|| HttpResponse::Ok())) + .route("/robots.txt/", web::get().to(robots)) // PROXY SERVICE - .service(proxy::handler) - // POST SERVICES - .service(post::short) - .service(post::page) - // SUBREDDIT SERVICES - .service(subreddit::page) - // POPULAR SERVICES - .service(popular::page) + .route("/proxy/{url:.*}/", web::get().to(proxy::handler)) // USER SERVICES - .service(user::page) + .route("/u/{username}/", web::get().to(user::page)) + .route("/user/{username}/", web::get().to(user::page)) + // SUBREDDIT SERVICES + .route("/r/{sub}/", web::get().to(subreddit::page)) + // POPULAR SERVICES + .route("/", web::get().to(popular::page)) + // POST SERVICES + .route("/{id:.{5,6}}/", web::get().to(post::short)) + .route("/r/{sub}/comments/{id}/{title}/", web::get().to(post::page)) }) .bind(address.clone()) .expect(format!("Cannot bind to the address: {}", address).as_str()) diff --git a/src/popular.rs b/src/popular.rs index 7fcd24a..74d28af 100644 --- a/src/popular.rs +++ b/src/popular.rs @@ -1,6 +1,6 @@ // CRATES use crate::utils::{fetch_posts, ErrorTemplate, Params, Post}; -use actix_web::{get, http::StatusCode, web, HttpResponse, Result}; +use actix_web::{http::StatusCode, web, HttpResponse, Result}; use askama::Template; // STRUCTS @@ -50,7 +50,6 @@ async fn render(sub_name: String, sort: Option, ends: (Option, O } // SERVICES -#[get("/")] pub async fn page(params: web::Query) -> Result { render("popular".to_string(), params.sort.clone(), (params.before.clone(), params.after.clone())).await } diff --git a/src/post.rs b/src/post.rs index b726d45..a8fad24 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,6 +1,7 @@ // CRATES use crate::utils::{format_num, format_url, request, val, Comment, ErrorTemplate, Flair, Params, Post}; -use actix_web::{get, http::StatusCode, web, HttpResponse, Result}; +use actix_web::{http::StatusCode, web, HttpResponse, Result}; + use askama::Template; use chrono::{TimeZone, Utc}; use pulldown_cmark::{html, Options, Parser}; @@ -53,13 +54,11 @@ async fn render(id: String, sort: String) -> Result { } // SERVICES -#[get("/{id}")] -async fn short(web::Path(id): web::Path) -> Result { +pub async fn short(web::Path(id): web::Path) -> Result { render(id.to_string(), "confidence".to_string()).await } -#[get("/r/{sub}/comments/{id}/{title}/")] -async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Query) -> Result { +pub async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Query) -> Result { match ¶ms.sort { Some(sort) => render(id, sort.to_string()).await, None => render(id, "confidence".to_string()).await, diff --git a/src/proxy.rs b/src/proxy.rs index a2341d7..b2e1292 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -1,10 +1,9 @@ -use actix_web::{client::Client, get, web, Error, HttpResponse, Result}; +use actix_web::{client::Client, web, Error, HttpResponse, Result}; #[cfg(feature = "proxy")] use base64::decode; -#[get("/imageproxy/{url:.*}")] -async fn handler(web::Path(url): web::Path) -> Result { +pub async fn handler(web::Path(url): web::Path) -> Result { if cfg!(feature = "proxy") { let media: String; diff --git a/src/subreddit.rs b/src/subreddit.rs index 585f6ae..dfe212b 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -1,6 +1,6 @@ // CRATES use crate::utils::{fetch_posts, format_num, format_url, request, val, ErrorTemplate, Params, Post, Subreddit}; -use actix_web::{get, http::StatusCode, web, HttpResponse, Result}; +use actix_web::{http::StatusCode, web, HttpResponse, Result}; use askama::Template; use std::convert::TryInto; @@ -16,8 +16,7 @@ struct SubredditTemplate { // SERVICES #[allow(dead_code)] -#[get("/r/{sub}")] -async fn page(web::Path(sub): web::Path, params: web::Query) -> Result { +pub async fn page(web::Path(sub): web::Path, params: web::Query) -> Result { render(sub, params.sort.clone(), (params.before.clone(), params.after.clone())).await } diff --git a/src/user.rs b/src/user.rs index eb6d8c8..51df515 100644 --- a/src/user.rs +++ b/src/user.rs @@ -1,6 +1,6 @@ // CRATES use crate::utils::{fetch_posts, nested_val, request, ErrorTemplate, Params, Post, User}; -use actix_web::{get, http::StatusCode, web, HttpResponse, Result}; +use actix_web::{http::StatusCode, web, HttpResponse, Result}; use askama::Template; // STRUCTS @@ -39,8 +39,7 @@ async fn render(username: String, sort: String) -> Result { } // SERVICES -#[get("/u/{username}")] -async fn page(web::Path(username): web::Path, params: web::Query) -> Result { +pub async fn page(web::Path(username): web::Path, params: web::Query) -> Result { match ¶ms.sort { Some(sort) => render(username, sort.to_string()).await, None => render(username, "hot".to_string()).await, diff --git a/src/utils.rs b/src/utils.rs index c638d10..2b7edac 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -81,7 +81,7 @@ pub struct ErrorTemplate { pub async fn format_url(url: &str) -> String { #[cfg(feature = "proxy")] - return "/imageproxy/".to_string() + encode(url).as_str(); + return "/proxy/".to_string() + encode(url).as_str(); #[cfg(not(feature = "proxy"))] return url.to_string();