feat(server): support a file for the landing page (#64)

* implemented landing_page_file

* fixed issues found by linter

* fixed formatting

* refactor(config): refactor landing page file handling

* add tests

* fix unnecessary conversion

* fix formatting

* remove comment

---------

Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
This commit is contained in:
Helmut K. C. Tessarek 2023-06-16 10:21:44 -04:00 committed by GitHub
parent f7beaef502
commit e0d6712dd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 1 deletions

View File

@ -0,0 +1,12 @@
[server]
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"
[paste]
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
default_extension = "txt"
duplicate_files = false

View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
landing_page="awesome_landing_from_file"
setup() {
echo $landing_page >page.txt
}
run_test() {
result=$(curl -s localhost:8000)
test "$landing_page" = "$result"
}
teardown() {
rm page.txt
}

View File

@ -45,6 +45,8 @@ pub struct ServerConfig {
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.

View File

@ -29,7 +29,11 @@ async fn index(config: web::Data<RwLock<Config>>) -> Result<HttpResponse, Error>
.landing_page_content_type
.clone()
.unwrap_or("text/plain; charset=utf-8".to_string());
match &config.server.landing_page {
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())),
@ -290,6 +294,8 @@ mod tests {
use actix_web::App;
use awc::ClientBuilder;
use glob::glob;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::str;
use std::thread;
@ -368,6 +374,49 @@ mod tests {
Ok(())
}
#[actix_web::test]
async fn test_index_with_landing_page_file() -> Result<(), Error> {
let filename = "landing_page.txt";
let mut config = Config::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)))
.service(index),
)
.await;
let request = TestRequest::default()
.insert_header(("content-type", "text/plain"))
.to_request();
let response = test::call_service(&app, request).await;
assert_eq!(StatusCode::OK, response.status());
assert_body(response.into_body(), "landing page from file").await?;
fs::remove_file(filename)?;
Ok(())
}
#[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 app = test::init_service(
App::new()
.app_data(Data::new(RwLock::new(config)))
.service(index),
)
.await;
let request = TestRequest::default()
.insert_header(("content-type", "text/plain"))
.to_request();
let response = test::call_service(&app, request).await;
assert_eq!(StatusCode::FOUND, response.status());
Ok(())
}
#[actix_web::test]
async fn test_version_without_auth() -> Result<(), Error> {
let mut config = Config::default();