|
@@ -1,72 +1,160 @@
|
|
|
-use std::sync::{Arc, PoisonError, RwLock, RwLockWriteGuard};
|
|
|
+use dioxus_core::ScopeState;
|
|
|
|
|
|
-use anymap::{any::Any, Map};
|
|
|
+/// A trait for an object that contains a server context
|
|
|
+pub trait HasServerContext {
|
|
|
+ /// Get the server context from the state
|
|
|
+ fn server_context(&self) -> DioxusServerContext;
|
|
|
|
|
|
-type SendSyncAnyMap = Map<dyn Any + Send + Sync + 'static>;
|
|
|
+ /// A shortcut for `self.server_context()`
|
|
|
+ fn sc(&self) -> DioxusServerContext {
|
|
|
+ self.server_context()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl HasServerContext for &ScopeState {
|
|
|
+ fn server_context(&self) -> DioxusServerContext {
|
|
|
+ #[cfg(feature = "ssr")]
|
|
|
+ {
|
|
|
+ self.consume_context().expect("No server context found")
|
|
|
+ }
|
|
|
+ #[cfg(not(feature = "ssr"))]
|
|
|
+ {
|
|
|
+ DioxusServerContext {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-/// A shared context for server functions.
|
|
|
+/// A shared context for server functions that contains infomation about the request and middleware state.
|
|
|
/// This allows you to pass data between your server framework and the server functions. This can be used to pass request information or information about the state of the server. For example, you could pass authentication data though this context to your server functions.
|
|
|
+///
|
|
|
+/// You should not construct this directly inside components. Instead use the `HasServerContext` trait to get the server context from the scope.
|
|
|
#[derive(Clone)]
|
|
|
pub struct DioxusServerContext {
|
|
|
- shared_context: Arc<RwLock<SendSyncAnyMap>>,
|
|
|
+ #[cfg(feature = "ssr")]
|
|
|
+ shared_context: std::sync::Arc<
|
|
|
+ std::sync::RwLock<anymap::Map<dyn anymap::any::Any + Send + Sync + 'static>>,
|
|
|
+ >,
|
|
|
+ #[cfg(feature = "ssr")]
|
|
|
+ headers: std::sync::Arc<std::sync::RwLock<hyper::header::HeaderMap>>,
|
|
|
+ #[cfg(feature = "ssr")]
|
|
|
+ pub(crate) parts: std::sync::Arc<RequestParts>,
|
|
|
}
|
|
|
|
|
|
+#[allow(clippy::derivable_impls)]
|
|
|
impl Default for DioxusServerContext {
|
|
|
fn default() -> Self {
|
|
|
Self {
|
|
|
- shared_context: Arc::new(RwLock::new(SendSyncAnyMap::new())),
|
|
|
+ #[cfg(feature = "ssr")]
|
|
|
+ shared_context: std::sync::Arc::new(std::sync::RwLock::new(anymap::Map::new())),
|
|
|
+ #[cfg(feature = "ssr")]
|
|
|
+ headers: Default::default(),
|
|
|
+ #[cfg(feature = "ssr")]
|
|
|
+ parts: Default::default(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl DioxusServerContext {
|
|
|
- /// Clone a value from the shared server context
|
|
|
- pub fn get<T: Any + Send + Sync + Clone + 'static>(&self) -> Option<T> {
|
|
|
- self.shared_context.read().ok()?.get::<T>().cloned()
|
|
|
+#[cfg(feature = "ssr")]
|
|
|
+pub use server_fn_impl::*;
|
|
|
+
|
|
|
+#[cfg(feature = "ssr")]
|
|
|
+mod server_fn_impl {
|
|
|
+ use super::*;
|
|
|
+ use std::sync::LockResult;
|
|
|
+ use std::sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
|
|
+
|
|
|
+ use anymap::{any::Any, Map};
|
|
|
+ type SendSyncAnyMap = Map<dyn Any + Send + Sync + 'static>;
|
|
|
+
|
|
|
+ impl DioxusServerContext {
|
|
|
+ /// Create a new server context from a request
|
|
|
+ pub fn new(parts: impl Into<Arc<RequestParts>>) -> Self {
|
|
|
+ Self {
|
|
|
+ parts: parts.into(),
|
|
|
+ shared_context: Arc::new(RwLock::new(SendSyncAnyMap::new())),
|
|
|
+ headers: Default::default(),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Clone a value from the shared server context
|
|
|
+ pub fn get<T: Any + Send + Sync + Clone + 'static>(&self) -> Option<T> {
|
|
|
+ self.shared_context.read().ok()?.get::<T>().cloned()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Insert a value into the shared server context
|
|
|
+ pub fn insert<T: Any + Send + Sync + 'static>(
|
|
|
+ &mut self,
|
|
|
+ value: T,
|
|
|
+ ) -> Result<(), PoisonError<RwLockWriteGuard<'_, SendSyncAnyMap>>> {
|
|
|
+ self.shared_context
|
|
|
+ .write()
|
|
|
+ .map(|mut map| map.insert(value))
|
|
|
+ .map(|_| ())
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Get the headers from the server context
|
|
|
+ pub fn responce_headers(&self) -> RwLockReadGuard<'_, hyper::header::HeaderMap> {
|
|
|
+ self.try_responce_headers()
|
|
|
+ .expect("Failed to get headers from server context")
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Try to get the headers from the server context
|
|
|
+ pub fn try_responce_headers(
|
|
|
+ &self,
|
|
|
+ ) -> LockResult<RwLockReadGuard<'_, hyper::header::HeaderMap>> {
|
|
|
+ self.headers.read()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Get the headers mutably from the server context
|
|
|
+ pub fn responce_headers_mut(&self) -> RwLockWriteGuard<'_, hyper::header::HeaderMap> {
|
|
|
+ self.try_responce_headers_mut()
|
|
|
+ .expect("Failed to get headers mutably from server context")
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Try to get the headers mut from the server context
|
|
|
+ pub fn try_responce_headers_mut(
|
|
|
+ &self,
|
|
|
+ ) -> LockResult<RwLockWriteGuard<'_, hyper::header::HeaderMap>> {
|
|
|
+ self.headers.write()
|
|
|
+ }
|
|
|
+
|
|
|
+ pub(crate) fn take_responce_headers(&self) -> hyper::header::HeaderMap {
|
|
|
+ let mut headers = self.headers.write().unwrap();
|
|
|
+ std::mem::take(&mut *headers)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Get the request that triggered:
|
|
|
+ /// - The initial SSR render if called from a ScopeState or ServerFn
|
|
|
+ /// - The server function to be called if called from a server function after the initial render
|
|
|
+ pub fn request_parts(&self) -> &RequestParts {
|
|
|
+ &self.parts
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /// Insert a value into the shared server context
|
|
|
- pub fn insert<T: Any + Send + Sync + 'static>(
|
|
|
- &mut self,
|
|
|
- value: T,
|
|
|
- ) -> Result<(), PoisonError<RwLockWriteGuard<'_, SendSyncAnyMap>>> {
|
|
|
- self.shared_context
|
|
|
- .write()
|
|
|
- .map(|mut map| map.insert(value))
|
|
|
- .map(|_| ())
|
|
|
+ /// Associated parts of an HTTP Request
|
|
|
+ #[derive(Debug, Default)]
|
|
|
+ pub struct RequestParts {
|
|
|
+ /// The request's method
|
|
|
+ pub method: http::Method,
|
|
|
+ /// The request's URI
|
|
|
+ pub uri: http::Uri,
|
|
|
+ /// The request's version
|
|
|
+ pub version: http::Version,
|
|
|
+ /// The request's headers
|
|
|
+ pub headers: http::HeaderMap<http::HeaderValue>,
|
|
|
+ /// The request's extensions
|
|
|
+ pub extensions: http::Extensions,
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-/// Generate a server context from a tuple of values
|
|
|
-macro_rules! server_context {
|
|
|
- ($({$(($name:ident: $ty:ident)),*}),*) => {
|
|
|
- $(
|
|
|
- #[allow(unused_mut)]
|
|
|
- impl< $($ty: Send + Sync + 'static),* > From<($($ty,)*)> for $crate::server_context::DioxusServerContext {
|
|
|
- fn from(( $($name,)* ): ($($ty,)*)) -> Self {
|
|
|
- let mut context = $crate::server_context::DioxusServerContext::default();
|
|
|
- $(context.insert::<$ty>($name).unwrap();)*
|
|
|
- context
|
|
|
- }
|
|
|
+ impl From<http::request::Parts> for RequestParts {
|
|
|
+ fn from(parts: http::request::Parts) -> Self {
|
|
|
+ Self {
|
|
|
+ method: parts.method,
|
|
|
+ uri: parts.uri,
|
|
|
+ version: parts.version,
|
|
|
+ headers: parts.headers,
|
|
|
+ extensions: parts.extensions,
|
|
|
}
|
|
|
- )*
|
|
|
- };
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
-server_context!(
|
|
|
- {},
|
|
|
- {(a: A)},
|
|
|
- {(a: A), (b: B)},
|
|
|
- {(a: A), (b: B), (c: C)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F), (g: G)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F), (g: G), (h: H)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F), (g: G), (h: H), (i: I)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F), (g: G), (h: H), (i: I), (j: J)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F), (g: G), (h: H), (i: I), (j: J), (k: K)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F), (g: G), (h: H), (i: I), (j: J), (k: K), (l: L)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F), (g: G), (h: H), (i: I), (j: J), (k: K), (l: L), (m: M)},
|
|
|
- {(a: A), (b: B), (c: C), (d: D), (e: E), (f: F), (g: G), (h: H), (i: I), (j: J), (k: K), (l: L), (m: M), (n: N)}
|
|
|
-);
|