make server crate example more compelling

This commit is contained in:
Evan Almloff committed 2023-04-03 12:45:01 -05:00
1 parent b8ca41bd92
commit 10a1c46a49
1 file changed
+18 -22
+18 -22
View File
@@ -6,6 +6,7 @@
//! - Instant RSX Hot reloading with [`dioxus-hot-reload`](https://crates.io/crates/dioxus-hot-reload). //! - Instant RSX Hot reloading with [`dioxus-hot-reload`](https://crates.io/crates/dioxus-hot-reload).
//! //!
//! # Example //! # Example
//! Full stack Dioxus in under 50 lines of code
//! ```rust //! ```rust
//! #![allow(non_snake_case)] //! #![allow(non_snake_case)]
//! use dioxus::prelude::*; //! use dioxus::prelude::*;
@@ -13,50 +14,45 @@
//! //!
//! fn main() { //! fn main() {
//! #[cfg(feature = "web")] //! #[cfg(feature = "web")]
//! // Hydrate the application on the client
//! dioxus_web::launch_cfg(app, dioxus_web::Config::new().hydrate(true)); //! dioxus_web::launch_cfg(app, dioxus_web::Config::new().hydrate(true));
//! #[cfg(feature = "ssr")] //! #[cfg(feature = "ssr")]
//! { //! {
//! GetServerData::register().unwrap(); //! GetMeaning::register().unwrap();
//! tokio::runtime::Runtime::new() //! tokio::runtime::Runtime::new()
//! .unwrap() //! .unwrap()
//! .block_on(async move { //! .block_on(async move {
//! let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080)); //! warp::serve(serve_dioxus_application(
//! axum::Server::bind(&addr) //! "",
//! .serve( //! ServeConfigBuilder::new(app, ()),
//! axum::Router::new() //! ))
//! // Server side render the application, serve static assets, and register server functions //! .run(([127, 0, 0, 1], 8080))
//! .serve_dioxus_application("", ServeConfigBuilder::new(app, ())) //! .await;
//! .into_make_service(),
//! )
//! .await
//! .unwrap();
//! }); //! });
//! } //! }
//! } //! }
//! //!
//! fn app(cx: Scope) -> Element { //! fn app(cx: Scope) -> Element {
//! let text = use_state(cx, || "...".to_string()); //! let meaning = use_state(cx, || None);
//!
//! cx.render(rsx! { //! cx.render(rsx! {
//! button { //! button {
//! onclick: move |_| { //! onclick: move |_| {
//! to_owned![text]; //! to_owned![meaning];
//! async move { //! async move {
//! if let Ok(data) = get_server_data().await { //! if let Ok(data) = get_meaning("life the universe and everything".into()).await {
//! text.set(data.clone()); //! meaning.set(data);
//! } //! }
//! } //! }
//! }, //! },
//! "Run a server function" //! "Run a server function"
//! } //! }
//! "Server said: {text}" //! "Server said: {meaning:?}"
//! }) //! })
//! } //! }
//! //!
//! #[server(GetServerData)] //! // This code will only run on the server
//! async fn get_server_data() -> Result<String, ServerFnError> { //! #[server(GetMeaning)]
//! Ok("Hello from the server!".to_string()) //! async fn get_meaning(of: String) -> Result<Option<u32>, ServerFnError> {
//! Ok(of.contains("life").then(|| 42))
//! } //! }
//! ``` //! ```