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 { pub struct ServerConfig {
/// The socket address to bind. /// The socket address to bind.
pub address: String, pub address: String,
/// URL that can be used to access the server externally.
pub url: Option<String>,
/// Number of workers to start. /// Number of workers to start.
pub workers: Option<usize>, pub workers: Option<usize>,
/// Maximum content length. /// Maximum content length.

View file

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