added customizability for /pasta/, /url/ and /raw/ endpoints

This commit is contained in:
Schrottkatze 2023-02-27 00:18:00 +01:00
parent f352129c78
commit 51751e3ee2
5 changed files with 31 additions and 10 deletions

View file

@ -133,6 +133,18 @@ pub struct Args {
/// Enable the use of Hash IDs for shorter URLs instead of animal names. /// Enable the use of Hash IDs for shorter URLs instead of animal names.
#[clap(long, env = "MICROBIN_HASH_IDS")] #[clap(long, env = "MICROBIN_HASH_IDS")]
pub hash_ids: bool, pub hash_ids: bool,
/// Endpoint for /url/
#[clap(long, env = "MICROBIN_URL_EP", default_value = "url" )]
pub url_endpoint: String,
/// Endpoint for /pasta/
#[clap(long, env = "MICROBIN_PASTA_EP", default_value = "pasta" )]
pub pasta_endpoint: String,
/// Endpoint for /raw/
#[clap(long, env = "MICROBIN_RAW_EP", default_value = "raw" )]
pub raw_endpoint: String,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -197,6 +197,6 @@ pub async fn create(
CONVERTER.to_names(id) CONVERTER.to_names(id)
}; };
Ok(HttpResponse::Found() Ok(HttpResponse::Found()
.append_header(("Location", format!("{}/pasta/{}", ARGS.public_path, slug))) .append_header(("Location", format!("{}/{}/{}", ARGS.public_path, ARGS.pasta_endpoint, slug)))
.finish()) .finish())
} }

View file

@ -19,7 +19,6 @@ struct PastaTemplate<'a> {
} }
/// Endpoint to view a pasta. /// Endpoint to view a pasta.
#[get("/pasta/{id}")]
pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse { pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
// get access to the pasta collection // get access to the pasta collection
let mut pastas = data.pastas.lock().await; let mut pastas = data.pastas.lock().await;
@ -84,7 +83,6 @@ pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpR
} }
/// Endpoint for redirection. /// Endpoint for redirection.
#[get("/url/{id}")]
pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse { pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
// get access to the pasta collection // get access to the pasta collection
let mut pastas = data.pastas.lock().await; let mut pastas = data.pastas.lock().await;
@ -152,7 +150,6 @@ pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> Ht
} }
/// Endpoint to request pasta as raw file. /// Endpoint to request pasta as raw file.
#[get("/raw/{id}")]
pub async fn getrawpasta(data: web::Data<AppState>, id: web::Path<String>) -> String { pub async fn getrawpasta(data: web::Data<AppState>, id: web::Path<String>) -> String {
// get access to the pasta collection // get access to the pasta collection
let mut pastas = data.pastas.lock().await; let mut pastas = data.pastas.lock().await;

View file

@ -83,9 +83,21 @@ async fn main() -> std::io::Result<()> {
.wrap(middleware::NormalizePath::trim()) .wrap(middleware::NormalizePath::trim())
.service(create::index) .service(create::index)
.service(info::info) .service(info::info)
.service(pasta_endpoint::getpasta) .route(
.service(pasta_endpoint::getrawpasta) &format!("/{}/{{id}}", ARGS.pasta_endpoint),
.service(pasta_endpoint::redirecturl) web::get().to(pasta_endpoint::getpasta)
)
.route(
&format!("/{}/{{id}}", ARGS.raw_endpoint),
web::get().to(pasta_endpoint::getrawpasta)
)
.route(
&format!("/{}/{{id}}", ARGS.url_endpoint),
web::get().to(pasta_endpoint::redirecturl)
)
//.service(pasta_endpoint::getpasta)
//.service(pasta_endpoint::getrawpasta)
//.service(pasta_endpoint::redirecturl)
.service(edit::get_edit) .service(edit::get_edit)
.service(edit::post_edit) .service(edit::post_edit)
.service(static_resources::static_resources) .service(static_resources::static_resources)

View file

@ -9,7 +9,7 @@
Copy Redirect Copy Redirect
</button> </button>
{%- endif %} {%- endif %}
<a style="margin-right: 1rem" href="{{ args.public_path }}/raw/{{pasta.id_as_animals()}}">Raw Text <a style="margin-right: 1rem" href="{{ args.public_path }}/{{ args.raw_endpoint }}/{{pasta.id_as_animals()}}">Raw Text
Content</a> Content</a>
{%- endif %} {%- endif %}
{% if args.qr && args.public_path.to_string() != "" %} {% if args.qr && args.public_path.to_string() != "" %}
@ -66,8 +66,8 @@
const copyTextBtn = document.getElementById("copy-text-button") const copyTextBtn = document.getElementById("copy-text-button")
const copyRedirectBtn = document.getElementById("copy-redirect-button") const copyRedirectBtn = document.getElementById("copy-redirect-button")
const content = `{{ pasta.content_escaped() }}` const content = `{{ pasta.content_escaped() }}`
const url = `{{ args.public_path }}/pasta/{{pasta.id_as_animals()}}` const url = `{{ args.public_path }}/{{ args.pasta_endpoint }}/{{pasta.id_as_animals()}}`
const redirect_url = `{{ args.public_path }}/url/{{pasta.id_as_animals()}}` const redirect_url = `{{ args.public_path }}/{{ args.url_endpoint }}/{{pasta.id_as_animals()}}`
copyURLBtn.addEventListener("click", () => { copyURLBtn.addEventListener("click", () => {
navigator.clipboard.writeText(url) navigator.clipboard.writeText(url)