Ensure base path always starts and ends with /

The base path is an absolute path, so it should always start with /.
Whenever it is used, it must also always be followed by at least one /,
so baking that into the value makes sense. Finally, we can now
deduplicate the / in case the base path is the root.
This commit is contained in:
Joscha 2023-08-10 21:03:25 +02:00
parent d5a41abaff
commit 6ed6ff1a36
4 changed files with 18 additions and 19 deletions

View file

@ -22,7 +22,7 @@ mod default {
use std::{net::SocketAddr, time::Duration};
pub fn web_base() -> String {
"".to_string()
"/".to_string()
}
pub fn web_address() -> SocketAddr {
@ -138,13 +138,14 @@ impl ConfigFile {
}
fn web_base(&self) -> String {
self.web
.base
.strip_prefix('/')
.unwrap_or(&self.web.base)
.strip_suffix('/')
.unwrap_or(&self.web.base)
.to_string()
let mut base = self.web.base.clone();
if !base.starts_with('/') {
base.insert(0, '/');
}
if !base.ends_with('/') {
base.push('/');
}
base
}
fn web_runner_token(&self) -> String {
@ -204,6 +205,7 @@ pub struct RunnerServerConfig {
}
pub struct Config {
/// Always starts and ends with a `/`.
pub web_base: String,
pub web_address: SocketAddr,
pub web_runner_token: String,