#How to use `extract` in server functions

1 messages · Page 1 of 1 (latest)

brazen coyote
#

Hi ! I'm discovering dioxus in fullstack. Look very interesting !
I'm trying to understand how to implement backend extract .

I'm trying to reproduce the axum-auth example (https://github.com/DioxusLabs/dioxus/tree/v0.5/packages/fullstack/examples/axum-auth).

I reproduced the axum-hello-world example here (so, without axum extract) : https://github.com/buxx/demo/blob/dioxus-test/src/main.rs. And I apply few changes inspired by axum-auth example:

diff --git a/Cargo.toml b/Cargo.toml
index de55bcd..f7671af 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,12 +7,14 @@ edition = "2021"
 
 [dependencies]
 dioxus = { version = "0.5.6", features = ["fullstack"] }
+dioxus-fullstack = { version = "0.5.6", features = ["server"] }
 serde = "1.0.159"
 simple_logger = "4.2.0"
 tracing-wasm = "0.2.1"
 tracing = "0.1.40"
 tracing-subscriber = "0.3.17"
 reqwest = "0.11.18"
+axum = "0.7.5"
 
 [features]
 default = []
diff --git a/src/main.rs b/src/main.rs
index 08a38cc..efc6541 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -41,6 +42,7 @@ async fn post_server_data(data: String) -> Result<(), ServerFnError> {
 
 #[server]
 async fn get_server_data() -> Result<String, ServerFnError> {
+    let method: axum::http::Method = extract().await?;
     Ok("Hello world".to_string())
 }

But, I got compilation error.

#
error[E0277]: the trait bound `Method: FromServerContext<_>` is not satisfied
   --> src/main.rs:45:38
    |
45  |     let method: axum::http::Method = extract().await?;
    |                                      ^^^^^^^^^ the trait `FromServerContext<_>` is not implemented for `Method`
    |
    = help: the trait `FromServerContext` is implemented for `fullstack::server_context::FromContext<T>`
note: required by a bound in `dioxus::prelude::extract`
   --> /home/bastiensevajol/.cargo/registry/src/index.crates.io-6f17d22bba15001f/dioxus-fullstack-0.5.6/src/server_context.rs:158:25
    |
158 | pub async fn extract<E: FromServerContext<I>, I>() -> Result<E, E::Rejection> {
    |                         ^^^^^^^^^^^^^^^^^^^^ required by this bound in `extract`

error[E0277]: the trait bound `Method: FromServerContext<_>` is not satisfied
  --> src/main.rs:45:48
   |
45 |     let method: axum::http::Method = extract().await?;
   |                                                ^^^^^ the trait `FromServerContext<_>` is not implemented for `Method`
   |
   = help: the trait `FromServerContext` is implemented for `fullstack::server_context::FromContext<T>`

I guess it need an import of this trait implementation, or feature usage configuration. But, by reading axum-auth example, I don't understand which one 🤔

upbeat river
#

You need to enable the "axum" feature on dioxus-fullstack or the "server" feature on dioxus

#

The "sever" feature in dioxus-fullstack just enables the server utilities without integrating with any specific backend framework

brazen coyote
#

Okay ! I will try this Thanks

#

(Dioxus is the revolution I wait for the web since 15 years !)

brazen coyote
#

There is no error now with cargo check command but dx serve have a lot. There is an extract:

| ⚙ Compiling registry+https://github.com/rust-lang/crates.io-index#[email protected]                                           error[E0432]: unresolved import `crate::sys::IoSourceState`
  --> /home/bastiensevajol/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-1.0.2/src/io_source.rs:14:5
   |
14 | use crate::sys::IoSourceState;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ no `IoSourceState` in `sys`

error[E0432]: unresolved import `crate::sys::tcp`
  --> /home/bastiensevajol/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-1.0.2/src/net/tcp/listener.rs:17:17
   |
17 | use crate::sys::tcp::{bind, listen, new_for_addr};
   |                 ^^^ could not find `tcp` in `sys`

error[E0432]: unresolved import `crate::sys::tcp`
  --> /home/bastiensevajol/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-1.0.2/src/net/tcp/stream.rs:15:17
   |
15 | use crate::sys::tcp::{connect, new_for_addr};
   |                 ^^^ could not find `tcp` in `sys`

error[E0433]: failed to resolve: could not find `Selector` in `sys`
   --> /home/bastiensevajol/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mio-1.0.2/src/poll.rs:321:18
    |
321 |             sys::Selector::new().map(|selector| Poll {
    |                  ^^^^^^^^ could not find `Selector` in `sys`
#

Will try to copy entire auth example Cargo deps

opal shell
#

IIRC it is because of tokio. It must be optional and you need to use it only for 'server' feature.

brazen coyote
#

Hi

#

I tried with this set of dependencies (tokio optional), "copied" from "auth" example :

[dependencies]
dioxus = { version = "0.5.6", features = ["fullstack", "router"] }
dioxus-fullstack = { version = "0.5.6", features = ["server", "axum"] }
axum = { version = "0.7.5", optional = true }
tokio = { version = "1.16.1", features = ["full"], optional = true }
tower-http = { version = "0.5.2", features = ["auth"], optional = true }
simple_logger = { version = "4.2.0", optional = true }
async-trait = { version = "0.1.71", optional = true }
sqlx = { version = "0.7.0", features = [
    "macros",
    "migrate",
    "postgres",
    "sqlite",
    "_unstable-all-types",
    "tls-native-tls",
    "runtime-tokio",
], optional = true }
http = { version = "1.0.0", optional = true }
tower = { version = "0.4.13", optional = true }
thiserror = "1.0.63"
bon = "2.1.1"
uuid = { version = "1.10.0", features = ["v4", "fast-rng"], optional = true }
redb = { version = "2.1.2", optional = true }
dioxus-logger = "0.5.1"

serde = "1.0.159"
execute = "0.2.12"
anyhow = "1.0.71"

[dependencies.axum_session]
version = "0.12.1"
features = ["sqlite-rustls"]
optional = true

[dependencies.axum_session_auth]
version = "0.12.1"
features = ["sqlite-rustls"]
optional = true

[features]
default = []
server = ["dioxus-fullstack/axum", "tokio", "dioxus-fullstack/axum", "tower-http", "simple_logger", "async-trait", "sqlx", "axum_session", "axum_session_auth", "http", "tower", "dep:uuid", "dep:redb", "dep:axum"]
web = ["dioxus/web"]

Same result 😦

It's very frustrating to not success to make a working minimal reproduction code of extractusage (which like to be a unique way to share state in backend) 😢

upbeat river
#

dioxus-fullstack = { version = "0.5.6", features = ["server", "axum"] }
dioxus fullstack can't be a dependency on wasm because it pulls in tokio

#

Try this:

[dependencies]
dioxus = { version = "0.5.6", features = ["fullstack", "router"] }
axum = { version = "0.7.5", optional = true }
tokio = { version = "1.16.1", features = ["full"], optional = true }
tower-http = { version = "0.5.2", features = ["auth"], optional = true }
simple_logger = { version = "4.2.0", optional = true }
async-trait = { version = "0.1.71", optional = true }
sqlx = { version = "0.7.0", features = [
    "macros",
    "migrate",
    "postgres",
    "sqlite",
    "_unstable-all-types",
    "tls-native-tls",
    "runtime-tokio",
], optional = true }
http = { version = "1.0.0", optional = true }
tower = { version = "0.4.13", optional = true }
thiserror = "1.0.63"
bon = "2.1.1"
uuid = { version = "1.10.0", features = ["v4", "fast-rng"], optional = true }
redb = { version = "2.1.2", optional = true }
dioxus-logger = "0.5.1"

serde = "1.0.159"
execute = "0.2.12"
anyhow = "1.0.71"

[dependencies.axum_session]
version = "0.12.1"
features = ["sqlite-rustls"]
optional = true

[dependencies.axum_session_auth]
version = "0.12.1"
features = ["sqlite-rustls"]
optional = true

[features]
default = []
server = ["dioxus/server", "tokio", "tower-http", "simple_logger", "async-trait", "sqlx", "axum_session", "axum_session_auth", "http", "tower", "dep:uuid", "dep:redb", "dep:axum"]
web = ["dioxus/web"]