Error Page
This commit is contained in:
parent
bc1b29246d
commit
39ba50dada
@ -42,9 +42,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
// .default_service(web::get().to(subreddit::page))
|
|
||||||
// TRAILING SLASH MIDDLEWARE
|
// TRAILING SLASH MIDDLEWARE
|
||||||
.wrap(NormalizePath::default())
|
.wrap(NormalizePath::default())
|
||||||
|
// DEFAULT SERVICE
|
||||||
|
.default_service(web::get().to(utils::error))
|
||||||
// GENERAL SERVICES
|
// GENERAL SERVICES
|
||||||
.route("/style.css/", web::get().to(style))
|
.route("/style.css/", web::get().to(style))
|
||||||
.route("/favicon.ico/", web::get().to(|| HttpResponse::Ok()))
|
.route("/favicon.ico/", web::get().to(|| HttpResponse::Ok()))
|
||||||
@ -52,14 +53,16 @@ async fn main() -> std::io::Result<()> {
|
|||||||
// PROXY SERVICE
|
// PROXY SERVICE
|
||||||
.route("/proxy/{url:.*}/", web::get().to(proxy::handler))
|
.route("/proxy/{url:.*}/", web::get().to(proxy::handler))
|
||||||
// SEARCH SERVICES
|
// SEARCH SERVICES
|
||||||
.route("/search/", web::get().to(search::page))
|
.route("/search/", web::get().to(search::find))
|
||||||
.route("r/{sub}/search/", web::get().to(search::page))
|
.route("r/{sub}/search/", web::get().to(search::find))
|
||||||
// USER SERVICES
|
// USER SERVICES
|
||||||
.route("/u/{username}/", web::get().to(user::profile))
|
.route("/u/{username}/", web::get().to(user::profile))
|
||||||
.route("/user/{username}/", web::get().to(user::profile))
|
.route("/user/{username}/", web::get().to(user::profile))
|
||||||
// SUBREDDIT SERVICES
|
// SUBREDDIT SERVICES
|
||||||
.route("/r/{sub}/", web::get().to(subreddit::page))
|
.route("/r/{sub}/", web::get().to(subreddit::page))
|
||||||
.route("/r/{sub}/{sort}/", web::get().to(subreddit::page))
|
.route("/r/{sub}/{sort}/", web::get().to(subreddit::page))
|
||||||
|
// WIKI SERVICES
|
||||||
|
// .route("/r/{sub}/wiki/index", web::get().to(subreddit::wiki))
|
||||||
// POPULAR SERVICES
|
// POPULAR SERVICES
|
||||||
.route("/", web::get().to(subreddit::page))
|
.route("/", web::get().to(subreddit::page))
|
||||||
.route("/{sort:best|hot|new|top|rising}/", web::get().to(subreddit::page))
|
.route("/{sort:best|hot|new|top|rising}/", web::get().to(subreddit::page))
|
||||||
|
11
src/post.rs
11
src/post.rs
@ -1,6 +1,6 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use crate::utils::{format_num, format_url, param, request, val, Comment, ErrorTemplate, Flags, Flair, Post};
|
use crate::utils::{error, format_num, format_url, param, request, val, Comment, Flags, Flair, Post};
|
||||||
use actix_web::{http::StatusCode, HttpRequest, HttpResponse, Result};
|
use actix_web::{HttpRequest, HttpResponse, Result};
|
||||||
|
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
|
|
||||||
@ -30,12 +30,7 @@ pub async fn item(req: HttpRequest) -> Result<HttpResponse> {
|
|||||||
|
|
||||||
// If the Reddit API returns an error, exit and send error page to user
|
// If the Reddit API returns an error, exit and send error page to user
|
||||||
if req.is_err() {
|
if req.is_err() {
|
||||||
let s = ErrorTemplate {
|
error(req.err().unwrap().to_string()).await
|
||||||
message: req.err().unwrap().to_string(),
|
|
||||||
}
|
|
||||||
.render()
|
|
||||||
.unwrap();
|
|
||||||
return Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s));
|
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, grab the JSON output from the request
|
// Otherwise, grab the JSON output from the request
|
||||||
let res = req.unwrap();
|
let res = req.unwrap();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use crate::utils::{fetch_posts, param, ErrorTemplate, Post};
|
use crate::utils::{error, fetch_posts, param, Post};
|
||||||
use actix_web::{http::StatusCode, HttpRequest, HttpResponse, Result};
|
use actix_web::{HttpRequest, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
@ -16,7 +16,7 @@ struct SearchTemplate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SERVICES
|
// SERVICES
|
||||||
pub async fn page(req: HttpRequest) -> Result<HttpResponse> {
|
pub async fn find(req: HttpRequest) -> Result<HttpResponse> {
|
||||||
let path = format!("{}.json?{}", req.path(), req.query_string());
|
let path = format!("{}.json?{}", req.path(), req.query_string());
|
||||||
let q = param(&path, "q").await;
|
let q = param(&path, "q").await;
|
||||||
let sort = if param(&path, "sort").await.is_empty() {
|
let sort = if param(&path, "sort").await.is_empty() {
|
||||||
@ -29,12 +29,7 @@ pub async fn page(req: HttpRequest) -> Result<HttpResponse> {
|
|||||||
let posts = fetch_posts(path.clone(), String::new()).await;
|
let posts = fetch_posts(path.clone(), String::new()).await;
|
||||||
|
|
||||||
if posts.is_err() {
|
if posts.is_err() {
|
||||||
let s = ErrorTemplate {
|
error(posts.err().unwrap().to_string()).await
|
||||||
message: posts.err().unwrap().to_string(),
|
|
||||||
}
|
|
||||||
.render()
|
|
||||||
.unwrap();
|
|
||||||
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
|
||||||
} else {
|
} else {
|
||||||
let items = posts.unwrap();
|
let items = posts.unwrap();
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use crate::utils::{fetch_posts, format_num, format_url, param, request, val, ErrorTemplate, Post, Subreddit};
|
use crate::utils::{error, fetch_posts, format_num, format_url, param, request, val, Post, Subreddit};
|
||||||
use actix_web::{http::StatusCode, HttpRequest, HttpResponse, Result};
|
use actix_web::{HttpRequest, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
@ -29,12 +29,7 @@ pub async fn page(req: HttpRequest) -> Result<HttpResponse> {
|
|||||||
let posts = fetch_posts(path.clone(), String::new()).await;
|
let posts = fetch_posts(path.clone(), String::new()).await;
|
||||||
|
|
||||||
if posts.is_err() {
|
if posts.is_err() {
|
||||||
let s = ErrorTemplate {
|
error(posts.err().unwrap().to_string()).await
|
||||||
message: posts.err().unwrap().to_string(),
|
|
||||||
}
|
|
||||||
.render()
|
|
||||||
.unwrap();
|
|
||||||
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
|
||||||
} else {
|
} else {
|
||||||
let sub = sub_result.unwrap_or(Subreddit::default());
|
let sub = sub_result.unwrap_or(Subreddit::default());
|
||||||
let items = posts.unwrap();
|
let items = posts.unwrap();
|
||||||
|
17
src/user.rs
17
src/user.rs
@ -1,6 +1,6 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use crate::utils::{fetch_posts, format_url, nested_val, param, request, ErrorTemplate, Post, User};
|
use crate::utils::{error, fetch_posts, format_url, nested_val, param, request, Post, User};
|
||||||
use actix_web::{http::StatusCode, HttpRequest, HttpResponse, Result};
|
use actix_web::{HttpRequest, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use chrono::{TimeZone, Utc};
|
use chrono::{TimeZone, Utc};
|
||||||
|
|
||||||
@ -28,12 +28,7 @@ pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
|
|||||||
|
|
||||||
// If there is an error show error page
|
// If there is an error show error page
|
||||||
if user.is_err() || posts.is_err() {
|
if user.is_err() || posts.is_err() {
|
||||||
let s = ErrorTemplate {
|
error(user.err().unwrap().to_string()).await
|
||||||
message: user.err().unwrap().to_string(),
|
|
||||||
}
|
|
||||||
.render()
|
|
||||||
.unwrap();
|
|
||||||
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
|
||||||
} else {
|
} else {
|
||||||
let posts_unwrapped = posts.unwrap();
|
let posts_unwrapped = posts.unwrap();
|
||||||
|
|
||||||
@ -58,15 +53,15 @@ pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
|
|||||||
async fn user(name: &String) -> Result<User, &'static str> {
|
async fn user(name: &String) -> Result<User, &'static str> {
|
||||||
// Build the Reddit JSON API path
|
// Build the Reddit JSON API path
|
||||||
let path: String = format!("user/{}/about.json", name);
|
let path: String = format!("user/{}/about.json", name);
|
||||||
|
|
||||||
// Send a request to the url, receive JSON in response
|
// Send a request to the url, receive JSON in response
|
||||||
let req = request(path).await;
|
let req = request(path).await;
|
||||||
|
|
||||||
// If the Reddit API returns an error, exit this function
|
// If the Reddit API returns an error, exit this function
|
||||||
if req.is_err() {
|
if req.is_err() {
|
||||||
return Err(req.err().unwrap());
|
return Err(req.err().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, grab the JSON output from the request
|
// Otherwise, grab the JSON output from the request
|
||||||
let res = req.unwrap();
|
let res = req.unwrap();
|
||||||
|
|
||||||
|
10
src/utils.rs
10
src/utils.rs
@ -1,6 +1,8 @@
|
|||||||
//
|
//
|
||||||
// CRATES
|
// CRATES
|
||||||
//
|
//
|
||||||
|
use actix_web::{http::StatusCode, HttpResponse, Result};
|
||||||
|
use askama::Template;
|
||||||
use chrono::{TimeZone, Utc};
|
use chrono::{TimeZone, Utc};
|
||||||
use serde_json::{from_str, Value};
|
use serde_json::{from_str, Value};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
@ -81,7 +83,7 @@ pub struct Params {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Error template
|
// Error template
|
||||||
#[derive(askama::Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "error.html", escape = "none")]
|
#[template(path = "error.html", escape = "none")]
|
||||||
pub struct ErrorTemplate {
|
pub struct ErrorTemplate {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
@ -202,6 +204,12 @@ pub async fn fetch_posts(path: String, fallback_title: String) -> Result<(Vec<Po
|
|||||||
// NETWORKING
|
// NETWORKING
|
||||||
//
|
//
|
||||||
|
|
||||||
|
pub async fn error(message: String) -> Result<HttpResponse> {
|
||||||
|
let msg = if message.is_empty() { "Page not found".to_string() } else { message };
|
||||||
|
let body = ErrorTemplate { message: msg }.render().unwrap();
|
||||||
|
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(body))
|
||||||
|
}
|
||||||
|
|
||||||
// Make a request to a Reddit API and parse the JSON response
|
// Make a request to a Reddit API and parse the JSON response
|
||||||
pub async fn request(path: String) -> Result<serde_json::Value, &'static str> {
|
pub async fn request(path: String) -> Result<serde_json::Value, &'static str> {
|
||||||
let url = format!("https://www.reddit.com/{}", path);
|
let url = format!("https://www.reddit.com/{}", path);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user