Леонтий Вартанян neon_arch commited on
Commit
5020f36
1 Parent(s): 5b48644

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

Browse files

* :recycle: refactor: change content-type

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

---------

Co-authored-by: neon_arch <[email protected]>

Files changed (2) hide show
  1. src/server/router.rs +36 -44
  2. src/server/routes/search.rs +11 -13
src/server/router.rs CHANGED
@@ -6,22 +6,20 @@ use crate::{
6
  config::parser::Config,
7
  handler::{file_path, FileType},
8
  };
9
- use actix_web::{get, web, HttpRequest, HttpResponse};
10
  use std::fs::read_to_string;
11
 
12
  /// Handles the route of index page or main page of the `websurfx` meta search engine website.
13
  #[get("/")]
14
  pub async fn index(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> {
15
- Ok(HttpResponse::Ok()
16
- .content_type("text/html; charset=utf-8")
17
- .body(
18
- crate::templates::views::index::index(
19
- &config.style.colorscheme,
20
- &config.style.theme,
21
- &config.style.animation,
22
- )
23
- .0,
24
- ))
25
  }
26
 
27
  /// 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
29
  pub async fn not_found(
30
  config: web::Data<Config>,
31
  ) -> Result<HttpResponse, Box<dyn std::error::Error>> {
32
- Ok(HttpResponse::Ok()
33
- .content_type("text/html; charset=utf-8")
34
- .body(
35
- crate::templates::views::not_found::not_found(
36
- &config.style.colorscheme,
37
- &config.style.theme,
38
- &config.style.animation,
39
- )
40
- .0,
41
- ))
42
  }
43
 
44
  /// 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:
47
  let page_content: String =
48
  read_to_string(format!("{}/robots.txt", file_path(FileType::Theme)?))?;
49
  Ok(HttpResponse::Ok()
50
- .content_type("text/plain; charset=ascii")
51
  .body(page_content))
52
  }
53
 
54
  /// Handles the route of about page of the `websurfx` meta search engine website.
55
  #[get("/about")]
56
  pub async fn about(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> {
57
- Ok(HttpResponse::Ok()
58
- .content_type("text/html; charset=utf-8")
59
- .body(
60
- crate::templates::views::about::about(
61
- &config.style.colorscheme,
62
- &config.style.theme,
63
- &config.style.animation,
64
- )
65
- .0,
66
- ))
67
  }
68
 
69
  /// 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
71
  pub async fn settings(
72
  config: web::Data<Config>,
73
  ) -> Result<HttpResponse, Box<dyn std::error::Error>> {
74
- Ok(HttpResponse::Ok()
75
- .content_type("text/html; charset=utf-8")
76
- .body(
77
- crate::templates::views::settings::settings(
78
- config.safe_search,
79
- &config.style.colorscheme,
80
- &config.style.theme,
81
- &config.style.animation,
82
- &config.upstream_search_engines,
83
- )?
84
- .0,
85
- ))
86
  }
 
6
  config::parser::Config,
7
  handler::{file_path, FileType},
8
  };
9
+ use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
10
  use std::fs::read_to_string;
11
 
12
  /// Handles the route of index page or main page of the `websurfx` meta search engine website.
13
  #[get("/")]
14
  pub async fn index(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> {
15
+ Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
16
+ crate::templates::views::index::index(
17
+ &config.style.colorscheme,
18
+ &config.style.theme,
19
+ &config.style.animation,
20
+ )
21
+ .0,
22
+ ))
 
 
23
  }
24
 
25
  /// Handles the route of any other accessed route/page which is not provided by the
 
27
  pub async fn not_found(
28
  config: web::Data<Config>,
29
  ) -> Result<HttpResponse, Box<dyn std::error::Error>> {
30
+ Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
31
+ crate::templates::views::not_found::not_found(
32
+ &config.style.colorscheme,
33
+ &config.style.theme,
34
+ &config.style.animation,
35
+ )
36
+ .0,
37
+ ))
 
 
38
  }
39
 
40
  /// Handles the route of robots.txt page of the `websurfx` meta search engine website.
 
43
  let page_content: String =
44
  read_to_string(format!("{}/robots.txt", file_path(FileType::Theme)?))?;
45
  Ok(HttpResponse::Ok()
46
+ .content_type(ContentType::plaintext())
47
  .body(page_content))
48
  }
49
 
50
  /// Handles the route of about page of the `websurfx` meta search engine website.
51
  #[get("/about")]
52
  pub async fn about(config: web::Data<Config>) -> Result<HttpResponse, Box<dyn std::error::Error>> {
53
+ Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
54
+ crate::templates::views::about::about(
55
+ &config.style.colorscheme,
56
+ &config.style.theme,
57
+ &config.style.animation,
58
+ )
59
+ .0,
60
+ ))
 
 
61
  }
62
 
63
  /// Handles the route of settings page of the `websurfx` meta search engine website.
 
65
  pub async fn settings(
66
  config: web::Data<Config>,
67
  ) -> Result<HttpResponse, Box<dyn std::error::Error>> {
68
+ Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
69
+ crate::templates::views::settings::settings(
70
+ config.safe_search,
71
+ &config.style.colorscheme,
72
+ &config.style.theme,
73
+ &config.style.animation,
74
+ &config.upstream_search_engines,
75
+ )?
76
+ .0,
77
+ ))
 
 
78
  }
src/server/routes/search.rs CHANGED
@@ -11,7 +11,7 @@ use crate::{
11
  },
12
  results::aggregator::aggregate,
13
  };
14
- use actix_web::{get, web, HttpRequest, HttpResponse};
15
  use regex::Regex;
16
  use std::{
17
  fs::File,
@@ -68,18 +68,16 @@ pub async fn search(
68
  get_results(page + 1)
69
  );
70
 
71
- Ok(HttpResponse::Ok()
72
- .content_type("text/html; charset=utf-8")
73
- .body(
74
- crate::templates::views::search::search(
75
- &config.style.colorscheme,
76
- &config.style.theme,
77
- &config.style.animation,
78
- query,
79
- &results?,
80
- )
81
- .0,
82
- ))
83
  }
84
  None => Ok(HttpResponse::TemporaryRedirect()
85
  .insert_header(("location", "/"))
 
11
  },
12
  results::aggregator::aggregate,
13
  };
14
+ use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
15
  use regex::Regex;
16
  use std::{
17
  fs::File,
 
68
  get_results(page + 1)
69
  );
70
 
71
+ Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
72
+ crate::templates::views::search::search(
73
+ &config.style.colorscheme,
74
+ &config.style.theme,
75
+ &config.style.animation,
76
+ query,
77
+ &results?,
78
+ )
79
+ .0,
80
+ ))
 
 
81
  }
82
  None => Ok(HttpResponse::TemporaryRedirect()
83
  .insert_header(("location", "/"))