serve.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use std::fs::File;
  2. use std::io::Read;
  3. use std::path::PathBuf;
  4. use dioxus_core::Component;
  5. #[derive(Clone)]
  6. pub struct ServeConfigBuilder<P: Clone> {
  7. pub(crate) app: Component<P>,
  8. pub(crate) props: P,
  9. pub(crate) root_id: Option<&'static str>,
  10. pub(crate) index_path: Option<&'static str>,
  11. pub(crate) assets_path: Option<&'static str>,
  12. }
  13. impl<P: Clone> ServeConfigBuilder<P> {
  14. /// Create a new ServeConfig
  15. pub fn new(app: Component<P>, props: P) -> Self {
  16. Self {
  17. app,
  18. props,
  19. root_id: None,
  20. index_path: None,
  21. assets_path: None,
  22. }
  23. }
  24. /// Set the path of the index.html file to be served. (defaults to {assets_path}/index.html)
  25. pub fn index_path(mut self, index_path: &'static str) -> Self {
  26. self.index_path = Some(index_path);
  27. self
  28. }
  29. /// Set the id of the root element in the index.html file to place the prerendered content into. (defaults to main)
  30. pub fn root_id(mut self, root_id: &'static str) -> Self {
  31. self.root_id = Some(root_id);
  32. self
  33. }
  34. /// Set the path of the assets folder generated by the Dioxus CLI. (defaults to dist)
  35. pub fn assets_path(mut self, assets_path: &'static str) -> Self {
  36. self.assets_path = Some(assets_path);
  37. self
  38. }
  39. /// Build the ServeConfig
  40. pub fn build(self) -> ServeConfig<P> {
  41. let assets_path = self.assets_path.unwrap_or("dist");
  42. let index_path = self
  43. .index_path
  44. .map(PathBuf::from)
  45. .unwrap_or_else(|| format!("{assets_path}/index.html").into());
  46. let root_id = self.root_id.unwrap_or("main");
  47. let index = load_index_html(index_path, root_id);
  48. ServeConfig {
  49. app: self.app,
  50. props: self.props,
  51. index,
  52. assets_path,
  53. }
  54. }
  55. }
  56. fn load_index_html(path: PathBuf, root_id: &'static str) -> IndexHtml {
  57. let mut file = File::open(path).expect("Failed to find index.html. Make sure the index_path is set correctly and the WASM application has been built.");
  58. let mut contents = String::new();
  59. file.read_to_string(&mut contents)
  60. .expect("Failed to read index.html");
  61. let (pre_main, post_main) = contents.split_once(&format!("id=\"{root_id}\"")).unwrap_or_else(|| panic!("Failed to find id=\"{root_id}\" in index.html. The id is used to inject the application into the page."));
  62. let post_main = post_main.split_once('>').unwrap_or_else(|| {
  63. panic!("Failed to find closing > after id=\"{root_id}\" in index.html.")
  64. });
  65. let (pre_main, post_main) = (
  66. pre_main.to_string() + &format!("id=\"{root_id}\"") + post_main.0 + ">",
  67. post_main.1.to_string(),
  68. );
  69. IndexHtml {
  70. pre_main,
  71. post_main,
  72. }
  73. }
  74. #[derive(Clone)]
  75. pub(crate) struct IndexHtml {
  76. pub(crate) pre_main: String,
  77. pub(crate) post_main: String,
  78. }
  79. #[derive(Clone)]
  80. pub struct ServeConfig<P: Clone> {
  81. pub(crate) app: Component<P>,
  82. pub(crate) props: P,
  83. pub(crate) index: IndexHtml,
  84. pub(crate) assets_path: &'static str,
  85. }
  86. impl<P: Clone> From<ServeConfigBuilder<P>> for ServeConfig<P> {
  87. fn from(builder: ServeConfigBuilder<P>) -> Self {
  88. builder.build()
  89. }
  90. }