From 2f4184fe26c1ca4cdbc1c78d606cee577c66a0e6 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Fri, 9 Jun 2023 13:40:20 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Nuevo=20ejemplo=20"/hello/{name}"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/Cargo.toml | 1 + examples/hello-name/Cargo.toml | 9 +++++++++ examples/hello-name/src/main.rs | 31 +++++++++++++++++++++++++++++++ examples/hello-world/Cargo.toml | 2 +- examples/hello-world/src/main.rs | 8 ++++---- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 examples/hello-name/Cargo.toml create mode 100644 examples/hello-name/src/main.rs diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 22a37520..3fbba518 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -1,4 +1,5 @@ [workspace] members = [ "hello-world", + "hello-name", ] diff --git a/examples/hello-name/Cargo.toml b/examples/hello-name/Cargo.toml new file mode 100644 index 00000000..a1076b25 --- /dev/null +++ b/examples/hello-name/Cargo.toml @@ -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" } diff --git a/examples/hello-name/src/main.rs b/examples/hello-name/src/main.rs new file mode 100644 index 00000000..4c9e148b --- /dev/null +++ b/examples/hello-name/src/main.rs @@ -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, +) -> ResultPage { + 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 +} diff --git a/examples/hello-world/Cargo.toml b/examples/hello-world/Cargo.toml index d35e5156..1543da0b 100644 --- a/examples/hello-world/Cargo.toml +++ b/examples/hello-world/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "example_hello_world" +name = "hello_world" version = "0.0.0" edition = "2021" publish = false diff --git a/examples/hello-world/src/main.rs b/examples/hello-world/src/main.rs index 36c70336..028b971b 100644 --- a/examples/hello-world/src/main.rs +++ b/examples/hello-world/src/main.rs @@ -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 { +async fn hello_world(request: service::HttpRequest) -> ResultPage { Page::new(request) - .with_in("content", L10n::html(html! { h1 { "Hello World!" } })) + .with_in("content", Html::with(html! { h1 { "Hello World!" } })) .render() }