From 25d199721b15b8e0c73b72a2f50a8a302b1fcca2 Mon Sep 17 00:00:00 2001 From: Joe Maples Date: Wed, 12 Apr 2023 11:15:19 -0400 Subject: [PATCH 1/2] Add short_path arg, use it for copy urls --- src/args.rs | 3 +++ templates/pasta.html | 6 +++--- templates/pastalist.html | 7 ++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/args.rs b/src/args.rs index 1dc3e722..2dd2fbda 100644 --- a/src/args.rs +++ b/src/args.rs @@ -54,6 +54,9 @@ pub struct Args { #[clap(long, env="MICROBIN_PUBLIC_PATH", default_value_t = PublicUrl(String::from("")))] pub public_path: PublicUrl, + #[clap(long, env="MICROBIN_SHORT_PATH", default_value_t = PublicUrl(String::from("")))] + pub short_path: PublicUrl, + #[clap(long, env = "MICROBIN_READONLY")] pub readonly: bool, diff --git a/templates/pasta.html b/templates/pasta.html index 50d3de4d..131039fb 100644 --- a/templates/pasta.html +++ b/templates/pasta.html @@ -66,8 +66,8 @@ const copyTextBtn = document.getElementById("copy-text-button") const copyRedirectBtn = document.getElementById("copy-redirect-button") const content = `{{ pasta.content_escaped() }}` - const url = `{{ args.public_path }}/pasta/{{pasta.id_as_animals()}}` - const redirect_url = `{{ args.public_path }}/url/{{pasta.id_as_animals()}}` + const url = (`{{ args.short_path }}` === "") ? `{{ args.public_path }}/pasta/{{pasta.id_as_animals()}}` : `{{ args.short_path }}/p/{{pasta.id_as_animals()}}` + const redirect_url = (`{{ args.short_path }}` === "") ? `{{ args.public_path }}/url/{{pasta.id_as_animals()}}` : `{{ args.short_path }}/u/{{pasta.id_as_animals()}}` const decodeEntity = (inputStr) => { var textarea = document.createElement("textarea"); @@ -151,4 +151,4 @@ } -{% include "footer.html" %} \ No newline at end of file +{% include "footer.html" %} diff --git a/templates/pastalist.html b/templates/pastalist.html index 269059ba..f4ff5642 100644 --- a/templates/pastalist.html +++ b/templates/pastalist.html @@ -90,8 +90,13 @@

URL Redirects

Open + {% if args.short_path.to_string() == "" %} Copy + {% else %} + Copy + {% endif %} {% if pasta.editable %} Edit {%- endif %} @@ -118,4 +123,4 @@

URL Redirects

- {% include "footer.html" %} \ No newline at end of file + {% include "footer.html" %} From 95aa514a3076f1ac3147364d19f46201e7fdceba Mon Sep 17 00:00:00 2001 From: Joe Maples Date: Wed, 12 Apr 2023 11:29:52 -0400 Subject: [PATCH 2/2] Add shortened endpoints for using short_path --- src/endpoints/pasta.rs | 27 +++++++++++++++++++++++---- src/main.rs | 2 ++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/endpoints/pasta.rs b/src/endpoints/pasta.rs index cf31fa35..da5cb1a9 100644 --- a/src/endpoints/pasta.rs +++ b/src/endpoints/pasta.rs @@ -18,8 +18,7 @@ struct PastaTemplate<'a> { args: &'a Args, } -#[get("/pasta/{id}")] -pub async fn getpasta(data: web::Data, id: web::Path) -> HttpResponse { +fn pastaresponse(data: web::Data, id: web::Path) -> HttpResponse { // get access to the pasta collection let mut pastas = data.pastas.lock().unwrap(); @@ -82,8 +81,17 @@ pub async fn getpasta(data: web::Data, id: web::Path) -> HttpR .body(ErrorTemplate { args: &ARGS }.render().unwrap()) } -#[get("/url/{id}")] -pub async fn redirecturl(data: web::Data, id: web::Path) -> HttpResponse { +#[get("/pasta/{id}")] +pub async fn getpasta(data: web::Data, id: web::Path) -> HttpResponse { + pastaresponse(data, id) +} + +#[get("/p/{id}")] +pub async fn getshortpasta(data: web::Data, id: web::Path) -> HttpResponse { + pastaresponse(data, id) +} + +fn urlresponse(data: web::Data, id: web::Path) -> HttpResponse { // get access to the pasta collection let mut pastas = data.pastas.lock().unwrap(); @@ -149,6 +157,17 @@ pub async fn redirecturl(data: web::Data, id: web::Path) -> Ht .body(ErrorTemplate { args: &ARGS }.render().unwrap()) } + +#[get("/url/{id}")] +pub async fn redirecturl(data: web::Data, id: web::Path) -> HttpResponse { + urlresponse(data, id) +} + +#[get("/u/{id}")] +pub async fn shortredirecturl(data: web::Data, id: web::Path) -> HttpResponse { + urlresponse(data, id) +} + #[get("/raw/{id}")] pub async fn getrawpasta(data: web::Data, id: web::Path) -> String { // get access to the pasta collection diff --git a/src/main.rs b/src/main.rs index cfbcc728..20652f3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,8 +90,10 @@ async fn main() -> std::io::Result<()> { .service(create::index) .service(info::info) .service(pasta_endpoint::getpasta) + .service(pasta_endpoint::getshortpasta) .service(pasta_endpoint::getrawpasta) .service(pasta_endpoint::redirecturl) + .service(pasta_endpoint::shortredirecturl) .service(edit::get_edit) .service(edit::post_edit) .service(static_resources::static_resources)