Wide UI Mode
This commit is contained in:
32
src/main.rs
32
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<String> = 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::<Vec<&str>>()[1].to_string();
|
||||
}
|
||||
for arg in std::env::args().collect::<Vec<String>>() {
|
||||
match arg.split('=').collect::<Vec<&str>>()[0] {
|
||||
"--address" | "-a" => address = arg.split('=').collect::<Vec<&str>>()[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
|
||||
}
|
||||
|
@ -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<Comment>,
|
||||
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
|
||||
|
@ -15,6 +15,7 @@ struct SettingsTemplate {
|
||||
pub struct SettingsForm {
|
||||
front_page: Option<String>,
|
||||
layout: Option<String>,
|
||||
wide: Option<String>,
|
||||
comment_sort: Option<String>,
|
||||
hide_nsfw: Option<String>,
|
||||
}
|
||||
@ -31,8 +32,8 @@ pub async fn get(req: HttpRequest) -> HttpResponse {
|
||||
pub async fn set(req: HttpRequest, form: Form<SettingsForm>) -> 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] {
|
||||
|
@ -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();
|
||||
|
@ -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"),
|
||||
}
|
||||
|
Reference in New Issue
Block a user