Integration with Rocket almost done, TODO: Fix font file serving; Add conditional to spin up Dioxus dev server.

This commit is contained in:
Ada Werefox 2023-03-29 12:43:39 +00:00
parent ff68778e94
commit b85a03c6fd
8 changed files with 63 additions and 22 deletions

View File

@ -9,11 +9,11 @@ edition = "2021"
[dependencies] [dependencies]
dioxus = "0.3.2" dioxus = "0.3.2"
dioxus-web = "0.3.1" dioxus-web = "0.3.1"
dioxus-ssr = "0.3.0"
log = "0.4.6" log = "0.4.6"
# WebAssembly Debug # WebAssembly Debug
wasm-logger = "0.2.0" wasm-logger = "0.2.0"
console_error_panic_hook = "0.1.7" console_error_panic_hook = "0.1.7"
wasm-bindgen = "0.2.84" rocket = "=0.5.0-rc.3"
dioxus-autofmt = "0.3.0"

View File

@ -1,21 +1,25 @@
# Dioxus - [letter.werefox.cafe](https://letter.werefox.cafe) # 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 ## Usage
### Just build the assets ### Just build
``` ```
dioxus build cargo build
``` ```
### Start a `dev-server` for the project: ### 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) (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: ### Package this project:
``` ```
dioxus build --release cargo build --release
``` ```
## Project Structure ## Project Structure

View File

@ -8,7 +8,7 @@ fn main() {
// "--minify", // "--minify",
// ]); // ]);
tailwind.args( 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(" "), .split(" "),
); );
tailwind.env("NODE_ENV", "production"); tailwind.env("NODE_ENV", "production");

8
public/styles/index.css Normal file
View File

@ -0,0 +1,8 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@font-face {
font-family: 'DejaVuSansMono';
src: local('DejaVuSansMono'), url('/fonts/DejaVuSansMono.ttf'), format('ttf');
}

File diff suppressed because one or more lines are too long

View File

@ -1,21 +1,22 @@
#![allow(non_snake_case)]
pub mod web_app { pub mod web_app {
// import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types // import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types
use dioxus::prelude::*; use dioxus::prelude::*;
pub fn app(cx: Scope) -> Element { pub fn App(cx: Scope) -> Element {
cx.render(rsx!( cx.render(rsx!(
div { div {
class: "min-h-screen font-nerd bg-alice-werefox-grey-light dark:bg-alice-werefox-grey", class: "min-h-screen font-nerd bg-alice-werefox-grey-light dark:bg-alice-werefox-grey",
div { div {
class: "container space-y-4 mx-auto p-4", class: "container space-y-4 mx-auto p-4",
add_heart {}, AddHeart {},
make_poem {}, MakePoem {},
} }
} }
)) ))
} }
fn add_heart(cx: Scope) -> Element { fn AddHeart(cx: Scope) -> Element {
cx.render(rsx!( cx.render(rsx!(
div { div {
class: "flex justify-center py-8", 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"); let poem = include_str!("data/poem.txt").split("\n\n");
cx.render(rsx!( cx.render(rsx!(

View File

@ -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() { // #[get("/<font>")]
// init debug tool for WebAssembly // async fn fonts(font: &str) -> (ContentType, Option<File>) {
wasm_logger::init(wasm_logger::Config::default()); // let filename = Path::new("public/fonts/").join(font);
console_error_panic_hook::set_once(); // (ContentType::TTF, File::open(&filename).await.ok())
// }
dioxus_web::launch(web_app::app); #[get("/")]
async fn index() -> rocket::response::content::RawHtml<String> {
let mut vdom = dioxus::core::VirtualDom::new(rust_letter::web_app::App);
let _ = vdom.rebuild();
let mut output = dioxus_ssr::render(&vdom);
output = format!(
"<head><link href=/styles/tailwind.min.css rel=stylesheet /></head>{}",
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(())
} }

View File

@ -3,6 +3,7 @@ module.exports = {
content: [ content: [
// Or if using `src` directory: // Or if using `src` directory:
"./src/**/*.{rs,html,js,ts,jsx,tsx,ttf}", "./src/**/*.{rs,html,js,ts,jsx,tsx,ttf}",
"./target/**/*.{rs,html,js,ts,jsx,tsx,ttf}",
], ],
theme: { theme: {
extend: { extend: {