refactor(config): use a separate section for the landing page (#65)

* add [landing_page] section to config

Migration path:

Old:

```
[server]
landing_page = "Landing page text."
landing_page_file = "index.html"
landing_page_content_type = "text/html; charset=utf-8"
```

New:

```
[landing_page]
text = "Landing page text."
file = "index.html"
content_type = "text/html; charset=utf-8"
```

* fix typo and remove comments

* make the section optional

* make the section optional

* test(server): fix landing page related test failures

* also change html_form.toml

* do not break current config

* refactor(config): deprecate server.landing_page config fields

---------

Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
This commit is contained in:
Helmut K. C. Tessarek 2023-06-23 11:20:24 -04:00 committed by GitHub
parent 62c461702a
commit aa1734b3f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 38 deletions

View File

@ -9,7 +9,9 @@ max_content_length = "10MB"
upload_path = "./upload"
timeout = "30s"
expose_version = false
landing_page = """
[landing_page]
text = """
@ -31,7 +33,8 @@ changed this.
Check out the GitHub repository at https://github.com/orhun/rustypaste
Command line tool is available at https://github.com/orhun/rustypaste-cli
"""
landing_page_content_type = "text/plain; charset=utf-8"
#file = "index.txt"
content_type = "text/plain; charset=utf-8"
[paste]
random_url = { enabled = true, type = "petname", words = 2, separator = "-" }

View File

@ -9,8 +9,10 @@ max_content_length = "10MB"
upload_path = "./upload"
timeout = "30s"
expose_version = false
landing_page_file = "landing_page.html"
landing_page_content_type = "text/html; charset=utf-8"
[landing_page]
file = "landing_page.html"
content_type = "text/html; charset=utf-8"
[paste]
random_url = { enabled = true, type = "petname", words = 2, separator = "-" }

View File

@ -2,9 +2,11 @@
address="127.0.0.1:8000"
max_content_length="10MB"
upload_path="./upload"
landing_page="awesome_landing"
landing_page_file="page.txt"
landing_page_content_type = "text/plain; charset=utf-8"
[landing_page]
text="awesome_landing"
file="page.txt"
content_type = "text/plain; charset=utf-8"
[paste]
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }

View File

@ -2,8 +2,10 @@
address="127.0.0.1:8000"
max_content_length="10MB"
upload_path="./upload"
landing_page="awesome_landing"
landing_page_content_type = "text/plain; charset=utf-8"
[landing_page]
text="awesome_landing"
content_type = "text/plain; charset=utf-8"
[paste]
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }

View File

@ -15,6 +15,8 @@ pub struct Config {
pub server: ServerConfig,
/// Paste configuration.
pub paste: PasteConfig,
/// Landing page configuration.
pub landing_page: Option<LandingPageConfig>,
}
/// General settings for configuration.
@ -43,14 +45,25 @@ pub struct ServerConfig {
pub timeout: Option<Duration>,
/// Authentication token.
pub auth_token: Option<String>,
/// Landing page text.
pub landing_page: Option<String>,
/// Landing page file.
pub landing_page_file: Option<String>,
/// Landing page content-type
pub landing_page_content_type: Option<String>,
/// Expose version.
pub expose_version: Option<bool>,
/// Landing page text.
#[deprecated(note = "use the [landing_page] table")]
pub landing_page: Option<String>,
/// Landing page content-type.
#[deprecated(note = "use the [landing_page] table")]
pub landing_page_content_type: Option<String>,
}
/// Landing page configuration.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct LandingPageConfig {
/// Landing page text.
pub text: Option<String>,
/// Landing page file.
pub file: Option<String>,
/// Landing page content-type
pub content_type: Option<String>,
}
/// Paste configuration.

View File

@ -12,6 +12,7 @@ use actix_web::{error, get, post, web, Error, HttpRequest, HttpResponse};
use awc::Client;
use byte_unit::Byte;
use futures_util::stream::StreamExt;
use mime::TEXT_PLAIN_UTF_8;
use serde::Deserialize;
use std::convert::TryFrom;
use std::env;
@ -20,26 +21,40 @@ use std::sync::RwLock;
/// Shows the landing page.
#[get("/")]
#[allow(deprecated)]
async fn index(config: web::Data<RwLock<Config>>) -> Result<HttpResponse, Error> {
let config = config
.read()
.map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?;
let content_type = config
.server
.landing_page_content_type
.clone()
.unwrap_or("text/plain; charset=utf-8".to_string());
let mut landing_page = config.server.landing_page.clone();
if let Some(file) = &config.server.landing_page_file {
landing_page = fs::read_to_string(file).ok();
}
match &landing_page {
Some(page) => Ok(HttpResponse::Ok()
.content_type(content_type)
.body(page.clone())),
None => Ok(HttpResponse::Found()
.append_header(("Location", env!("CARGO_PKG_HOMEPAGE")))
.finish()),
let redirect = HttpResponse::Found()
.append_header(("Location", env!("CARGO_PKG_HOMEPAGE")))
.finish();
if let Some(mut landing_page) = config.landing_page.clone() {
if config.server.landing_page.is_some() {
landing_page.text = config.server.landing_page.clone();
log::warn!("[server].landing_page is deprecated, please use [landing_page].text");
}
if config.server.landing_page_content_type.is_some() {
landing_page.content_type = config.server.landing_page_content_type.clone();
log::warn!(
"[server].landing_page_content_type is deprecated, please use [landing_page].content_type"
);
}
let content_type = landing_page
.content_type
.unwrap_or(TEXT_PLAIN_UTF_8.to_string());
let mut text = landing_page.text.clone();
if let Some(file) = landing_page.file {
text = fs::read_to_string(file).ok();
}
match &text {
Some(page) => Ok(HttpResponse::Ok()
.content_type(content_type)
.body(page.clone())),
None => Ok(redirect),
}
} else {
Ok(redirect)
}
}
@ -283,6 +298,7 @@ pub fn configure_routes(cfg: &mut web::ServiceConfig) {
#[cfg(test)]
mod tests {
use super::*;
use crate::config::LandingPageConfig;
use crate::middleware::ContentLengthLimiter;
use crate::random::{RandomURLConfig, RandomURLType};
use actix_web::body::MessageBody;
@ -357,8 +373,13 @@ mod tests {
#[actix_web::test]
async fn test_index_with_landing_page() -> Result<(), Error> {
let mut config = Config::default();
config.server.landing_page = Some(String::from("landing page"));
let config = Config {
landing_page: Some(LandingPageConfig {
text: Some(String::from("landing page")),
..Default::default()
}),
..Default::default()
};
let app = test::init_service(
App::new()
.app_data(Data::new(RwLock::new(config)))
@ -377,10 +398,15 @@ mod tests {
#[actix_web::test]
async fn test_index_with_landing_page_file() -> Result<(), Error> {
let filename = "landing_page.txt";
let mut config = Config::default();
let config = Config {
landing_page: Some(LandingPageConfig {
file: Some(filename.to_string()),
..Default::default()
}),
..Default::default()
};
let mut file = File::create(filename)?;
file.write_all("landing page from file".as_bytes())?;
config.server.landing_page_file = Some(filename.to_string());
let app = test::init_service(
App::new()
.app_data(Data::new(RwLock::new(config)))
@ -400,9 +426,14 @@ mod tests {
#[actix_web::test]
async fn test_index_with_landing_page_file_not_found() -> Result<(), Error> {
let filename = "landing_page.txt";
let mut config = Config::default();
config.server.landing_page = Some(String::from("landing page"));
config.server.landing_page_file = Some(filename.to_string());
let config = Config {
landing_page: Some(LandingPageConfig {
text: Some(String::from("landing page")),
file: Some(filename.to_string()),
..Default::default()
}),
..Default::default()
};
let app = test::init_service(
App::new()
.app_data(Data::new(RwLock::new(config)))