Switch to awc

This commit is contained in:
spikecodes 2021-01-14 09:53:54 -08:00
parent 842d97e9fa
commit dbe617d7eb
7 changed files with 61 additions and 91 deletions

57
Cargo.lock generated
View File

@ -31,7 +31,7 @@ dependencies = [
"futures-util", "futures-util",
"http", "http",
"log", "log",
"rustls 0.18.1", "rustls",
"tokio-rustls", "tokio-rustls",
"trust-dns-proto", "trust-dns-proto",
"trust-dns-resolver", "trust-dns-resolver",
@ -193,10 +193,10 @@ dependencies = [
"actix-service", "actix-service",
"actix-utils", "actix-utils",
"futures-util", "futures-util",
"rustls 0.18.1", "rustls",
"tokio-rustls", "tokio-rustls",
"webpki", "webpki",
"webpki-roots 0.20.0", "webpki-roots",
] ]
[[package]] [[package]]
@ -249,7 +249,7 @@ dependencies = [
"mime", "mime",
"pin-project 1.0.4", "pin-project 1.0.4",
"regex", "regex",
"rustls 0.18.1", "rustls",
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
@ -393,7 +393,7 @@ dependencies = [
"mime", "mime",
"percent-encoding", "percent-encoding",
"rand", "rand",
"rustls 0.18.1", "rustls",
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
@ -529,12 +529,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chunked_transfer"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7477065d45a8fe57167bf3cf8bcd3729b54cfcb81cca49bda2d038ea89ae82ca"
[[package]] [[package]]
name = "const_fn" name = "const_fn"
version = "0.4.5" version = "0.4.5"
@ -992,7 +986,6 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"time", "time",
"ureq",
"url", "url",
] ]
@ -1420,19 +1413,6 @@ dependencies = [
"webpki", "webpki",
] ]
[[package]]
name = "rustls"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "064fd21ff87c6e87ed4506e68beb42459caa4a0e2eb144932e6776768556980b"
dependencies = [
"base64 0.13.0",
"log",
"ring",
"sct",
"webpki",
]
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.5" version = "1.0.5"
@ -1769,7 +1749,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a" checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"rustls 0.18.1", "rustls",
"tokio", "tokio",
"webpki", "webpki",
] ]
@ -1910,22 +1890,6 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "ureq"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96014ded8c85822677daee4f909d18acccca744810fd4f8ffc492c284f2324bc"
dependencies = [
"base64 0.13.0",
"chunked_transfer",
"log",
"once_cell",
"rustls 0.19.0",
"url",
"webpki",
"webpki-roots 0.21.0",
]
[[package]] [[package]]
name = "url" name = "url"
version = "2.2.0" version = "2.2.0"
@ -2033,15 +1997,6 @@ dependencies = [
"webpki", "webpki",
] ]
[[package]]
name = "webpki-roots"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82015b7e0b8bad8185994674a13a93306bea76cf5a16c5a181382fd3a5ec2376"
dependencies = [
"webpki",
]
[[package]] [[package]]
name = "widestring" name = "widestring"
version = "0.4.3" version = "0.4.3"

View File

@ -10,7 +10,6 @@ edition = "2018"
[dependencies] [dependencies]
base64 = "0.13.0" base64 = "0.13.0"
actix-web = { version = "3.3.2", features = ["rustls"] } actix-web = { version = "3.3.2", features = ["rustls"] }
ureq = "2.0.1"
askama = "0.10.5" askama = "0.10.5"
serde = { version = "1.0.118", default_features = false, features = ["derive"] } serde = { version = "1.0.118", default_features = false, features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -50,7 +50,7 @@ async fn main() -> std::io::Result<()> {
// Append trailing slash and remove double slashes // Append trailing slash and remove double slashes
.wrap(middleware::NormalizePath::default()) .wrap(middleware::NormalizePath::default())
// Default service in case no routes match // Default service in case no routes match
.default_service(web::get().to(|| utils::error("Nothing here"))) .default_service(web::get().to(|| utils::error("Nothing here".to_string())))
// Read static files // Read static files
.route("/style.css/", web::get().to(style)) .route("/style.css/", web::get().to(style))
.route("/favicon.ico/", web::get().to(favicon)) .route("/favicon.ico/", web::get().to(favicon))
@ -95,10 +95,10 @@ async fn main() -> std::io::Result<()> {
.route("/{page}/", web::get().to(subreddit::wiki)), .route("/{page}/", web::get().to(subreddit::wiki)),
) )
// Search all of Reddit // Search all of Reddit
.route("/search/", web::get().to(search::find)), .route("/search/", web::get().to(search::find))
)
// Short link for post // Short link for post
.route("/{id:.{5,6}}/", web::get().to(post::item)) .route("/{id:.{5,6}}/", web::get().to(post::item)),
)
}) })
.bind(&address) .bind(&address)
.unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e)) .unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e))

View File

@ -84,7 +84,7 @@ pub async fn wiki(req: HttpRequest) -> HttpResponse {
} }
// SUBREDDIT // SUBREDDIT
async fn subreddit(sub: &str) -> Result<Subreddit, &'static str> { async fn subreddit(sub: &str) -> Result<Subreddit, String> {
// Build the Reddit JSON API url // Build the Reddit JSON API url
let path: String = format!("/r/{}/about.json?raw_json=1", sub); let path: String = format!("/r/{}/about.json?raw_json=1", sub);

View File

@ -47,7 +47,7 @@ pub async fn profile(req: HttpRequest) -> HttpResponse {
} }
// USER // USER
async fn user(name: &str) -> Result<User, &'static str> { async fn user(name: &str) -> Result<User, String> {
// 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);

View File

@ -101,7 +101,7 @@ pub struct Params {
#[derive(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 msg: String,
pub prefs: Preferences, pub prefs: Preferences,
} }
@ -169,7 +169,7 @@ pub fn format_num(num: i64) -> String {
} }
} }
pub async fn media(data: &serde_json::Value) -> (String, String) { pub async fn media(data: &Value) -> (String, String) {
let post_type: &str; let post_type: &str;
let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() { let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() {
post_type = "video"; post_type = "video";
@ -249,12 +249,12 @@ pub fn time(unix_time: i64) -> String {
// //
// val() function used to parse JSON from Reddit APIs // val() function used to parse JSON from Reddit APIs
pub fn val(j: &serde_json::Value, k: &str) -> String { pub fn val(j: &Value, k: &str) -> String {
String::from(j["data"][k].as_str().unwrap_or_default()) String::from(j["data"][k].as_str().unwrap_or_default())
} }
// Fetch posts of a user or subreddit and return a vector of posts and the "after" value // Fetch posts of a user or subreddit and return a vector of posts and the "after" value
pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> { pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post>, String), String> {
let res; let res;
let post_list; let post_list;
@ -271,7 +271,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
// Fetch the list of posts from the JSON response // Fetch the list of posts from the JSON response
match res["data"]["children"].as_array() { match res["data"]["children"].as_array() {
Some(list) => post_list = list, Some(list) => post_list = list,
None => return Err("No posts found"), None => return Err("No posts found".to_string()),
} }
let mut posts: Vec<Post> = Vec::new(); let mut posts: Vec<Post> = Vec::new();
@ -336,9 +336,9 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
// NETWORKING // NETWORKING
// //
pub async fn error(msg: &str) -> HttpResponse { pub async fn error(msg: String) -> HttpResponse {
let body = ErrorTemplate { let body = ErrorTemplate {
message: msg.to_string(), msg,
prefs: Preferences::default(), prefs: Preferences::default(),
} }
.render() .render()
@ -347,34 +347,50 @@ pub async fn error(msg: &str) -> HttpResponse {
} }
// 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: &str) -> Result<serde_json::Value, &'static str> { pub async fn request(path: &str) -> Result<Value, String> {
let url = format!("https://www.reddit.com{}", path); let url = format!("https://www.reddit.com{}", path);
// Send request using ureq // Send request using awc
match ureq::get(&url).call() { async fn send(url: &str) -> Result<String, (bool, String)> {
// If response is success let client = actix_web::client::Client::default();
Ok(response) => { let response = client.get(url).send().await;
// Parse the response from Reddit as JSON
match from_str(&response.into_string().unwrap()) { match response {
Ok(mut payload) => {
// Get first number of response HTTP status code
match payload.status().to_string().chars().next() {
// If success
Some('2') => Ok(String::from_utf8(payload.body().limit(20_000_000).await.unwrap().to_vec()).unwrap()),
// If redirection
Some('3') => Err((true, payload.headers().get("location").unwrap().to_str().unwrap().to_string())),
// Otherwise
_ => Err((false, "Page not found".to_string())),
}
}
Err(_) => Err((false, "Couldn't send request to Reddit".to_string())),
}
}
fn err(u: String, m: String) -> Result<Value, String> {
#[cfg(debug_assertions)]
dbg!(format!("{} - {}", u, m));
Err(m)
};
fn json(url: String, body: String) -> Result<Value, String> {
match from_str(body.as_str()) {
Ok(json) => Ok(json), Ok(json) => Ok(json),
Err(_) => { Err(_) => err(url, "Failed to parse page JSON data".to_string()),
#[cfg(debug_assertions)]
dbg!(format!("{} - Failed to parse page JSON data", url));
Err("Failed to parse page JSON data")
} }
} }
}
// If response is error match send(&url).await {
Err(ureq::Error::Status(_, _)) => { Ok(body) => json(url, body),
#[cfg(debug_assertions)] Err((true, location)) => match send(location.as_str()).await {
dbg!(format!("{} - Page not found", url)); Ok(body) => json(url, body),
Err("Page not found") Err((true, location)) => err(url, location),
} Err((_, msg)) => err(url, msg),
// If failed to send request },
Err(e) => { Err((_, msg)) => err(url, msg),
#[cfg(debug_assertions)]
dbg!(e);
Err("Couldn't send request to Reddit")
}
} }
} }

View File

@ -1,6 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block title %}Error: {{ message }}{% endblock %} {% block title %}Error: {{ msg }}{% endblock %}
{% block sortstyle %}{% endblock %} {% block sortstyle %}{% endblock %}
{% block content %} {% block content %}
<h1 style="text-align: center; font-size: 50px;">{{ message }}</h1> <h1 style="text-align: center; font-size: 50px;">{{ msg }}</h1>
{% endblock %} {% endblock %}