Skip to content

Commit 89fadfb

Browse files
thehanrivergituser12981u2
authored andcommitted
chore: Initialize scaffolding for dioxus cross platform
1 parent a9b147b commit 89fadfb

42 files changed

Lines changed: 810 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/app/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target
4+
.DS_Store
5+
6+
# These are backup files generated by rustfmt
7+
**/*.rs.bk

src/app/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[workspace]
2+
resolver = "2"
3+
members = [
4+
"ui",
5+
"web",
6+
"desktop",
7+
"mobile",
8+
"api",
9+
]
10+
11+
[workspace.dependencies]
12+
dioxus = { version = "0.6.0" }
13+
14+
# workspace
15+
ui = { path = "ui" }
16+
api = { path = "api" }

src/app/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Development
2+
3+
Your new workspace contains a member crate for each of the web, desktop and mobile platforms, a `ui` crate for shared components and a `api` crate for shared backend logic:
4+
5+
```
6+
your_project/
7+
├─ web/
8+
│ ├─ ... # Web specific UI/logic
9+
├─ desktop/
10+
│ ├─ ... # Desktop specific UI/logic
11+
├─ mobile/
12+
│ ├─ ... # Mobile specific UI/logic
13+
├─ api/
14+
│ ├─ ... # All shared server logic
15+
├─ ui/
16+
│ ├─ ... # Component shared between multiple platforms
17+
```
18+
19+
## Platform crates
20+
21+
Each platform crate contains the entry point for the platform, and any assets, components and dependencies that are specific to that platform. For example, the desktop crate in the workspace looks something like this:
22+
23+
```
24+
desktop/ # The desktop crate contains all platform specific UI, logic and dependencies for the desktop app
25+
├─ assets/ # Assets used by the desktop app - Any platform specific assets should go in this folder
26+
├─ src/
27+
│ ├─ main.rs # The entrypoint for the desktop app. It also defines the routes for the desktop platform
28+
│ ├─ views/ # The views each route will render in the desktop version of the app
29+
│ │ ├─ mod.rs # Defines the module for the views route and re-exports the components for each route
30+
│ │ ├─ blog.rs # The component that will render at the /blog/:id route
31+
│ │ ├─ home.rs # The component that will render at the / route
32+
├─ Cargo.toml # The desktop crate's Cargo.toml - This should include all desktop specific dependencies
33+
```
34+
35+
When you start developing with the workspace setup each of the platform crates will look almost identical. The UI starts out exactly the same on all platforms. However, as you continue developing your application, this setup makes it easy to let the views for each platform change independently.
36+
37+
## Shared UI crate
38+
39+
The workspace contains a `ui` crate with components that are shared between multiple platforms. You should put any UI elements you want to use in multiple platforms in this crate. You can also put some shared client side logic in this crate, but be careful to not pull in platform specific dependencies. The `ui` crate starts out something like this:
40+
41+
```
42+
ui/
43+
├─ src/
44+
│ ├─ lib.rs # The entrypoint for the ui crate
45+
│ ├─ hero.rs # The Hero component that will be used in every platform
46+
│ ├─ echo.rs # The shared echo component that communicates with the server
47+
│ ├─ navbar.rs # The Navbar component that will be used in the layout of every platform's router
48+
```
49+
50+
## Shared backend logic
51+
52+
The workspace contains a `api` crate with shared backend logic. This crate defines all of the shared server functions for all platforms. Server functions are async functions that expose a public API on the server. They can be called like a normal async function from the client. When you run `dx serve`, all of the server functions will be collected in the server build and hosted on a public API for the client to call. The `api` crate starts out something like this:
53+
54+
```
55+
api/
56+
├─ src/
57+
│ ├─ lib.rs # Exports a server function that echos the input string
58+
```
59+
60+
### Serving Your App
61+
62+
Navigate to the platform crate of your choice:
63+
```bash
64+
cd web
65+
```
66+
67+
and serve:
68+
69+
```bash
70+
dx serve
71+
```
72+

src/app/api/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "api"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
dioxus = { workspace = true, features = ["fullstack"] }

src/app/api/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# API
2+
3+
This crate contains all shared fullstack server functions. This is a great place to place any server-only logic you would like to expose in multiple platforms like a method that accesses your database or a method that sends an email.
4+
5+
This crate will be built twice:
6+
1. Once for the server build with the `dioxus/server` feature enabled
7+
2. Once for the client build with the client feature disabled
8+
9+
During the server build, the server functions will be collected and hosted on a public API for the client to call. During the client build, the server functions will be compiled into the client build.
10+
11+
## Dependencies
12+
13+
Most server dependencies (like sqlx and tokio) will not compile on client platforms like WASM. To avoid building server dependencies on the client, you should add platform specific dependencies under the `server` feature in the [Cargo.toml](../Cargo.toml) file. More details about managing server only dependencies can be found in the [Dioxus guide](https://dioxuslabs.com/learn/0.6/guides/fullstack/managing_dependencies#adding-server-only-dependencies).

src/app/api/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! This crate contains all shared fullstack server functions.
2+
use dioxus::prelude::*;
3+
4+
/// Echo the user input on the server.
5+
#[server(Echo)]
6+
pub async fn echo(input: String) -> Result<String, ServerFnError> {
7+
Ok(input)
8+
}

src/app/clippy.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
await-holding-invalid-types = [
2+
"generational_box::GenerationalRef",
3+
{ path = "generational_box::GenerationalRef", reason = "Reads should not be held over an await point. This will cause any writes to fail while the await is pending since the read borrow is still active." },
4+
"generational_box::GenerationalRefMut",
5+
{ path = "generational_box::GenerationalRefMut", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
6+
"dioxus_signals::Write",
7+
{ path = "dioxus_signals::Write", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
8+
]

src/app/desktop/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "desktop"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
dioxus = { workspace = true, features = ["router"] }
8+
ui = { workspace = true }
9+
10+
[features]
11+
default = []
12+
desktop = ["dioxus/desktop"]
13+
server = ["dioxus/server"]

src/app/desktop/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Development
2+
3+
The desktop crate defines the entrypoint for the desktop app along with any assets, components and dependencies that are specific to desktop builds. The desktop crate starts out something like this:
4+
5+
```
6+
desktop/
7+
├─ assets/ # Assets used by the desktop app - Any platform specific assets should go in this folder
8+
├─ src/
9+
│ ├─ main.rs # The entrypoint for the desktop app.It also defines the routes for the desktop platform
10+
│ ├─ views/ # The views each route will render in the desktop version of the app
11+
│ │ ├─ mod.rs # Defines the module for the views route and re-exports the components for each route
12+
│ │ ├─ blog.rs # The component that will render at the /blog/:id route
13+
│ │ ├─ home.rs # The component that will render at the / route
14+
├─ Cargo.toml # The desktop crate's Cargo.toml - This should include all desktop specific dependencies
15+
```
16+
17+
## Dependencies
18+
Since you have fullstack enabled, the desktop crate will be built two times:
19+
1. Once for the server build with the `server` feature enabled
20+
2. Once for the client build with the `desktop` feature enabled
21+
22+
You should make all desktop specific dependencies optional and only enabled in the `desktop` feature. This will ensure that the server builds don't pull in desktop specific dependencies which cuts down on build times significantly.
23+
24+
### Serving Your Desktop App
25+
26+
You can start your desktop app with the following command:
27+
28+
```bash
29+
dx serve
30+
```

src/app/desktop/assets/blog.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#blog {
2+
margin-top: 50px;
3+
}
4+
5+
#blog a {
6+
color: #ffffff;
7+
margin-top: 50px;
8+
}

0 commit comments

Comments
 (0)