diff --git a/src/misc/utils.rs b/src/misc/utils.rs index e3cdeba..418ce6b 100644 --- a/src/misc/utils.rs +++ b/src/misc/utils.rs @@ -1,5 +1,9 @@ +use actix_web::{ + http::header::{self, ContentType, TryIntoHeaderValue}, + HttpRequest, HttpResponse, Responder, +}; use cached::proc_macro::cached; -use reqwest::Client; +use reqwest::{Client, StatusCode}; use crate::config::FileConfig; @@ -26,3 +30,18 @@ pub fn get_url(fc: FileConfig) -> String { pub fn make_kw(list: &[&str]) -> Option { Some(list.join(", ")) } + +/// Send HTML file +pub struct Html(pub String); +impl Responder for Html { + type Body = String; + + fn respond_to(self, _req: &HttpRequest) -> HttpResponse { + let mut res = HttpResponse::with_body(StatusCode::OK, self.0); + res.headers_mut().insert( + header::CONTENT_TYPE, + ContentType::html().try_into_value().unwrap(), + ); + res + } +} diff --git a/src/routes/agreements.rs b/src/routes/agreements.rs index d26bd81..3589c37 100644 --- a/src/routes/agreements.rs +++ b/src/routes/agreements.rs @@ -1,5 +1,5 @@ use crate::{config::Config, misc::utils::get_url, template::Infos}; -use actix_web::{get, routes, web, HttpResponse, Responder}; +use actix_web::{get, http::header::ContentType, routes, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; @@ -7,7 +7,9 @@ use ramhorns::Content; #[get("/.well-known/security.txt")] #[get("/security.txt")] async fn security(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_securitytxt(config.get_ref().to_owned())) + HttpResponse::Ok() + .content_type(ContentType::plaintext()) + .body(build_securitytxt(config.get_ref().to_owned())) } #[derive(Content, Debug)] @@ -32,7 +34,9 @@ fn build_securitytxt(config: Config) -> String { #[get("/humans.txt")] async fn humans(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_humanstxt(config.get_ref().to_owned())) + HttpResponse::Ok() + .content_type(ContentType::plaintext()) + .body(build_humanstxt(config.get_ref().to_owned())) } #[derive(Content, Debug)] @@ -57,7 +61,9 @@ fn build_humanstxt(config: Config) -> String { #[get("/robots.txt")] async fn robots() -> impl Responder { - HttpResponse::Ok().body(build_robotstxt()) + HttpResponse::Ok() + .content_type(ContentType::plaintext()) + .body(build_robotstxt()) } #[once] diff --git a/src/routes/blog.rs b/src/routes/blog.rs index a941d8f..d567bc9 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -21,7 +21,7 @@ use crate::{ markdown::{ get_metadata, get_options, read_file, File, FileMetadataBlog, TypeFileMetadata, }, - utils::{get_url, make_kw}, + utils::{get_url, make_kw, Html}, }, template::{Infos, NavBar}, }; @@ -30,7 +30,7 @@ const MIME_TYPE_RSS: &str = "application/rss+xml"; #[get("/blog")] async fn index(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_index(config.get_ref().to_owned())) + Html(build_index(config.get_ref().to_owned())) } #[derive(Content, Debug)] @@ -185,7 +185,7 @@ struct BlogPostTemplate { #[get("/blog/p/{id}")] async fn page(path: web::Path<(String,)>, config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_post(path.into_inner().0, config.get_ref().to_owned())) + Html(build_post(path.into_inner().0, config.get_ref().to_owned())) } fn build_post(file: String, config: Config) -> String { diff --git a/src/routes/contact.rs b/src/routes/contact.rs index b348a2a..91b7775 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -1,4 +1,4 @@ -use actix_web::{get, routes, web, HttpRequest, HttpResponse, Responder}; +use actix_web::{get, routes, web, HttpRequest, Responder}; use cached::proc_macro::once; use glob::glob; use ramhorns::Content; @@ -8,7 +8,7 @@ use crate::{ config::Config, misc::{ markdown::{read_file, File, TypeFileMetadata}, - utils::make_kw, + utils::{make_kw, Html}, }, template::{Infos, NavBar}, }; @@ -27,7 +27,7 @@ pub fn pages(cfg: &mut web::ServiceConfig) { #[get("")] async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) + Html(build_page(config.get_ref().to_owned())) } #[derive(Clone, Debug)] diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index dedb5f7..82802f9 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -4,17 +4,17 @@ use crate::{ config::Config, misc::{ github::{fetch_pr, ProjectState}, - utils::make_kw, + utils::{make_kw, Html}, }, template::{Infos, NavBar}, }; -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, Responder}; use cached::proc_macro::once; use ramhorns::Content; #[get("/contrib")] async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned()).await) + Html(build_page(config.get_ref().to_owned()).await) } #[derive(Content, Debug)] diff --git a/src/routes/index.rs b/src/routes/index.rs index 6f1fa8e..b8291d8 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -1,16 +1,16 @@ -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, Responder}; use cached::proc_macro::once; use ramhorns::Content; use crate::{ config::Config, - misc::utils::make_kw, + misc::utils::{make_kw, Html}, template::{Infos, NavBar}, }; #[get("/")] async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) + Html(build_page(config.get_ref().to_owned())) } #[derive(Content, Debug)] diff --git a/src/routes/not_found.rs b/src/routes/not_found.rs index c257142..861d6a6 100644 --- a/src/routes/not_found.rs +++ b/src/routes/not_found.rs @@ -1,15 +1,15 @@ -use actix_web::{web, HttpResponse, Responder}; +use actix_web::{web, Responder}; use cached::proc_macro::once; use ramhorns::Content; use crate::{ config::Config, - misc::utils::get_url, + misc::utils::{get_url, Html}, template::{Infos, NavBar}, }; pub async fn page(config: web::Data) -> impl Responder { - HttpResponse::NotFound().body(build_page(config.get_ref().to_owned())) + Html(build_page(config.get_ref().to_owned())) } #[derive(Content, Debug)] diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index 70975c2..e0fad8d 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -1,4 +1,4 @@ -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, Responder}; use cached::proc_macro::once; use glob::glob; use ramhorns::Content; @@ -7,14 +7,14 @@ use crate::{ config::Config, misc::{ markdown::{read_file, File, TypeFileMetadata}, - utils::make_kw, + utils::{make_kw, Html}, }, template::{Infos, NavBar}, }; #[get("/portfolio")] async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) + Html(build_page(config.get_ref().to_owned())) } #[derive(Content, Debug)] diff --git a/src/routes/web3.rs b/src/routes/web3.rs index 0150b46..e3242a8 100644 --- a/src/routes/web3.rs +++ b/src/routes/web3.rs @@ -1,11 +1,15 @@ -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, Responder}; use cached::proc_macro::once; -use crate::{config::Config, misc::utils::make_kw, template::Infos}; +use crate::{ + config::Config, + misc::utils::{make_kw, Html}, + template::Infos, +}; #[get("/web3")] async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) + Html(build_page(config.get_ref().to_owned())) } #[once]