From b85a03c6fdcee7d689c10656ab0341bf600a8c92 Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Wed, 29 Mar 2023 12:43:39 +0000 Subject: [PATCH] Integration with Rocket almost done, TODO: Fix font file serving; Add conditional to spin up Dioxus dev server. --- Cargo.toml | 4 +-- README.md | 14 ++++++---- build.rs | 2 +- public/styles/index.css | 8 ++++++ public/{ => styles}/tailwind.min.css | 2 +- src/lib.rs | 13 +++++---- src/main.rs | 41 +++++++++++++++++++++++----- tailwind.config.js | 1 + 8 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 public/styles/index.css rename public/{ => styles}/tailwind.min.css (67%) diff --git a/Cargo.toml b/Cargo.toml index 52f4092..a0d5216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,11 +9,11 @@ edition = "2021" [dependencies] dioxus = "0.3.2" dioxus-web = "0.3.1" +dioxus-ssr = "0.3.0" log = "0.4.6" # WebAssembly Debug wasm-logger = "0.2.0" console_error_panic_hook = "0.1.7" -wasm-bindgen = "0.2.84" -dioxus-autofmt = "0.3.0" +rocket = "=0.5.0-rc.3" diff --git a/README.md b/README.md index a0db4a8..f951a0b 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,25 @@ # Dioxus - [letter.werefox.cafe](https://letter.werefox.cafe) -> A re-implementation of the Valentine's Day site I made, but using Rust's [Dioxus](https://dioxuslabs.com/) crate. +> A re-implementation of the Valentine's Day site I made, but using Rust's [Dioxus](https://dioxuslabs.com/) crate and [Rocket](https://rocket.rs/) crate. ## Usage -### Just build the assets +### Just build ``` -dioxus build +cargo build ``` ### Start a `dev-server` for the project: ``` -dioxus serve +cargo run ``` +## Ignore This Section + +> I need to set things up so you can run the Dioxus dev server wothout launching rocket. I'll figire that out later. + (or, I personally use the following since port `8080` is usually being used and I want hot reloading) ``` @@ -25,7 +29,7 @@ dioxus serve --hot-reload --port 8234 ### Package this project: ``` -dioxus build --release +cargo build --release ``` ## Project Structure diff --git a/build.rs b/build.rs index 3a61682..d23236b 100644 --- a/build.rs +++ b/build.rs @@ -8,7 +8,7 @@ fn main() { // "--minify", // ]); tailwind.args( - "tailwindcss -i src/index.css -c tailwind.config.js -o public/tailwind.min.css --minify" + "tailwindcss -i src/index.css -c tailwind.config.js -o public/styles/tailwind.min.css --minify" .split(" "), ); tailwind.env("NODE_ENV", "production"); diff --git a/public/styles/index.css b/public/styles/index.css new file mode 100644 index 0000000..0657d06 --- /dev/null +++ b/public/styles/index.css @@ -0,0 +1,8 @@ + +@tailwind base; +@tailwind components; +@tailwind utilities; +@font-face { + font-family: 'DejaVuSansMono'; + src: local('DejaVuSansMono'), url('/fonts/DejaVuSansMono.ttf'), format('ttf'); +} diff --git a/public/tailwind.min.css b/public/styles/tailwind.min.css similarity index 67% rename from public/tailwind.min.css rename to public/styles/tailwind.min.css index c2b4c50..462d1c0 100644 --- a/public/tailwind.min.css +++ b/public/styles/tailwind.min.css @@ -1 +1 @@ -/*! tailwindcss v3.2.7 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }.container{width:100%}@media (min-width:640px){.container{max-width:640px}}@media (min-width:768px){.container{max-width:768px}}@media (min-width:1024px){.container{max-width:1024px}}@media (min-width:1280px){.container{max-width:1280px}}@media (min-width:1536px){.container{max-width:1536px}}.relative{position:relative}.mx-4{margin-left:1rem;margin-right:1rem}.mx-auto{margin-left:auto;margin-right:auto}.flex{display:flex}.h-48{height:12rem}.min-h-screen{min-height:100vh}.w-48{width:12rem}.max-w-fit{max-width:-moz-fit-content;max-width:fit-content}@keyframes growshrink{0%{transform:scale(.5)}40%{transform:scale(1.2)}60%{transform:scale(1.2)}to{transform:scale(.5)}}.animate-growshrink{animation:growshrink 2s cubic-bezier(1,0,0,1) infinite}.flex-col{flex-direction:column}.justify-center{justify-content:center}.space-y-4>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(1rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1rem*var(--tw-space-y-reverse))}.border-4{border-width:4px}.border-alice-werefox-red-dark{--tw-border-opacity:1;border-color:rgb(128 0 8/var(--tw-border-opacity))}.bg-alice-werefox-grey-light{--tw-bg-opacity:1;background-color:rgb(204 204 204/var(--tw-bg-opacity))}.bg-alice-werefox-grey-lightest{--tw-bg-opacity:1;background-color:rgb(238 238 238/var(--tw-bg-opacity))}.p-4{padding:1rem}.py-4{padding-top:1rem;padding-bottom:1rem}.py-8{padding-top:2rem;padding-bottom:2rem}.font-nerd{font-family:DejaVuSansMono}.text-alice-werefox-red-dark{--tw-text-opacity:1;color:rgb(128 0 8/var(--tw-text-opacity))}@font-face{font-family:DejaVuSansMono;src:local("DejaVuSansMono"),url(/fonts/DejaVuSansMono.ttf),format("ttf")}@media (prefers-color-scheme:dark){.dark\:border-alice-werefox-red{--tw-border-opacity:1;border-color:rgb(201 52 57/var(--tw-border-opacity))}.dark\:bg-alice-werefox-grey{--tw-bg-opacity:1;background-color:rgb(36 36 36/var(--tw-bg-opacity))}.dark\:bg-alice-werefox-grey-dark{--tw-bg-opacity:1;background-color:rgb(18 18 18/var(--tw-bg-opacity))}.dark\:text-alice-werefox-red-light{--tw-text-opacity:1;color:rgb(224 133 135/var(--tw-text-opacity))}} \ No newline at end of file +/*! tailwindcss v3.2.7 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }.container{width:100%}@media (min-width:640px){.container{max-width:640px}}@media (min-width:768px){.container{max-width:768px}}@media (min-width:1024px){.container{max-width:1024px}}@media (min-width:1280px){.container{max-width:1280px}}@media (min-width:1536px){.container{max-width:1536px}}.relative{position:relative}.mx-4{margin-left:1rem;margin-right:1rem}.mx-auto{margin-left:auto;margin-right:auto}.flex{display:flex}.hidden{display:none}.h-48{height:12rem}.min-h-screen{min-height:100vh}.w-48{width:12rem}.max-w-fit{max-width:-moz-fit-content;max-width:fit-content}@keyframes growshrink{0%{transform:scale(.5)}40%{transform:scale(1.2)}60%{transform:scale(1.2)}to{transform:scale(.5)}}.animate-growshrink{animation:growshrink 2s cubic-bezier(1,0,0,1) infinite}.flex-col{flex-direction:column}.justify-center{justify-content:center}.space-y-4>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(1rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1rem*var(--tw-space-y-reverse))}.border-4{border-width:4px}.border-alice-werefox-red-dark{--tw-border-opacity:1;border-color:rgb(128 0 8/var(--tw-border-opacity))}.bg-alice-werefox-grey-light{--tw-bg-opacity:1;background-color:rgb(204 204 204/var(--tw-bg-opacity))}.bg-alice-werefox-grey-lightest{--tw-bg-opacity:1;background-color:rgb(238 238 238/var(--tw-bg-opacity))}.p-4{padding:1rem}.py-4{padding-top:1rem;padding-bottom:1rem}.py-8{padding-top:2rem;padding-bottom:2rem}.font-nerd{font-family:DejaVuSansMono}.text-alice-werefox-red-dark{--tw-text-opacity:1;color:rgb(128 0 8/var(--tw-text-opacity))}@font-face{font-family:DejaVuSansMono;src:local("DejaVuSansMono"),url(/fonts/DejaVuSansMono.ttf),format("ttf")}@media (prefers-color-scheme:dark){.dark\:border-alice-werefox-red{--tw-border-opacity:1;border-color:rgb(201 52 57/var(--tw-border-opacity))}.dark\:bg-alice-werefox-grey{--tw-bg-opacity:1;background-color:rgb(36 36 36/var(--tw-bg-opacity))}.dark\:bg-alice-werefox-grey-dark{--tw-bg-opacity:1;background-color:rgb(18 18 18/var(--tw-bg-opacity))}.dark\:text-alice-werefox-red-light{--tw-text-opacity:1;color:rgb(224 133 135/var(--tw-text-opacity))}} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index eb622c2..1809267 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,21 +1,22 @@ +#![allow(non_snake_case)] pub mod web_app { // import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types use dioxus::prelude::*; - pub fn app(cx: Scope) -> Element { - cx.render(rsx! ( + pub fn App(cx: Scope) -> Element { + cx.render(rsx!( div { class: "min-h-screen font-nerd bg-alice-werefox-grey-light dark:bg-alice-werefox-grey", div { class: "container space-y-4 mx-auto p-4", - add_heart {}, - make_poem {}, + AddHeart {}, + MakePoem {}, } } )) } - fn add_heart(cx: Scope) -> Element { + fn AddHeart(cx: Scope) -> Element { cx.render(rsx!( div { class: "flex justify-center py-8", @@ -34,7 +35,7 @@ pub mod web_app { )) } - fn make_poem(cx: Scope) -> Element { + fn MakePoem(cx: Scope) -> Element { let poem = include_str!("data/poem.txt").split("\n\n"); cx.render(rsx!( diff --git a/src/main.rs b/src/main.rs index 845b744..af9d7ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,38 @@ -#![allow(non_snake_case)] +#[macro_use] +extern crate rocket; -use rust_letter::web_app; +use rocket::fs::FileServer; +use rocket::http::ContentType; +use rocket::tokio::fs::File; +use std::path::Path; -fn main() { - // init debug tool for WebAssembly - wasm_logger::init(wasm_logger::Config::default()); - console_error_panic_hook::set_once(); +// #[get("/")] +// async fn fonts(font: &str) -> (ContentType, Option) { +// let filename = Path::new("public/fonts/").join(font); +// (ContentType::TTF, File::open(&filename).await.ok()) +// } - dioxus_web::launch(web_app::app); +#[get("/")] +async fn index() -> rocket::response::content::RawHtml { + let mut vdom = dioxus::core::VirtualDom::new(rust_letter::web_app::App); + let _ = vdom.rebuild(); + let mut output = dioxus_ssr::render(&vdom); + output = format!( + "{}", + output + ) + .to_string(); + rocket::response::content::RawHtml(output) +} + +#[rocket::main] +async fn main() -> Result<(), rocket::Error> { + let _result = rocket::build() + .mount("/images", FileServer::from("public/images")) + .mount("/styles", FileServer::from("public/styles")) + .mount("/fonts", FileServer::from("public/fonts")) + .mount("/", routes![index]) + .launch() + .await?; + Ok(()) } diff --git a/tailwind.config.js b/tailwind.config.js index c0bf36b..33be8a5 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -3,6 +3,7 @@ module.exports = { content: [ // Or if using `src` directory: "./src/**/*.{rs,html,js,ts,jsx,tsx,ttf}", + "./target/**/*.{rs,html,js,ts,jsx,tsx,ttf}", ], theme: { extend: {