Hello world working with Actix
This commit is contained in:
parent
141ed69449
commit
2c94fe0cab
10 changed files with 1222 additions and 346 deletions
1
project/.gitignore
vendored
Normal file
1
project/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
target/
|
1478
project/Cargo.lock
generated
1478
project/Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -7,4 +7,6 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
yew = "0.11.0"
|
||||
actix-web = "2.0.0"
|
||||
actix-rt = "1.0.0"
|
||||
serde = "1.0.104"
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
use yew::{html, Callback, ClickEvent, Component, ComponentLink, Html, ShouldRender};
|
||||
|
||||
pub struct App {
|
||||
clicked: bool,
|
||||
on_click: Callback<ClickEvent>,
|
||||
}
|
||||
|
||||
pub enum Msg {
|
||||
Click,
|
||||
}
|
||||
|
||||
impl Component for App {
|
||||
type Message = Msg;
|
||||
type Properties = ();
|
||||
|
||||
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||
App {
|
||||
clicked: false,
|
||||
on_click: link.callback(|_| Msg::Click),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||
match msg {
|
||||
Msg::Click => {
|
||||
self.clicked = true;
|
||||
true // Indicate that the component should re-render
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self) -> Html {
|
||||
let button_text = if self.clicked { "Clicked!" } else { "Click me!" };
|
||||
|
||||
html! {
|
||||
<button onclick=&self.on_click>{ button_text }</button>
|
||||
}
|
||||
}
|
||||
}
|
8
project/src/controllers.rs
Normal file
8
project/src/controllers.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
#[macro_use]
|
||||
use actix_web::{get, post, HttpResponse};
|
||||
|
||||
#[get("/")]
|
||||
pub async fn hello_world() -> HttpResponse {
|
||||
let message = "Hello World!";
|
||||
HttpResponse::Ok().json(message)
|
||||
}
|
|
@ -1,5 +1,12 @@
|
|||
mod app;
|
||||
mod controllers;
|
||||
|
||||
fn main() {
|
||||
yew::start_app::<app::App>();
|
||||
use crate::controllers::*;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(move || App::new().service(hello_world))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue