tide_ssr.rs 3.4 KB

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