From f445c42f55807042091cb036ed5dab348130e3ba Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Sun, 10 Jan 2021 13:08:36 -0800 Subject: [PATCH] Wide UI Mode --- src/main.rs | 32 +++++++++++++------ src/post.rs | 5 +-- src/settings.rs | 5 +-- src/subreddit.rs | 2 ++ src/utils.rs | 2 ++ static/style.css | 40 +++++++++++++++--------- templates/base.html | 2 +- templates/post.html | 2 ++ templates/search.html | 2 +- templates/settings.html | 66 +++++++++++++++++++++------------------- templates/subreddit.html | 2 +- templates/user.html | 4 +-- templates/wiki.html | 2 ++ 13 files changed, 102 insertions(+), 64 deletions(-) diff --git a/src/main.rs b/src/main.rs index 64f6e98..030abff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ // Import Crates -use actix_web::{get, middleware::NormalizePath, web, App, HttpResponse, HttpServer}; +use actix_web::{App, HttpResponse, HttpServer, get, middleware, web}; // dev::Service // Reference local files mod post; @@ -28,14 +28,14 @@ async fn favicon() -> HttpResponse { #[actix_web::main] async fn main() -> std::io::Result<()> { - let args: Vec = std::env::args().collect(); let mut address = "0.0.0.0:8080".to_string(); + // let mut https = false; - if args.len() > 1 { - for arg in args { - if arg.starts_with("--address=") || arg.starts_with("-a=") { - address = arg.split('=').collect::>()[1].to_string(); - } + for arg in std::env::args().collect::>() { + match arg.split('=').collect::>()[0] { + "--address" | "-a" => address = arg.split('=').collect::>()[1].to_string(), + // "--redirect-https" | "-r" => https = true, + _ => {} } } @@ -44,8 +44,22 @@ async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() + // REDIRECT TO HTTPS + // .wrap(middleware::DefaultHeaders::new().header("Strict-Transport-Security", "max-age=31536000")) + // .wrap_fn(|req, srv| { + // let fut = srv.call(req); + // async { + // let mut res = fut.await?; + // if https { + // res.headers_mut().insert( + // actix_web::http::header::STRICT_TRANSPORT_SECURITY, actix_web::http::HeaderValue::from_static("max-age=31536000;"), + // ); + // } + // Ok(res) + // } + // }) // TRAILING SLASH MIDDLEWARE - .wrap(NormalizePath::default()) + .wrap( middleware::NormalizePath::default()) // DEFAULT SERVICE .default_service(web::get().to(|| utils::error("Nothing here".to_string()))) // GENERAL SERVICES @@ -80,7 +94,7 @@ async fn main() -> std::io::Result<()> { .route("/r/{sub}/comments/{id}/{title}/{comment_id}/", web::get().to(post::item)) }) .bind(&address) - .unwrap_or_else(|_| panic!("Cannot bind to the address: {}", address)) + .unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e)) .run() .await } diff --git a/src/post.rs b/src/post.rs index 92dbf88..4da36c6 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{cookie, error, format_num, format_url, media, param, request, rewrite_url, val, Comment, Flags, Flair, Post}; +use crate::utils::{Comment, Flags, Flair, Post, Preferences, cookie, error, format_num, format_url, media, param, prefs, request, rewrite_url, val}; use actix_web::{HttpRequest, HttpResponse}; use async_recursion::async_recursion; @@ -14,6 +14,7 @@ struct PostTemplate { comments: Vec, post: Post, sort: String, + prefs: Preferences } pub async fn item(req: HttpRequest) -> HttpResponse { @@ -45,7 +46,7 @@ pub async fn item(req: HttpRequest) -> HttpResponse { let comments = parse_comments(&res[1]).await; // Use the Post and Comment structs to generate a website to show users - let s = PostTemplate { comments, post, sort }.render().unwrap(); + let s = PostTemplate { comments, post, sort, prefs: prefs(req) }.render().unwrap(); HttpResponse::Ok().content_type("text/html").body(s) } // If the Reddit API returns an error, exit and send error page to user diff --git a/src/settings.rs b/src/settings.rs index 6678591..5ce566f 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -15,6 +15,7 @@ struct SettingsTemplate { pub struct SettingsForm { front_page: Option, layout: Option, + wide: Option, comment_sort: Option, hide_nsfw: Option, } @@ -31,8 +32,8 @@ pub async fn get(req: HttpRequest) -> HttpResponse { pub async fn set(req: HttpRequest, form: Form) -> HttpResponse { let mut res = HttpResponse::Found(); - let names = vec!["front_page", "layout", "comment_sort", "hide_nsfw"]; - let values = vec![&form.front_page, &form.layout, &form.comment_sort, &form.hide_nsfw]; + let names = vec!["front_page", "layout", "wide", "comment_sort", "hide_nsfw"]; + let values = vec![&form.front_page, &form.layout, &form.wide, &form.comment_sort, &form.hide_nsfw]; for (i, name) in names.iter().enumerate() { match values[i] { diff --git a/src/subreddit.rs b/src/subreddit.rs index 3e474a8..a092eeb 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -20,6 +20,7 @@ struct WikiTemplate { sub: String, wiki: String, page: String, + prefs: Preferences, } // SERVICES @@ -67,6 +68,7 @@ pub async fn wiki(req: HttpRequest) -> HttpResponse { sub: sub.to_string(), wiki: rewrite_url(res["data"]["content_html"].as_str().unwrap_or_default()), page: page.to_string(), + prefs: prefs(req), } .render() .unwrap(); diff --git a/src/utils.rs b/src/utils.rs index 0279852..1ddf171 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -96,6 +96,7 @@ pub struct ErrorTemplate { pub struct Preferences { pub front_page: String, pub layout: String, + pub wide: String, pub hide_nsfw: String, pub comment_sort: String, } @@ -109,6 +110,7 @@ pub fn prefs(req: HttpRequest) -> Preferences { Preferences { front_page: cookie(&req, "front_page"), layout: cookie(&req, "layout"), + wide: cookie(&req, "wide"), hide_nsfw: cookie(&req, "hide_nsfw"), comment_sort: cookie(&req, "comment_sort"), } diff --git a/static/style.css b/static/style.css index 42de255..43df3c7 100644 --- a/static/style.css +++ b/static/style.css @@ -2,6 +2,7 @@ :root { --accent: aqua; + --green: #5cff85; --text: white; --foreground: #222; --background: #0F0F0F; @@ -62,6 +63,15 @@ main { margin: 60px auto 20px auto } +.wide main { + max-width: calc(100% - 40px); +} + +.wide #column_one { + width: 100%; + max-width: 100%; +} + #column_one { max-width: 750px; border-radius: 5px; @@ -454,8 +464,8 @@ input[type="submit"]:hover { color: var(--accent); } } .stickied { - --accent: #5cff85; - border: 1px solid #5cff85; + --accent: var(--green); + border: 1px solid var(--green); } /* Comment */ @@ -567,60 +577,60 @@ input[type="submit"]:hover { color: var(--accent); } /* Layouts */ -#compact .post:not(.highlighted) { +.compact .post:not(.highlighted) { border-radius: 0; margin: 0; padding: 0; } -#compact .post:first-of-type { +.compact .post:first-of-type { border-radius: 5px 5px 0 0; overflow: hidden; } -#compact .post:last-of-type { +.compact .post:last-of-type { border-radius: 0 0 5px 5px; overflow: hidden; } -#compact .post.highlighted { +.compact .post.highlighted { border-radius: 5px; } -#compact .post:not(:last-of-type):not(.highlighted):not(.stickied) { +.compact .post:not(:last-of-type):not(.highlighted):not(.stickied) { border-bottom: 0; } -#compact .post_left { +.compact .post_left { border-radius: 0; } -#compact .post_header { +.compact .post_header { font-size: 14px; } -#compact .post_title { +.compact .post_title { margin-top: 5px; } -#compact .post_text { +.compact .post_text { padding: 10px; } -#compact .post_thumbnail { +.compact .post_thumbnail { max-width: 75px; max-height: 75px; } -#compact footer { +.compact footer { margin-top: 20px; } -#card .post_right { +.card .post_right { flex-direction: column; } -#card .post:not(.highlighted) .post_media { +.card .post:not(.highlighted) .post_media { margin-top: 0; margin-bottom: 15px; } diff --git a/templates/base.html b/templates/base.html index 5a1a493..6f8649a 100644 --- a/templates/base.html +++ b/templates/base.html @@ -11,7 +11,7 @@ {% endblock %} - +