2023-07-15 00:23:12 +00:00
use crate ::html_storage ::HTMLData ;
2023-07-07 00:54:05 +00:00
pub use server_fn_impl ::* ;
use std ::sync ::Arc ;
use std ::sync ::RwLock ;
2023-04-01 22:00:12 +00:00
2023-07-08 01:25:45 +00:00
/// A shared context for server functions that contains information about the request and middleware state.
2023-04-03 13:09:22 +00:00
/// 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.
2023-04-29 22:04:54 +00:00
///
/// You should not construct this directly inside components. Instead use the `HasServerContext` trait to get the server context from the scope.
2023-04-01 22:00:12 +00:00
#[ derive(Clone) ]
pub struct DioxusServerContext {
2023-04-29 22:04:54 +00:00
shared_context : std ::sync ::Arc <
std ::sync ::RwLock < anymap ::Map < dyn anymap ::any ::Any + Send + Sync + 'static > > ,
> ,
2023-07-08 01:25:45 +00:00
response_parts : std ::sync ::Arc < std ::sync ::RwLock < http ::response ::Parts > > ,
2023-07-07 00:54:05 +00:00
pub ( crate ) parts : Arc < RwLock < http ::request ::Parts > > ,
2023-07-15 00:23:12 +00:00
html_data : Arc < RwLock < HTMLData > > ,
2023-04-01 22:00:12 +00:00
}
2023-04-29 22:04:54 +00:00
#[ allow(clippy::derivable_impls) ]
2023-04-01 22:00:12 +00:00
impl Default for DioxusServerContext {
fn default ( ) -> Self {
Self {
2023-04-29 22:04:54 +00:00
shared_context : std ::sync ::Arc ::new ( std ::sync ::RwLock ::new ( anymap ::Map ::new ( ) ) ) ,
2023-07-08 01:25:45 +00:00
response_parts : std ::sync ::Arc ::new ( RwLock ::new (
http ::response ::Response ::new ( ( ) ) . into_parts ( ) . 0 ,
) ) ,
2023-07-07 00:54:05 +00:00
parts : std ::sync ::Arc ::new ( RwLock ::new ( http ::request ::Request ::new ( ( ) ) . into_parts ( ) . 0 ) ) ,
2023-07-15 00:23:12 +00:00
html_data : Arc ::new ( RwLock ::new ( HTMLData ::default ( ) ) ) ,
2023-04-01 22:00:12 +00:00
}
}
}
2023-04-29 22:04:54 +00:00
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
2023-07-07 00:54:05 +00:00
pub fn new ( parts : impl Into < Arc < RwLock < http ::request ::Parts > > > ) -> Self {
2023-04-29 22:04:54 +00:00
Self {
parts : parts . into ( ) ,
shared_context : Arc ::new ( RwLock ::new ( SendSyncAnyMap ::new ( ) ) ) ,
2023-07-08 01:25:45 +00:00
response_parts : std ::sync ::Arc ::new ( RwLock ::new (
http ::response ::Response ::new ( ( ) ) . into_parts ( ) . 0 ,
) ) ,
2023-07-15 00:23:12 +00:00
html_data : Arc ::new ( RwLock ::new ( HTMLData ::default ( ) ) ) ,
2023-04-29 22:04:54 +00:00
}
}
/// 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 ( | _ | ( ) )
}
2023-07-08 01:25:45 +00:00
/// Get the response parts from the server context
pub fn response_parts ( & self ) -> LockResult < RwLockReadGuard < '_ , http ::response ::Parts > > {
self . response_parts . read ( )
2023-04-29 22:04:54 +00:00
}
2023-07-08 01:25:45 +00:00
/// Get the response parts from the server context
pub fn response_parts_mut (
2023-04-29 22:04:54 +00:00
& self ,
2023-07-08 01:25:45 +00:00
) -> LockResult < RwLockWriteGuard < '_ , http ::response ::Parts > > {
self . response_parts . write ( )
2023-04-29 22:04:54 +00:00
}
/// 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
2023-07-07 00:54:05 +00:00
pub fn request_parts (
& self ,
) -> std ::sync ::LockResult < RwLockReadGuard < '_ , http ::request ::Parts > > {
self . parts . read ( )
}
/// 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_mut (
& self ,
) -> std ::sync ::LockResult < RwLockWriteGuard < '_ , http ::request ::Parts > > {
self . parts . write ( )
2023-04-29 22:04:54 +00:00
}
2023-04-01 22:00:12 +00:00
2023-07-07 00:54:05 +00:00
/// Extract some part from the request
pub async fn extract < R : std ::error ::Error , T : FromServerContext < Rejection = R > > (
& self ,
) -> Result < T , R > {
T ::from_request ( self ) . await
}
2023-07-15 00:23:12 +00:00
/// Insert some data into the html data store
2023-07-17 16:48:35 +00:00
pub ( crate ) fn push_html_data < T : serde ::Serialize > (
2023-07-15 00:23:12 +00:00
& self ,
value : & T ,
) -> Result < ( ) , PoisonError < RwLockWriteGuard < '_ , HTMLData > > > {
self . html_data . write ( ) . map ( | mut map | {
map . push ( value ) ;
} )
}
/// Get the html data store
pub ( crate ) fn html_data ( & self ) -> LockResult < RwLockReadGuard < '_ , HTMLData > > {
self . html_data . read ( )
}
2023-04-01 22:00:12 +00:00
}
2023-07-07 00:54:05 +00:00
}
2023-04-03 13:09:22 +00:00
2023-07-07 00:54:05 +00:00
std ::thread_local! {
2023-07-17 23:48:54 +00:00
pub ( crate ) static SERVER_CONTEXT : std ::cell ::RefCell < Box < DioxusServerContext > > = std ::cell ::RefCell ::new ( Box ::new ( DioxusServerContext ::default ( ) ) ) ;
2023-07-07 00:54:05 +00:00
}
/// Get information about the current server request.
///
2023-07-08 01:25:45 +00:00
/// This function will only provide the current server context if it is called from a server function or on the server rendering a request.
2023-07-07 00:54:05 +00:00
pub fn server_context ( ) -> DioxusServerContext {
2023-07-07 18:03:59 +00:00
SERVER_CONTEXT . with ( | ctx | * ctx . borrow ( ) . clone ( ) )
}
/// Extract some part from the current server request.
2023-07-08 01:25:45 +00:00
///
/// This function will only provide the current server context if it is called from a server function or on the server rendering a request.
2023-07-12 22:20:44 +00:00
pub async fn extract < E : FromServerContext < I > , I > ( ) -> Result < E , E ::Rejection > {
2023-07-07 18:03:59 +00:00
E ::from_request ( & server_context ( ) ) . await
2023-07-07 00:54:05 +00:00
}
pub ( crate ) fn with_server_context < O > (
context : Box < DioxusServerContext > ,
f : impl FnOnce ( ) -> O ,
) -> ( O , Box < DioxusServerContext > ) {
// before polling the future, we need to set the context
let prev_context = SERVER_CONTEXT . with ( | ctx | ctx . replace ( context ) ) ;
// poll the future, which may call server_context()
let result = f ( ) ;
// after polling the future, we need to restore the context
( result , SERVER_CONTEXT . with ( | ctx | ctx . replace ( prev_context ) ) )
}
/// A future that provides the server context to the inner future
#[ pin_project::pin_project ]
pub struct ProvideServerContext < F : std ::future ::Future > {
context : Option < Box < DioxusServerContext > > ,
#[ pin ]
f : F ,
}
impl < F : std ::future ::Future > ProvideServerContext < F > {
/// Create a new future that provides the server context to the inner future
pub fn new ( f : F , context : DioxusServerContext ) -> Self {
Self {
context : Some ( Box ::new ( context ) ) ,
f ,
2023-04-29 22:04:54 +00:00
}
}
2023-04-03 13:09:22 +00:00
}
2023-07-07 00:54:05 +00:00
impl < F : std ::future ::Future > std ::future ::Future for ProvideServerContext < F > {
type Output = F ::Output ;
fn poll (
self : std ::pin ::Pin < & mut Self > ,
cx : & mut std ::task ::Context < '_ > ,
) -> std ::task ::Poll < Self ::Output > {
let this = self . project ( ) ;
let context = this . context . take ( ) . unwrap ( ) ;
let ( result , context ) = with_server_context ( context , | | this . f . poll ( cx ) ) ;
* this . context = Some ( context ) ;
result
}
}
/// A trait for extracting types from the server context
#[ async_trait::async_trait(?Send) ]
2023-07-08 01:25:45 +00:00
pub trait FromServerContext < I = ( ) > : Sized {
/// The error type returned when extraction fails. This type must implement `std::error::Error`.
2023-07-07 00:54:05 +00:00
type Rejection : std ::error ::Error ;
/// Extract this type from the server context.
async fn from_request ( req : & DioxusServerContext ) -> Result < Self , Self ::Rejection > ;
}
/// A type was not found in the server context
pub struct NotFoundInServerContext < T : 'static > ( std ::marker ::PhantomData < T > ) ;
impl < T : 'static > std ::fmt ::Debug for NotFoundInServerContext < T > {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
let type_name = std ::any ::type_name ::< T > ( ) ;
write! ( f , " `{type_name}` not found in server context " )
}
}
impl < T : 'static > std ::fmt ::Display for NotFoundInServerContext < T > {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
let type_name = std ::any ::type_name ::< T > ( ) ;
write! ( f , " `{type_name}` not found in server context " )
}
}
impl < T : 'static > std ::error ::Error for NotFoundInServerContext < T > { }
pub struct FromContext < T : std ::marker ::Send + std ::marker ::Sync + Clone + 'static > ( pub ( crate ) T ) ;
#[ async_trait::async_trait(?Send) ]
impl < T : Send + Sync + Clone + 'static > FromServerContext for FromContext < T > {
type Rejection = NotFoundInServerContext < T > ;
async fn from_request ( req : & DioxusServerContext ) -> Result < Self , Self ::Rejection > {
Ok ( Self ( req . clone ( ) . get ::< T > ( ) . ok_or_else ( | | {
NotFoundInServerContext ::< T > ( std ::marker ::PhantomData ::< T > )
} ) ? ) )
}
}
#[ cfg(feature = " axum " ) ]
/// An adapter for axum extractors for the server context
2023-07-08 01:25:45 +00:00
pub struct Axum ;
2023-07-07 00:54:05 +00:00
#[ cfg(feature = " axum " ) ]
#[ async_trait::async_trait(?Send) ]
impl <
I : axum ::extract ::FromRequestParts < ( ) , Rejection = R > ,
R : axum ::response ::IntoResponse + std ::error ::Error ,
2023-07-08 01:25:45 +00:00
> FromServerContext < Axum > for I
2023-07-07 00:54:05 +00:00
{
type Rejection = R ;
async fn from_request ( req : & DioxusServerContext ) -> Result < Self , Self ::Rejection > {
2023-07-08 01:25:45 +00:00
Ok ( I ::from_request_parts ( & mut * req . request_parts_mut ( ) . unwrap ( ) , & ( ) ) . await ? )
2023-07-07 00:54:05 +00:00
}
}