base url en variable de entorno

This commit is contained in:
serxoz 2022-10-24 17:00:07 +02:00
parent c2167c2126
commit 6f657250fd
4 changed files with 24 additions and 10 deletions

View File

@ -6,10 +6,11 @@ Devolve un link para descargalo.
## Execución
```
UPLOADS="/tmp/uploads/" PORT=4000 cargo run
UPLOADS="/tmp/uploads/" BASE_URL="http://127.0.0.1" PORT=4000 cargo run
```
As variables de entorno UPLOADS e PORT son opcionais tendo como valores por defecto:
As variables de entorno UPLOADS, BASE_URL e PORT son opcionais tendo como valores por defecto:
- UPLOADS = "uploads/"
- BASE_URL = "http://127.0.0.1"
- PORT = 3000
## Funcionamento

View File

@ -26,3 +26,14 @@ pub fn env_listen_port() -> u16 {
None => 3000,
}
}
// le a variable de entorno BASE_URL para configurar o porto de escoita
// por defecto: http://localhost:3000
pub fn env_base_url() -> String {
let port = env_listen_port();
match env::var_os("BASE_URL") {
Some(val) => format!("{}:{}", val.into_string().unwrap(), port),
None => format!("{}:{}", "http://localhost", port),
}
}

View File

@ -1,16 +1,18 @@
use axum::response::{Html, IntoResponse, Response};
use crate::lib::env_base_url;
// basic handler that responds with a static string
pub async fn basic() -> Response {
Html(
let url = env_base_url();
let html = format!(
"<pre>
SH4R.IN
=======
HTTP POST files here:
curl -F'file=@yourfile.png' https://sh4r.in
curl -F'file=@yourfile.png' {}
You can also POST remote URLs:
curl -F'url=http://example.com/image.jpg' https://sh4r.in
curl -F'url=http://example.com/image.jpg' {}
File URLs are valid for aproximately 5 days.
@ -34,11 +36,11 @@ and the originating IP address blocked from further uploads.
UPLOAD DIRECTLY
---------------
</pre>
<form action=\"https://sh4r.in\" method=\"POST\" enctype=\"multipart/form-data\">
<form action=\"{}\" method=\"POST\" enctype=\"multipart/form-data\">
<input class=\"form-control\" type=\"file\" name=\"file\">
<input class=\"form-control\" type=\"submit\" value=\"send\">
</form>
",
)
.into_response()
url, url, url);
Html(html).into_response()
}

View File

@ -1,4 +1,4 @@
use crate::lib::{randstr, env_uploads_dir};
use crate::lib::{randstr, env_uploads_dir, env_base_url};
use axum::{
extract::Multipart,
response::{IntoResponse, Response},
@ -28,7 +28,7 @@ pub async fn upload(mut multipart: Multipart) -> Response {
let mut file = File::create(dest).await.expect("error creando arquivo");
file.write_all(&data).await.expect("error gardando contido");
link = format!("https://sh4r.in/f/{}", fillo);
link = format!("{}/f/{}", env_base_url(), fillo);
}
link.into_response()