0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-21 21:48:21 -05:00

♻️ refactor: standardize the content-type header by using an enum value over typing it manually (#474)

* ♻️  refactor: change content-type

* 🐛  fix: change parameters that were passed to the settings function

---------

Co-authored-by: neon_arch <mustafadhuleb53@gmail.com>
This commit is contained in:
Леонтий Вартанян 2023-12-31 13:31:07 +03:00 committed by GitHub
parent 5b4864424a
commit 5020f36c90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 57 deletions

View File

@ -6,22 +6,20 @@ use crate::{
config::parser::Config, config::parser::Config,
handler::{file_path, FileType}, handler::{file_path, FileType},
}; };
use actix_web::{get, web, HttpRequest, HttpResponse}; use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
use std::fs::read_to_string; use std::fs::read_to_string;
/// Handles the route of index page or main page of the `websurfx` meta search engine website. /// Handles the route of index page or main page of the `websurfx` meta search engine website.
#[get("/")] #[get("/")]
pub async fn index(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> { pub async fn index(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> {
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
.content_type("text/html; charset=utf-8") crate::templates::views::index::index(
.body( &config.style.colorscheme,
crate::templates::views::index::index( &config.style.theme,
&config.style.colorscheme, &config.style.animation,
&config.style.theme, )
&config.style.animation, .0,
) ))
.0,
))
} }
/// Handles the route of any other accessed route/page which is not provided by the /// Handles the route of any other accessed route/page which is not provided by the
@ -29,16 +27,14 @@ pub async fn index(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn st
pub async fn not_found( pub async fn not_found(
config: web::Data<Config>, config: web::Data<Config>,
) -> Result<HttpResponse, Box<dyn std::error::Error>> { ) -> Result<HttpResponse, Box<dyn std::error::Error>> {
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
.content_type("text/html; charset=utf-8") crate::templates::views::not_found::not_found(
.body( &config.style.colorscheme,
crate::templates::views::not_found::not_found( &config.style.theme,
&config.style.colorscheme, &config.style.animation,
&config.style.theme, )
&config.style.animation, .0,
) ))
.0,
))
} }
/// Handles the route of robots.txt page of the `websurfx` meta search engine website. /// Handles the route of robots.txt page of the `websurfx` meta search engine website.
@ -47,23 +43,21 @@ pub async fn robots_data(_req: HttpRequest) -> Result<HttpResponse, Box<dyn std:
let page_content: String = let page_content: String =
read_to_string(format!("{}/robots.txt", file_path(FileType::Theme)?))?; read_to_string(format!("{}/robots.txt", file_path(FileType::Theme)?))?;
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("text/plain; charset=ascii") .content_type(ContentType::plaintext())
.body(page_content)) .body(page_content))
} }
/// Handles the route of about page of the `websurfx` meta search engine website. /// Handles the route of about page of the `websurfx` meta search engine website.
#[get("/about")] #[get("/about")]
pub async fn about(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> { pub async fn about(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> {
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
.content_type("text/html; charset=utf-8") crate::templates::views::about::about(
.body( &config.style.colorscheme,
crate::templates::views::about::about( &config.style.theme,
&config.style.colorscheme, &config.style.animation,
&config.style.theme, )
&config.style.animation, .0,
) ))
.0,
))
} }
/// Handles the route of settings page of the `websurfx` meta search engine website. /// Handles the route of settings page of the `websurfx` meta search engine website.
@ -71,16 +65,14 @@ pub async fn about(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn st
pub async fn settings( pub async fn settings(
config: web::Data<Config>, config: web::Data<Config>,
) -> Result<HttpResponse, Box<dyn std::error::Error>> { ) -> Result<HttpResponse, Box<dyn std::error::Error>> {
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
.content_type("text/html; charset=utf-8") crate::templates::views::settings::settings(
.body( config.safe_search,
crate::templates::views::settings::settings( &config.style.colorscheme,
config.safe_search, &config.style.theme,
&config.style.colorscheme, &config.style.animation,
&config.style.theme, &config.upstream_search_engines,
&config.style.animation, )?
&config.upstream_search_engines, .0,
)? ))
.0,
))
} }

View File

@ -11,7 +11,7 @@ use crate::{
}, },
results::aggregator::aggregate, results::aggregator::aggregate,
}; };
use actix_web::{get, web, HttpRequest, HttpResponse}; use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
use regex::Regex; use regex::Regex;
use std::{ use std::{
fs::File, fs::File,
@ -68,18 +68,16 @@ pub async fn search(
get_results(page + 1) get_results(page + 1)
); );
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
.content_type("text/html; charset=utf-8") crate::templates::views::search::search(
.body( &config.style.colorscheme,
crate::templates::views::search::search( &config.style.theme,
&config.style.colorscheme, &config.style.animation,
&config.style.theme, query,
&config.style.animation, &results?,
query, )
&results?, .0,
) ))
.0,
))
} }
None => Ok(HttpResponse::TemporaryRedirect() None => Ok(HttpResponse::TemporaryRedirect()
.insert_header(("location", "/")) .insert_header(("location", "/"))