Rewrite URL Dispatch

This commit is contained in:
spikecodes 2020-12-14 16:35:04 -08:00
parent 148d87fb45
commit 75bc170eba
7 changed files with 28 additions and 32 deletions

View File

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

View File

@ -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<String>, ends: (Option<String>, O
}
// SERVICES
#[get("/")]
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
render("popular".to_string(), params.sort.clone(), (params.before.clone(), params.after.clone())).await
}

View File

@ -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<HttpResponse> {
}
// SERVICES
#[get("/{id}")]
async fn short(web::Path(id): web::Path<String>) -> Result<HttpResponse> {
pub async fn short(web::Path(id): web::Path<String>) -> Result<HttpResponse> {
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<Params>) -> Result<HttpResponse> {
pub async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render(id, sort.to_string()).await,
None => render(id, "confidence".to_string()).await,

View File

@ -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<String>) -> Result<HttpResponse> {
pub async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
if cfg!(feature = "proxy") {
let media: String;

View File

@ -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<String>, params: web::Query<Params>) -> Result<HttpResponse> {
pub async fn page(web::Path(sub): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
render(sub, params.sort.clone(), (params.before.clone(), params.after.clone())).await
}

View File

@ -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<HttpResponse> {
}
// SERVICES
#[get("/u/{username}")]
async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render(username, sort.to_string()).await,
None => render(username, "hot".to_string()).await,

View File

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