Allow setting a public url for all paths

This commit is contained in:
HeapUnderflow 2022-10-12 17:13:21 +02:00
parent 3ca89291dc
commit a404f9a997
No known key found for this signature in database
GPG key ID: 6BDB4D05427A70A2
9 changed files with 52 additions and 28 deletions

View file

@ -1,4 +1,7 @@
use std::convert::Infallible;
use std::fmt;
use std::net::IpAddr;
use std::str::FromStr;
use clap::Parser;
use lazy_static::lazy_static;
@ -48,6 +51,9 @@ pub struct Args {
#[clap(long, env="MICROBIN_PURE_HTML")]
pub pure_html: bool,
#[clap(long, env="MICROBIN_PUBLIC_PATH", default_value_t = PublicUrl(String::from("/")))]
pub public_path: PublicUrl,
#[clap(long, env="MICROBIN_READONLY")]
pub readonly: bool,
@ -59,4 +65,22 @@ pub struct Args {
#[clap(long, env="MICROBIN_WIDE")]
pub wide: bool,
}
#[derive(Debug, Clone)]
pub struct PublicUrl(pub String);
impl fmt::Display for PublicUrl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for PublicUrl {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let uri = s.strip_suffix('/').unwrap_or(s).to_owned();
Ok(PublicUrl(uri))
}
}

View file

@ -32,7 +32,7 @@ pub async fn create(
) -> Result<HttpResponse, Error> {
if ARGS.readonly {
return Ok(HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish());
}
@ -154,6 +154,6 @@ pub async fn create(
save_to_file(&pastas);
Ok(HttpResponse::Found()
.append_header(("Location", format!("/pasta/{}", to_animal_names(id))))
.append_header(("Location", format!("{}/pasta/{}", ARGS.public_path, to_animal_names(id))))
.finish())
}

View file

@ -28,7 +28,7 @@ pub async fn get_edit(data: web::Data<AppState>, id: web::Path<String>) -> HttpR
if pasta.id == id {
if !pasta.editable {
return HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish();
}
return HttpResponse::Ok().content_type("text/html").body(
@ -55,7 +55,7 @@ pub async fn post_edit(
) -> Result<HttpResponse, Error> {
if ARGS.readonly {
return Ok(HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish());
}
@ -85,7 +85,7 @@ pub async fn post_edit(
save_to_file(&pastas);
return Ok(HttpResponse::Found()
.append_header(("Location", format!("/pasta/{}", pastas[i].id_as_animals())))
.append_header(("Location", format!("{}/pasta/{}", ARGS.public_path, pastas[i].id_as_animals())))
.finish());
} else {
break;

View file

@ -17,7 +17,7 @@ struct PastaListTemplate<'a> {
pub async fn list(data: web::Data<AppState>) -> HttpResponse {
if ARGS.no_listing {
return HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish();
}

View file

@ -13,7 +13,7 @@ use std::fs;
pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
if ARGS.readonly {
return HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish();
}
@ -39,7 +39,7 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
// remove it from in-memory pasta list
pastas.remove(i);
return HttpResponse::Found()
.append_header(("Location", "/pastalist"))
.append_header(("Location", format!("{}/pastalist", ARGS.public_path)))
.finish();
}
}