📝 Update the guides to the latest PageTop API
This commit is contained in:
parent
c0e86f132d
commit
b99daec9a6
8 changed files with 27 additions and 28 deletions
|
|
@ -94,7 +94,7 @@ sentencias que modifican datos. Ver [Escribe SQL a mano](how-to/database.md#8-es
|
||||||
|
|
||||||
**¿Cómo muestro una página 404 o 500 con el aspecto del sitio?**
|
**¿Cómo muestro una página 404 o 500 con el aspecto del sitio?**
|
||||||
|
|
||||||
Devuelve `Err(ErrorPage::NotFound(Some(request)))` (o la variante que toque) desde el *handler*. Ver
|
Devuelve `Err(ErrorPage::NotFound(request))` (o la variante que toque) desde el *handler*. Ver
|
||||||
[Mostrar páginas de error](how-to/error-pages.md).
|
[Mostrar páginas de error](how-to/error-pages.md).
|
||||||
|
|
||||||
**¿Cómo comprimo las respuestas o limito el tamaño de lo que se envía?**
|
**¿Cómo comprimo las respuestas o limito el tamaño de lo que se envía?**
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ async fn plot(
|
||||||
web::Path(code): web::Path<String>,
|
web::Path(code): web::Path<String>,
|
||||||
) -> Result<Markup, ErrorPage> {
|
) -> Result<Markup, ErrorPage> {
|
||||||
let Some((_, holder)) = PLOTS.iter().find(|(c, _)| *c == code) else {
|
let Some((_, holder)) = PLOTS.iter().find(|(c, _)| *c == code) else {
|
||||||
return Err(ErrorPage::NotFound(Some(request)));
|
return Err(ErrorPage::NotFound(request));
|
||||||
};
|
};
|
||||||
let holder = holder.to_string();
|
let holder = holder.to_string();
|
||||||
Page::new(request)
|
Page::new(request)
|
||||||
|
|
@ -47,9 +47,8 @@ Elige la variante por lo que ha pasado:
|
||||||
| `ErrorPage::ServiceUnavailable` | 503 | El servicio no puede atender ahora (mantenimiento, carga) |
|
| `ErrorPage::ServiceUnavailable` | 503 | El servicio no puede atender ahora (mantenimiento, carga) |
|
||||||
| `ErrorPage::GatewayTimeout` | 504 | Un servicio externo no ha respondido a tiempo |
|
| `ErrorPage::GatewayTimeout` | 504 | Un servicio externo no ha respondido a tiempo |
|
||||||
|
|
||||||
Pásale siempre la petición (`Some(request)`): con ella la página de error sale en el idioma del
|
Se le pasa la petición: con ella la página de error sale en el idioma del visitante y sabe quién
|
||||||
visitante y sabe quién es, por ejemplo para mostrar su menú de cuenta. Con `None` también se
|
es, por ejemplo para mostrar su menú de cuenta.
|
||||||
muestra, pero sin esos datos.
|
|
||||||
|
|
||||||
Las rutas que no existen no necesitan nada: PageTop responde por sí solo con la misma página 404.
|
Las rutas que no existen no necesitan nada: PageTop responde por sí solo con la misma página 404.
|
||||||
|
|
||||||
|
|
@ -63,7 +62,7 @@ falla. Conviértelo con `map_err()` y sigue usando `?`:
|
||||||
async fn report(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
async fn report(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
||||||
let text = std::fs::read_to_string("data/report.txt").map_err(|err| {
|
let text = std::fs::read_to_string("data/report.txt").map_err(|err| {
|
||||||
trace::error!(%err, "Cannot read the report");
|
trace::error!(%err, "Cannot read the report");
|
||||||
ErrorPage::InternalError(Some(request.clone()))
|
ErrorPage::InternalError(request.clone())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Page::new(request)
|
Page::new(request)
|
||||||
|
|
@ -194,7 +193,7 @@ async fn plot(
|
||||||
web::Path(code): web::Path<String>,
|
web::Path(code): web::Path<String>,
|
||||||
) -> Result<Markup, ErrorPage> {
|
) -> Result<Markup, ErrorPage> {
|
||||||
let Some((_, holder)) = PLOTS.iter().find(|(c, _)| *c == code) else {
|
let Some((_, holder)) = PLOTS.iter().find(|(c, _)| *c == code) else {
|
||||||
return Err(ErrorPage::NotFound(Some(request)));
|
return Err(ErrorPage::NotFound(request));
|
||||||
};
|
};
|
||||||
let holder = holder.to_string();
|
let holder = holder.to_string();
|
||||||
Page::new(request)
|
Page::new(request)
|
||||||
|
|
@ -207,7 +206,7 @@ async fn plot(
|
||||||
async fn report(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
async fn report(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
||||||
let text = std::fs::read_to_string("data/report.txt").map_err(|err| {
|
let text = std::fs::read_to_string("data/report.txt").map_err(|err| {
|
||||||
trace::error!(%err, "Cannot read the report");
|
trace::error!(%err, "Cannot read the report");
|
||||||
ErrorPage::InternalError(Some(request.clone()))
|
ErrorPage::InternalError(request.clone())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Page::new(request)
|
Page::new(request)
|
||||||
|
|
@ -256,7 +255,7 @@ También puedes comprobarlo en una prueba (ve [Probar rutas y componentes](testi
|
||||||
# web::Path(code): web::Path<String>,
|
# web::Path(code): web::Path<String>,
|
||||||
# ) -> Result<Markup, ErrorPage> {
|
# ) -> Result<Markup, ErrorPage> {
|
||||||
# let Some((_, holder)) = PLOTS.iter().find(|(c, _)| *c == code) else {
|
# let Some((_, holder)) = PLOTS.iter().find(|(c, _)| *c == code) else {
|
||||||
# return Err(ErrorPage::NotFound(Some(request)));
|
# return Err(ErrorPage::NotFound(request));
|
||||||
# };
|
# };
|
||||||
# let holder = holder.to_string();
|
# let holder = holder.to_string();
|
||||||
# Page::new(request)
|
# Page::new(request)
|
||||||
|
|
|
||||||
|
|
@ -552,7 +552,7 @@ base_url = "https://garden.example.com"
|
||||||
# .with_to(gardener.email)
|
# .with_to(gardener.email)
|
||||||
# .send()
|
# .send()
|
||||||
# .await
|
# .await
|
||||||
# .map_err(|_| ErrorPage::InternalError(Some(request)))?;
|
# .map_err(|_| ErrorPage::InternalError(request))?;
|
||||||
# Ok(Redirect::see_other(cx.route("/harvest")))
|
# Ok(Redirect::see_other(cx.route("/harvest")))
|
||||||
# }
|
# }
|
||||||
use pagetop_mail::outbox;
|
use pagetop_mail::outbox;
|
||||||
|
|
|
||||||
|
|
@ -167,7 +167,7 @@ async fn view(
|
||||||
web::Path(id): web::Path<u32>,
|
web::Path(id): web::Path<u32>,
|
||||||
) -> Result<Markup, ErrorPage> {
|
) -> Result<Markup, ErrorPage> {
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
return Err(ErrorPage::NotFound(Some(request)));
|
return Err(ErrorPage::NotFound(request));
|
||||||
}
|
}
|
||||||
Page::new(request).render().await
|
Page::new(request).render().await
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ async fn upload(request: HttpRequest, multipart: web::Multipart) -> Result<Respo
|
||||||
Ok(sent) => sent,
|
Ok(sent) => sent,
|
||||||
// Too large, a type not allowed...: shows the form again with the reason.
|
// Too large, a type not allowed...: shows the form again with the reason.
|
||||||
Err(err) if err.is_client_error() => return show_form(request, err.message()).await,
|
Err(err) if err.is_client_error() => return show_form(request, err.message()).await,
|
||||||
Err(_) => return Err(ErrorPage::InternalError(Some(request))),
|
Err(_) => return Err(ErrorPage::InternalError(request)),
|
||||||
};
|
};
|
||||||
let Some(photo) = sent.file("photo") else {
|
let Some(photo) = sent.file("photo") else {
|
||||||
return show_form(request, Lc::n("Choose a photo to upload.")).await;
|
return show_form(request, Lc::n("Choose a photo to upload.")).await;
|
||||||
|
|
@ -133,15 +133,15 @@ async fn photo(
|
||||||
) -> Result<Response, ErrorPage> {
|
) -> Result<Response, ErrorPage> {
|
||||||
// Check the permissions here, as in any other handler...
|
// Check the permissions here, as in any other handler...
|
||||||
let Some((path, original_name)) = find_photo(index) else {
|
let Some((path, original_name)) = find_photo(index) else {
|
||||||
return Err(ErrorPage::NotFound(Some(request)));
|
return Err(ErrorPage::NotFound(request));
|
||||||
};
|
};
|
||||||
web::Download::new(path)
|
web::Download::new(path)
|
||||||
.with_file_name(original_name)
|
.with_file_name(original_name)
|
||||||
.serve(&request)
|
.serve(&request)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| match err {
|
.map_err(|err| match err {
|
||||||
web::UploadError::NotFound(_) => ErrorPage::NotFound(Some(request)),
|
web::UploadError::NotFound(_) => ErrorPage::NotFound(request),
|
||||||
_ => ErrorPage::InternalError(Some(request)),
|
_ => ErrorPage::InternalError(request),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
@ -301,7 +301,7 @@ async fn upload(request: HttpRequest, multipart: web::Multipart) -> Result<Respo
|
||||||
let sent = match photo_upload().receive::<PhotoData>(multipart).await {
|
let sent = match photo_upload().receive::<PhotoData>(multipart).await {
|
||||||
Ok(sent) => sent,
|
Ok(sent) => sent,
|
||||||
Err(err) if err.is_client_error() => return show_form(request, err.message()).await,
|
Err(err) if err.is_client_error() => return show_form(request, err.message()).await,
|
||||||
Err(_) => return Err(ErrorPage::InternalError(Some(request))),
|
Err(_) => return Err(ErrorPage::InternalError(request)),
|
||||||
};
|
};
|
||||||
let Some(file) = sent.file("photo") else {
|
let Some(file) = sent.file("photo") else {
|
||||||
return show_form(request, Lc::n("Choose a photo to upload.")).await;
|
return show_form(request, Lc::n("Choose a photo to upload.")).await;
|
||||||
|
|
@ -329,15 +329,15 @@ async fn photo(
|
||||||
.get(index)
|
.get(index)
|
||||||
.map(|photo| (photo.path.clone(), photo.original_name.clone()));
|
.map(|photo| (photo.path.clone(), photo.original_name.clone()));
|
||||||
let Some((path, original_name)) = found else {
|
let Some((path, original_name)) = found else {
|
||||||
return Err(ErrorPage::NotFound(Some(request)));
|
return Err(ErrorPage::NotFound(request));
|
||||||
};
|
};
|
||||||
web::Download::new(path)
|
web::Download::new(path)
|
||||||
.with_file_name(original_name)
|
.with_file_name(original_name)
|
||||||
.serve(&request)
|
.serve(&request)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| match err {
|
.map_err(|err| match err {
|
||||||
web::UploadError::NotFound(_) => ErrorPage::NotFound(Some(request)),
|
web::UploadError::NotFound(_) => ErrorPage::NotFound(request),
|
||||||
_ => ErrorPage::InternalError(Some(request)),
|
_ => ErrorPage::InternalError(request),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -400,7 +400,7 @@ otro lugar en `config/test.toml` (por ejemplo, `"target/test-uploads"`):
|
||||||
# let sent = match photo_upload().receive::<PhotoData>(multipart).await {
|
# let sent = match photo_upload().receive::<PhotoData>(multipart).await {
|
||||||
# Ok(sent) => sent,
|
# Ok(sent) => sent,
|
||||||
# Err(err) if err.is_client_error() => return show_form(request, err.message()).await,
|
# Err(err) if err.is_client_error() => return show_form(request, err.message()).await,
|
||||||
# Err(_) => return Err(ErrorPage::InternalError(Some(request))),
|
# Err(_) => return Err(ErrorPage::InternalError(request)),
|
||||||
# };
|
# };
|
||||||
# let Some(file) = sent.file("photo") else {
|
# let Some(file) = sent.file("photo") else {
|
||||||
# return show_form(request, Lc::n("Choose a photo to upload.")).await;
|
# return show_form(request, Lc::n("Choose a photo to upload.")).await;
|
||||||
|
|
@ -421,13 +421,13 @@ otro lugar en `config/test.toml` (por ejemplo, `"target/test-uploads"`):
|
||||||
# .get(index)
|
# .get(index)
|
||||||
# .map(|photo| (photo.path.clone(), photo.original_name.clone()));
|
# .map(|photo| (photo.path.clone(), photo.original_name.clone()));
|
||||||
# let Some((path, original_name)) = found else {
|
# let Some((path, original_name)) = found else {
|
||||||
# return Err(ErrorPage::NotFound(Some(request)));
|
# return Err(ErrorPage::NotFound(request));
|
||||||
# };
|
# };
|
||||||
# web::Download::new(path)
|
# web::Download::new(path)
|
||||||
# .with_file_name(original_name)
|
# .with_file_name(original_name)
|
||||||
# .serve(&request)
|
# .serve(&request)
|
||||||
# .await
|
# .await
|
||||||
# .map_err(|_| ErrorPage::NotFound(Some(request)))
|
# .map_err(|_| ErrorPage::NotFound(request))
|
||||||
# }
|
# }
|
||||||
// A submission with a title and one file, as a browser sends it.
|
// A submission with a title and one file, as a browser sends it.
|
||||||
fn photo_body(file_name: &str, content: &str) -> String {
|
fn photo_body(file_name: &str, content: &str) -> String {
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ Lo esencial de PageTop en una página. Cada línea enlaza con la guía que lo ex
|
||||||
| [Un formulario](../how-to/forms.md#3-recibe-y-valida-los-datos) | `web::Form(data): web::Form<T>` (el último parámetro) |
|
| [Un formulario](../how-to/forms.md#3-recibe-y-valida-los-datos) | `web::Form(data): web::Form<T>` (el último parámetro) |
|
||||||
| [Un formulario con archivos](../how-to/uploads.md#3-recibe-el-envío) | `upload.receive::<T>(multipart).await` con `multipart: web::Multipart` (el último parámetro) |
|
| [Un formulario con archivos](../how-to/uploads.md#3-recibe-el-envío) | `upload.receive::<T>(multipart).await` con `multipart: web::Multipart` (el último parámetro) |
|
||||||
| [Servir un archivo subido](../how-to/uploads.md#4-sirve-el-archivo-con-permisos) | `web::Download::new(path).with_file_name(name).serve(&request).await` |
|
| [Servir un archivo subido](../how-to/uploads.md#4-sirve-el-archivo-con-permisos) | `web::Download::new(path).with_file_name(name).serve(&request).await` |
|
||||||
| [Un error 404](../how-to/error-pages.md) | `Err(ErrorPage::NotFound(Some(request)))` |
|
| [Un error 404](../how-to/error-pages.md) | `Err(ErrorPage::NotFound(request))` |
|
||||||
| [Redirigir](../how-to/routes.md#4-elige-la-respuesta) | `Redirect::see_other("/target")` |
|
| [Redirigir](../how-to/routes.md#4-elige-la-respuesta) | `Redirect::see_other("/target")` |
|
||||||
| [Responder JSON](../how-to/routes.md#4-elige-la-respuesta) | `Json(value)` |
|
| [Responder JSON](../how-to/routes.md#4-elige-la-respuesta) | `Json(value)` |
|
||||||
| [Un error de API en JSON](../how-to/error-pages.md#7-errores-de-una-api-json) | `impl IntoResponse for ApiError` |
|
| [Un error de API en JSON](../how-to/error-pages.md#7-errores-de-una-api-json) | `impl IntoResponse for ApiError` |
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ theme = "Bootsier"
|
||||||
language = "es-ES"
|
language = "es-ES"
|
||||||
lang_negotiation = "Full"
|
lang_negotiation = "Full"
|
||||||
timezone = "Europe/Madrid"
|
timezone = "Europe/Madrid"
|
||||||
startup_banner = "Slant"
|
startup_banner = true
|
||||||
```
|
```
|
||||||
|
|
||||||
| Clave | Tipo | Por defecto | Notas |
|
| Clave | Tipo | Por defecto | Notas |
|
||||||
|
|
@ -73,7 +73,7 @@ startup_banner = "Slant"
|
||||||
| `timezone_order` | `Nearest`, `Alphabetical`, `Listed` | `"Nearest"` | Orden de esas regiones en el selector |
|
| `timezone_order` | `Nearest`, `Alphabetical`, `Listed` | `"Nearest"` | Orden de esas regiones en el selector |
|
||||||
| `timezone_per_user` | Booleano | `true` | Si cada usuario puede tener su propia zona horaria |
|
| `timezone_per_user` | Booleano | `true` | Si cada usuario puede tener su propia zona horaria |
|
||||||
| `admin_path` | Texto | `"/admin"` | Raíz del panel de administración: empieza por `/`, sin `/` final; una no válida impide arrancar |
|
| `admin_path` | Texto | `"/admin"` | Raíz del panel de administración: empieza por `/`, sin `/` final; una no válida impide arrancar |
|
||||||
| `startup_banner` | `Off`, `Slant`, `Small`, `Speed`, `Starwars` | `"Slant"` | Banner al arrancar |
|
| `startup_banner` | Booleano | `true` | Si se muestra un banner con el nombre de la aplicación al arrancar |
|
||||||
|
|
||||||
`lang_negotiation`:
|
`lang_negotiation`:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -358,7 +358,7 @@ async fn load_list() -> Result<(Vec<task::Model>, u64), DbErr> {
|
||||||
|
|
||||||
async fn home(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
async fn home(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
||||||
let Ok((tasks, pending)) = load_list().await else {
|
let Ok((tasks, pending)) = load_list().await else {
|
||||||
return Err(ErrorPage::InternalError(Some(request)));
|
return Err(ErrorPage::InternalError(request));
|
||||||
};
|
};
|
||||||
Page::new(request)
|
Page::new(request)
|
||||||
.with_title(Lc::n("Garden tasks"))
|
.with_title(Lc::n("Garden tasks"))
|
||||||
|
|
@ -561,7 +561,7 @@ así que la nueva es la tercera.
|
||||||
# }
|
# }
|
||||||
# async fn home(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
# async fn home(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
||||||
# let Ok((tasks, pending)) = load_list().await else {
|
# let Ok((tasks, pending)) = load_list().await else {
|
||||||
# return Err(ErrorPage::InternalError(Some(request)));
|
# return Err(ErrorPage::InternalError(request));
|
||||||
# };
|
# };
|
||||||
# Page::new(request)
|
# Page::new(request)
|
||||||
# .with_child(Html::with(move |_| html! {
|
# .with_child(Html::with(move |_| html! {
|
||||||
|
|
@ -872,7 +872,7 @@ async fn load_list() -> Result<(Vec<task::Model>, u64), DbErr> {
|
||||||
|
|
||||||
async fn home(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
async fn home(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
||||||
let Ok((tasks, pending)) = load_list().await else {
|
let Ok((tasks, pending)) = load_list().await else {
|
||||||
return Err(ErrorPage::InternalError(Some(request)));
|
return Err(ErrorPage::InternalError(request));
|
||||||
};
|
};
|
||||||
Page::new(request)
|
Page::new(request)
|
||||||
.with_title(Lc::n("Garden tasks"))
|
.with_title(Lc::n("Garden tasks"))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue