tide_ssr.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //! This demo shows how to use the to_string utility on VNodes to convert them into valid HTML.
  2. //! You can use the html! macro to craft webpages generated by the server on-the-fly.
  3. //!
  4. //! Server-side-renderered webpages are a great use of Rust's async story, where servers can handle
  5. //! thousands of simultaneous clients on minimal hardware.
  6. use dioxus::prelude::*;
  7. use tide::{Request, Response};
  8. #[async_std::main]
  9. async fn main() -> Result<(), std::io::Error> {
  10. dioxus_examples::logger::set_up_logging("tide_ssr");
  11. // Build the API
  12. let mut app = tide::new();
  13. app.at("/fib/:n").get(fibsum);
  14. // Launch the server
  15. let addr = "127.0.0.1:8080";
  16. log::info!("App is ready at {}", addr);
  17. log::info!("Navigate to an fibonacci number, like so http://localhost:8080/fib/21");
  18. app.listen(addr).await?;
  19. Ok(())
  20. }
  21. /// Calculate the fibonacci number for a given request input
  22. async fn fibsum(req: Request<()>) -> tide::Result<tide::Response> {
  23. use std::time::Instant;
  24. let n: usize = req.param("n")?.parse().unwrap_or(0);
  25. fn fib(n: usize) -> usize {
  26. if n == 0 || n == 1 {
  27. n
  28. } else {
  29. fib(n - 1) + fib(n - 2)
  30. }
  31. }
  32. // Start a stopwatch
  33. // Compute the nth number in the fibonacci sequence
  34. // Stop the stopwatch
  35. let start = Instant::now();
  36. let fib_n = fib(n);
  37. let duration = start.elapsed().as_nanos();
  38. let mut rng = rand::thread_rng();
  39. use rand::Rng;
  40. let other_fib_to_try = rng.gen_range(1..42);
  41. let nodes = html! {
  42. <html>
  43. <head>
  44. <meta content="text/html;charset=utf-8" />
  45. <meta charset="UTF-8" />
  46. <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
  47. </head>
  48. <body>
  49. <div class="flex items-center justify-center flex-col">
  50. <div class="flex items-center justify-center">
  51. <div class="flex flex-col bg-white rounded p-4 w-full max-w-xs">
  52. // Title
  53. <div class="font-bold text-xl">
  54. {format!("Fibonacci Calculator: n = {}",n)}
  55. </div>
  56. // Subtext / description
  57. <div class="text-sm text-gray-500">
  58. {format!("Calculated in {} nanoseconds",duration)}
  59. </div>
  60. <div class="flex flex-row items-center justify-center mt-6">
  61. // Main number
  62. <div class="font-medium text-6xl">
  63. {format!("{}",fib_n)}
  64. </div>
  65. </div>
  66. <div class="flex flex-row justify-between mt-6">
  67. <a href=format!("http://localhost:8080/fib/{}", other_fib_to_try) class="underline">
  68. {"Click to try another number"}
  69. </a>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. </body>
  75. </html>
  76. };
  77. Ok(Response::builder(203)
  78. .body(nodes.to_string())
  79. .header("custom-header", "value")
  80. .content_type(tide::http::mime::HTML)
  81. .build())
  82. }