8000 Added usage of `resolve_ref()` across `HttpContext` and `HttpRequest` by RomanEmreis · Pull Request #71 · RomanEmreis/volga · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added usage of resolve_ref() across HttpContext and HttpRequest #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/di.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ pub mod container;
struct DiError;

impl DiError {
#[inline]
fn service_not_registered(type_name: &str) -> Error {
Error::client_error(format!("Services Error: service not registered: {}", type_name))
Error::server_error(format!("Services Error: service not registered: {}", type_name))
}

#[inline]
fn resolve_error(type_name: &str) -> Error {
Error::client_error(format!("Services Error: unable to resolve the service: {}", type_name))
Error::server_error(format!("Services Error: unable to resolve the service: {}", type_name))
}

#[inline]
fn resolve_transient_error() -> Error {
Error::server_error(
"Services Error: cannot resolve a `Transient` service as ref, use `resolve::<T>()` or `Dc<T>` instead",
)
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/di/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ impl Container {
/// Resolve a service as ref
pub async fn resolve_ref<T: Inject + 'static>(&mut self) -> Result<&T, Error> {
match self.get_service_entry::<T>()? {
ServiceEntry::Transient => Err(Error::server_error(
"cannot resolve a `Transient` service as ref, use `resolve::<T>()` or `Dc<T>` instead",
)),
ServiceEntry::Transient => Err(DiError::resolve_transient_error()),
ServiceEntry::Singleton(instance) => Self::resolve_internal(instance),
ServiceEntry::Scoped(cell) => {
let instance = cell
Expand Down
1 change: 1 addition & 0 deletions src/di/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub trait Inject: Clone + Send + Sync {
}

impl<T: Default + Clone + Send + Sync> Inject for T {
#[inline]
fn inject(_: &mut Container) -> impl Future<Output = Result<Self, Error>> + Send {
ok(Self::default())
}
Expand Down
7 changes: 7 additions & 0 deletions src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ impl HttpRequest {
pub async fn resolve<T: Inject + 'static>(&mut self) -> Result<T, Error> {
self.container.resolve::<T>().await
}

/// Resolves a service from Dependency Container and returns a reference
#[inline]
#[cfg(feature = "di")]
pub async fn resolve_ref<T: Inject + 'static>(&mut self) -> Result<&T, Error> {
self.container.resolve_ref::<T>().await
}

/// Extracts a payload from request parts
///
Expand Down
11 changes: 11 additions & 0 deletions src/middleware/http_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct HttpContext {

impl HttpContext {
/// Creates a new [`HttpContext`]
#[inline]
pub(crate) fn new(
request: HttpRequest,
handler: RouteHandler,
Expand All @@ -34,6 +35,7 @@ impl HttpContext {
Self { request, handler, error_handler }
}

#[inline]
#[allow(dead_code)]
pub(super) fn into_parts(self) -> (HttpRequest, RouteHandler, WeakErrorHandler) {
(self.request, self.handler, self.error_handler)
Expand Down Expand Up @@ -63,10 +65,18 @@ impl HttpContext {
}

/// Resolves a service from Dependency Container
#[inline]
#[cfg(feature = "di")]
pub async fn resolve<T: Inject + 'static>(&mut self) -> Result<T, Error> {
self.request.resolve::<T>().await
}

/// Resolves a service from Dependency Container and returns a reference
#[inline]
#[cfg(feature = "di")]
pub async fn resolve_ref<T: Inject + 'static>(&mut self) -> Result<&T, Error> {
self.request.resolve_ref::<T>().await
}

/// Inserts the [`Header<T>`] to HTTP request headers
#[inline]
Expand All @@ -75,6 +85,7 @@ impl HttpContext {
}

/// Executes the request handler for current HTTP request
#[inline]
pub(crate) async fn execute(self) -> HttpResult {
self.handler.call(self.request).await
}
Expand Down
0