fix the prefix

This commit is contained in:
Toshit Chawda 2024-03-25 17:51:00 -07:00
parent 47e30683cb
commit ff2a1ad269
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D

View file

@ -28,8 +28,8 @@ type HttpBody = http_body_util::Full<hyper::body::Bytes>;
#[command(version = clap::crate_version!())] #[command(version = clap::crate_version!())]
struct Cli { struct Cli {
/// URL prefix the server should serve on /// URL prefix the server should serve on
#[arg(long, default_value = "")] #[arg(long)]
prefix: String, prefix: Option<String>,
/// Port the server should bind to /// Port the server should bind to
#[arg(long, short, default_value = "4000")] #[arg(long, short, default_value = "4000")]
port: String, port: String,
@ -117,13 +117,18 @@ async fn main() -> Result<(), Error> {
let socket = bind(&addr, opt.unix_socket).await?; let socket = bind(&addr, opt.unix_socket).await?;
let prefix = if opt.prefix.starts_with('/') { let prefix = if let Some(prefix) = opt.prefix {
opt.prefix match (prefix.starts_with('/'), prefix.ends_with('/')) {
(true, true) => prefix,
(true, false) => prefix + "/",
(false, true) => "/".to_string() + &prefix,
(false, false) => "/".to_string() + &prefix + "/",
}
} else { } else {
"/".to_string() + &opt.prefix "/".to_string()
}; };
println!("listening on `{}`", addr); println!("listening on `{}` with prefix `{}`", addr, prefix);
while let Ok((stream, addr)) = socket.accept().await { while let Ok((stream, addr)) = socket.accept().await {
let prefix = prefix.clone(); let prefix = prefix.clone();
tokio::spawn(async move { tokio::spawn(async move {
@ -155,7 +160,7 @@ async fn accept_http(
{ {
let (res, fut) = upgrade::upgrade(&mut req)?; let (res, fut) = upgrade::upgrade(&mut req)?;
if uri == "/" { if uri.is_empty() {
tokio::spawn(async move { accept_ws(fut, addr.clone(), block_local).await }); tokio::spawn(async move { accept_ws(fut, addr.clone(), block_local).await });
} else if let Some(uri) = uri.strip_prefix('/').map(|x| x.to_string()) { } else if let Some(uri) = uri.strip_prefix('/').map(|x| x.to_string()) {
tokio::spawn(async move { accept_wsproxy(fut, uri, addr.clone(), block_local).await }); tokio::spawn(async move { accept_wsproxy(fut, uri, addr.clone(), block_local).await });