1
0
Fork 0
This commit is contained in:
Florian RICHER (MrDev023) 2022-03-21 19:35:53 +01:00
commit 4b7cb4ad1e
9 changed files with 1756 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

1685
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

10
Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "actix-example"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"
yew = { git = "https://github.com/yewstack/yew/", features = ["ssr"] }

View file

@ -0,0 +1,14 @@
use actix_web::{get, Responder, web, HttpResponse};
use yew::ServerRenderer;
use crate::front::components::hello::{Hello, HelloProps};
#[get("/{name}")]
async fn index(name: web::Path<String>) -> impl Responder {
let name = name.into_inner();
let renderer = ServerRenderer::<Hello>::with_props(HelloProps {
name
});
HttpResponse::Ok().body(renderer.render().await)
}

1
src/controllers/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod hello_controller;

View file

@ -0,0 +1,29 @@
use yew::prelude::*;
#[derive(Properties, PartialEq)]
pub struct HelloProps {
pub name: String,
}
#[function_component]
pub fn Hello(props: &HelloProps) -> Html {
let counter = use_state(|| 0);
let onclick = {
let counter = counter.clone();
Callback::from(move |_| counter.set(*counter + 1))
};
html! {
<div>
<button {onclick}>{ "Increment value" }</button>
<p>
<b>{ "Current value: " }</b>
{ *counter }
</p>
<p>
{ props.name.clone() }
</p>
</div>
}
}

View file

@ -0,0 +1 @@
pub mod hello;

1
src/front/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod components;

14
src/main.rs Normal file
View file

@ -0,0 +1,14 @@
pub(self) mod controllers;
pub(self) mod front;
use actix_web::{App, HttpServer, web};
#[actix_web::main]
async fn main() -> std::io::Result<()>{
HttpServer::new(|| {
App::new().service(web::scope("/hello").service(controllers::hello_controller::index))
})
.bind(("127.0.0.1", 8000))?
.run()
.await
}