Simplify routes in main.rs

This commit is contained in:
spikecodes 2021-02-24 09:26:01 -08:00
parent 3e567d9acf
commit 4a06882dc8
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
3 changed files with 50 additions and 63 deletions

47
Cargo.lock generated
View File

@ -786,9 +786,9 @@ checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
[[package]]
name = "futures"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da9052a1a50244d8d5aa9bf55cbc2fb6f357c86cc52e46c62ed390a7180cf150"
checksum = "7f55667319111d593ba876406af7c409c0ebb44dc4be6132a783ccf163ea14c1"
dependencies = [
"futures-channel",
"futures-core",
@ -801,9 +801,9 @@ dependencies = [
[[package]]
name = "futures-channel"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2d31b7ec7efab6eefc7c57233bb10b847986139d88cc2f5a02a1ae6871a1846"
checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939"
dependencies = [
"futures-core",
"futures-sink",
@ -811,15 +811,15 @@ dependencies = [
[[package]]
name = "futures-core"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79e5145dde8da7d1b3892dad07a9c98fc04bc39892b1ecc9692cf53e2b780a65"
checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94"
[[package]]
name = "futures-executor"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9e59fdc009a4b3096bf94f740a0f2424c082521f20a9b08c5c07c48d90fd9b9"
checksum = "891a4b7b96d84d5940084b2a37632dd65deeae662c114ceaa2c879629c9c0ad1"
dependencies = [
"futures-core",
"futures-task",
@ -828,9 +828,9 @@ dependencies = [
[[package]]
name = "futures-io"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28be053525281ad8259d47e4de5de657b25e7bac113458555bb4b70bc6870500"
checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59"
[[package]]
name = "futures-lite"
@ -849,9 +849,9 @@ dependencies = [
[[package]]
name = "futures-macro"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c287d25add322d9f9abdcdc5927ca398917996600182178774032e9f8258fedd"
checksum = "ea405816a5139fb39af82c2beb921d52143f556038378d6db21183a5c37fbfb7"
dependencies = [
"proc-macro-hack",
"proc-macro2",
@ -861,24 +861,21 @@ dependencies = [
[[package]]
name = "futures-sink"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caf5c69029bda2e743fddd0582d1083951d65cc9539aebf8812f36c3491342d6"
checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3"
[[package]]
name = "futures-task"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13de07eb8ea81ae445aca7b69f5f7bf15d7bf4912d8ca37d6645c77ae8a58d86"
dependencies = [
"once_cell",
]
checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80"
[[package]]
name = "futures-util"
version = "0.3.12"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "632a8cd0f2a4b3fdea1657f08bde063848c3bd00f9bbf6e256b8be78802e624b"
checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1"
dependencies = [
"futures-channel",
"futures-core",
@ -1282,9 +1279,9 @@ dependencies = [
[[package]]
name = "once_cell"
version = "1.6.0"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ad167a2f54e832b82dbe003a046280dceffe5227b5f79e08e363a29638cfddd"
checksum = "10acf907b94fc1b1a152d08ef97e7759650268cf986bf127f387e602b02c7e5a"
[[package]]
name = "opaque-debug"
@ -1951,9 +1948,9 @@ dependencies = [
[[package]]
name = "tracing"
version = "0.1.24"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f77d3842f76ca899ff2dbcf231c5c65813dea431301d6eb686279c15c4464f12"
checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f"
dependencies = [
"cfg-if 1.0.0",
"log",

View File

@ -52,19 +52,6 @@ impl<State: Clone + Send + Sync + 'static> Middleware<State> for NormalizePath {
}
// Create Services
async fn style(_req: Request<()>) -> tide::Result {
Ok(Response::builder(200).content_type("text/css").body(include_str!("../static/style.css")).build())
}
// Required for creating a PWA
async fn manifest(_req: Request<()>) -> tide::Result {
Ok(
Response::builder(200)
.content_type("application/json")
.body(include_str!("../static/manifest.json"))
.build(),
)
}
// Required for the manifest to be valid
async fn pwa_logo(_req: Request<()>) -> tide::Result {
@ -81,16 +68,6 @@ async fn iphone_logo(_req: Request<()>) -> tide::Result {
)
}
async fn robots(_req: Request<()>) -> tide::Result {
Ok(
Response::builder(200)
.content_type("text/plain")
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.body("User-agent: *\nAllow: /")
.build(),
)
}
async fn favicon(_req: Request<()>) -> tide::Result {
Ok(
Response::builder(200)
@ -101,6 +78,19 @@ async fn favicon(_req: Request<()>) -> tide::Result {
)
}
async fn resource(body: &str, content_type: &str, cache: bool) -> tide::Result {
let mut res = Response::new(200);
if cache {
res.insert_header("Cache-Control", "public, max-age=1209600, s-maxage=86400");
}
res.set_content_type(content_type);
res.set_body(body);
Ok(res)
}
#[async_std::main]
async fn main() -> tide::Result<()> {
let matches = App::new("Libreddit")
@ -140,8 +130,6 @@ async fn main() -> tide::Result<()> {
let listener = format!("{}:{}", address, port);
// Start HTTP server
println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), &listener);
let mut app = tide::new();
// Redirect to HTTPS if "--redirect-https" enabled
@ -163,10 +151,10 @@ async fn main() -> tide::Result<()> {
}));
// Read static files
app.at("/style.css/").get(style);
app.at("/style.css/").get(|_| resource(include_str!("../static/style.css"), "text/css", false));
app.at("/manifest.json/").get(|_| resource(include_str!("../static/manifest.json"), "application/json", false));
app.at("/robots.txt/").get(|_| resource("User-agent: *\nAllow: /", "text/plain", true));
app.at("/favicon.ico/").get(favicon);
app.at("/robots.txt/").get(robots);
app.at("/manifest.json/").get(manifest);
app.at("/logo.png/").get(pwa_logo);
app.at("/touch-icon-iphone.png/").get(iphone_logo);
app.at("/apple-touch-icon.png/").get(iphone_logo);
@ -209,23 +197,22 @@ async fn main() -> tide::Result<()> {
app.at("/settings/restore/").get(settings::restore);
// Subreddit services
// See posts and info about subreddit
app.at("/r/:sub/").get(subreddit::page);
// Handle subscribe/unsubscribe
app.at("/r/:sub/subscribe/").post(subreddit::subscriptions);
app.at("/r/:sub/unsubscribe/").post(subreddit::subscriptions);
// View post on subreddit
app.at("/r/:sub/comments/:id/").get(post::item);
app.at("/r/:sub/comments/:id/:title/").get(post::item);
app.at("/r/:sub/comments/:id/:title/:comment_id/").get(post::item);
// Search inside subreddit
app.at("/r/:sub/search/").get(search::find);
// View wiki of subreddit
app.at("/r/:sub/w/").get(subreddit::wiki);
app.at("/r/:sub/w/:page/").get(subreddit::wiki);
app.at("/r/:sub/wiki/").get(subreddit::wiki);
app.at("/r/:sub/wiki/:page/").get(subreddit::wiki);
// Sort subreddit posts
app.at("/r/:sub/w/").get(subreddit::wiki);
app.at("/r/:sub/w/:page/").get(subreddit::wiki);
app.at("/r/:sub/:sort/").get(subreddit::page);
// Front page
@ -249,6 +236,7 @@ async fn main() -> tide::Result<()> {
Ok("best") | Ok("hot") | Ok("new") | Ok("top") | Ok("rising") | Ok("controversial") => subreddit::page(req).await,
// Short link for post
Ok(id) if id.len() > 4 && id.len() < 7 => post::item(req).await,
// Error message for unknown pages
_ => error(req, "Nothing here".to_string()).await,
}
});
@ -256,6 +244,7 @@ async fn main() -> tide::Result<()> {
// Default service in case no routes match
app.at("*").get(|req| error(req, "Nothing here".to_string()));
app.listen(listener).await?;
Ok(())
app.listen(&listener).await?;
Ok(println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), listener))
}

View File

@ -160,6 +160,7 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author:
let score = data["score"].as_i64().unwrap_or(0);
let body = rewrite_urls(&val(&comment, "body_html"));
// If this comment contains replies, handle those too
let replies: Vec<Comment> = if data["replies"].is_object() {
parse_comments(&data["replies"], post_link, post_author, highlighted_comment).await
} else {