mirror of
https://github.com/amigan/rustypaste-pretty.git
synced 2024-11-21 11:59:48 -05:00
feat(config): support overriding the server URL
This commit is contained in:
parent
4e45b6a20d
commit
9ea7d5de8a
2 changed files with 17 additions and 9 deletions
|
@ -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.
|
||||
|
|
|
@ -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"));
|
||||
|
|
Loading…
Reference in a new issue