feat(config): support overriding the server URL

This commit is contained in:
Orhun Parmaksız 2023-05-13 23:20:52 +03:00
parent 4e45b6a20d
commit 9ea7d5de8a
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 17 additions and 9 deletions

View file

@ -30,6 +30,8 @@ pub struct Settings {
pub struct ServerConfig {
/// The socket address to bind.
pub address: String,
/// URL that can be used to access the server externally.
pub url: Option<String>,
/// Number of workers to start.
pub workers: Option<usize>,
/// Maximum content length.

View file

@ -137,6 +137,18 @@ async fn upload(
) -> Result<HttpResponse, Error> {
let connection = request.connection_info().clone();
let host = connection.peer_addr().unwrap_or("unknown host");
let server_url = match config
.read()
.map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?
.server
.url
.clone()
{
Some(v) => v,
None => {
format!("{}://{}", connection.scheme(), connection.host(),)
}
};
auth::check(
host,
request.headers(),
@ -187,9 +199,8 @@ async fn upload(
.get_file(bytes_checksum)
{
urls.push(format!(
"{}://{}/{}\n",
connection.scheme(),
connection.host(),
"{}/{}\n",
server_url,
file.path
.file_name()
.map(|v| v.to_string_lossy())
@ -227,12 +238,7 @@ async fn upload(
Byte::from_bytes(paste.data.len() as u128).get_appropriate_unit(false),
host
);
urls.push(format!(
"{}://{}/{}\n",
connection.scheme(),
connection.host(),
file_name
));
urls.push(format!("{}/{}\n", server_url, file_name));
} else {
log::warn!("{} sent an invalid form field", host);
return Err(error::ErrorBadRequest("invalid form field"));