From 37cb4d3fcb254632262ccd0120f1bd9598ced7b4 Mon Sep 17 00:00:00 2001 From: TheTechRobo <52163910+TheTechRobo@users.noreply.github.com> Date: Mon, 3 Oct 2022 17:27:35 -0400 Subject: [PATCH] feat(server): add landing page (#26) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(server): add landing page Fixes orhun/rustypaste#13 * feat(server): allow using {REPOSITORY} in landing page * fix(server): Get rid of unused import, add line about expiration * chore(fmt): cargo fmt * fix(tests): inject app data for fixing index test * feat(server): redirect to GitHub repository if landing page is not specified * test(fixtures): add fixture test for landing page Co-authored-by: Orhun Parmaksız --- config.toml | 15 +++++++ fixtures/test-server-landing-page/config.toml | 10 +++++ fixtures/test-server-landing-page/test.sh | 16 +++++++ src/config.rs | 2 + src/server.rs | 43 ++++++++++++++++--- 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 fixtures/test-server-landing-page/config.toml create mode 100755 fixtures/test-server-landing-page/test.sh diff --git a/config.toml b/config.toml index 26e9fb5..cdf3297 100644 --- a/config.toml +++ b/config.toml @@ -7,6 +7,21 @@ address="127.0.0.1:8000" max_content_length="10MB" upload_path="./upload" timeout="30s" +landing_page="""Submit files via HTTP POST here: + curl -F 'file=@example.txt' " +This will return the finished URL. + +The server administrator might remove any pastes that they do not personally +want to host. + +If you are the server administrator and want to change this page, just go +into your config file and change it! If you change the expiry time, it is +recommended that you do. + +Check out the GitHub repository at https://github.com/orhun/rustypaste + +By default, pastes expire every hour. The server admin may or may not have +changed this.""" [paste] random_url = { enabled = true, type = "petname", words = 2, separator = "-" } diff --git a/fixtures/test-server-landing-page/config.toml b/fixtures/test-server-landing-page/config.toml new file mode 100644 index 0000000..6f3be4b --- /dev/null +++ b/fixtures/test-server-landing-page/config.toml @@ -0,0 +1,10 @@ +[server] +address="127.0.0.1:8000" +max_content_length="10MB" +upload_path="./upload" +landing_page="awesome_landing" + +[paste] +random_url = { enabled = false, type = "petname", words = 2, separator = "-" } +default_extension = "txt" +duplicate_files = false diff --git a/fixtures/test-server-landing-page/test.sh b/fixtures/test-server-landing-page/test.sh new file mode 100755 index 0000000..2c49b0c --- /dev/null +++ b/fixtures/test-server-landing-page/test.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +landing_page="awesome_landing" + +setup() { + :; +} + +run_test() { + result=$(curl -s localhost:8000) + test "$landing_page" = "$result" +} + +teardown() { + :; +} diff --git a/src/config.rs b/src/config.rs index e330b9f..2c76767 100644 --- a/src/config.rs +++ b/src/config.rs @@ -41,6 +41,8 @@ pub struct ServerConfig { pub timeout: Option, /// Authentication token. pub auth_token: Option, + /// Landing page text. + pub landing_page: Option, } /// Paste configuration. diff --git a/src/server.rs b/src/server.rs index 234be89..8054d6e 100644 --- a/src/server.rs +++ b/src/server.rs @@ -8,7 +8,7 @@ use crate::util; use crate::AUTH_TOKEN_ENV; use actix_files::NamedFile; use actix_multipart::Multipart; -use actix_web::{error, get, post, web, Error, HttpRequest, HttpResponse, Responder}; +use actix_web::{error, get, post, web, Error, HttpRequest, HttpResponse}; use awc::Client; use byte_unit::Byte; use futures_util::stream::StreamExt; @@ -19,10 +19,16 @@ use std::sync::RwLock; /// Shows the landing page. #[get("/")] -async fn index() -> impl Responder { - HttpResponse::Found() - .append_header(("Location", env!("CARGO_PKG_HOMEPAGE"))) - .finish() +async fn index(config: web::Data>) -> Result { + let config = config + .read() + .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?; + match &config.server.landing_page { + Some(page) => Ok(HttpResponse::Ok().body(page.clone())), + None => Ok(HttpResponse::Found() + .append_header(("Location", env!("CARGO_PKG_HOMEPAGE"))) + .finish()), + } } /// Serves a file from the upload directory. @@ -260,7 +266,13 @@ mod tests { #[actix_web::test] async fn test_index() { - let app = test::init_service(App::new().service(index)).await; + let config = Config::default(); + 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(); @@ -268,6 +280,25 @@ mod tests { assert_eq!(StatusCode::FOUND, response.status()); } + #[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 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, "landing page").await?; + Ok(()) + } + #[actix_web::test] async fn test_auth() -> Result<(), Error> { let mut config = Config::default();