Nuevo ejemplo "/hello/{name}"

This commit is contained in:
Manuel Cillero 2023-06-09 13:40:20 +02:00
parent 7ffea7fab6
commit 2f4184fe26
5 changed files with 46 additions and 5 deletions

View file

@ -1,4 +1,5 @@
[workspace]
members = [
"hello-world",
"hello-name",
]

View file

@ -0,0 +1,9 @@
[package]
name = "hello_name"
version = "0.0.0"
edition = "2021"
publish = false
[dependencies]
actix-web = "4"
pagetop = { version = "0.0", path = "../../pagetop" }

View file

@ -0,0 +1,31 @@
use pagetop::prelude::*;
define_handle!(APP_HELLO_NAME);
struct HelloName;
impl ModuleTrait for HelloName {
fn handle(&self) -> Handle {
APP_HELLO_NAME
}
fn configure_service(&self, cfg: &mut service::web::ServiceConfig) {
cfg.service(hello_name);
}
}
#[service::get("/hello/{name}")]
async fn hello_name(
request: service::HttpRequest,
path: service::web::Path<String>,
) -> ResultPage<Markup, FatalError> {
let name = path.into_inner();
Page::new(request)
.with_in("content", Html::with(html! { h1 { "Hello " (name) "!" } }))
.render()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
Application::prepare(&HelloName).unwrap().run()?.await
}

View file

@ -1,5 +1,5 @@
[package]
name = "example_hello_world"
name = "hello_world"
version = "0.0.0"
edition = "2021"
publish = false

View file

@ -9,14 +9,14 @@ impl ModuleTrait for HelloWorld {
APP_HELLO_WORLD
}
fn configure_service(&self, cfg: &mut server::web::ServiceConfig) {
cfg.route("/", server::web::get().to(hello_world));
fn configure_service(&self, cfg: &mut service::web::ServiceConfig) {
cfg.route("/", service::web::get().to(hello_world));
}
}
async fn hello_world(request: server::HttpRequest) -> ResultPage<Markup, FatalError> {
async fn hello_world(request: service::HttpRequest) -> ResultPage<Markup, FatalError> {
Page::new(request)
.with_in("content", L10n::html(html! { h1 { "Hello World!" } }))
.with_in("content", Html::with(html! { h1 { "Hello World!" } }))
.render()
}