-
I know how to get state using ////////////// AppState
#[derive(Clone, FromRef)]
pub struct AppState {
pub db: DbState,
}
///////////// Extractor
#[async_trait]
impl<S> FromRequestParts<S> for Authentication
where
S: Send + Sync,
{
type Rejection = AppError;
async fn from_request_parts(
parts: &mut Parts,
state: &S,
) -> std::result::Result<Self, Self::Rejection> {
// this works
// let Extension(app_state) = parts
// .extract::<Extension<AppState>>()
// .await
// .map_err(|e| AppError::InternalServerError(e.into()))?;
// this throws an error
let state = parts
.extract_with_state::<AppState, _>(state)
.await
.map_err(|e| AppError::InternalServerError(e.into()))?;
}
}
//////////////////// Router
Router::new()
.api_route("/api/healthcheck", get(healthcheck::controller))
.with_state(app_state) I get the following compiler error, which says that
#[async_trait]
impl<S> FromRequestParts<S> for AppState
where
S: Send + Sync,
{
type Rejection = AppError;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
// How do I extract the AppState here?
// when I console log the `state` parameter, it looks like it's the AppState, but I'm not sure how to convert the type to AppState
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
nick-kang
Feb 6, 2023
Replies: 1 comment 5 replies
-
After posting this, I think I was able to solve this, but would love to see if there is a better way. ///////////// Extractor
#[async_trait]
impl<S> FromRequestParts<S> for Authentication
where
AppState: FromRef<S>, // <---- added this line
S: Send + Sync,
{
type Rejection = AppError;
async fn from_request_parts(
parts: &mut Parts,
state: &S,
) -> std::result::Result<Self, Self::Rejection> {
let state = parts
.extract_with_state::<AppState, _>(state)
.await
.map_err(|e| AppError::InternalServerError(e.into()))?;
}
} #[async_trait]
impl<S> FromRequestParts<S> for AppState
where
Self: FromRef<S>, // <---- added this line
S: Send + Sync + Debug,
{
type Rejection = AppError;
async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
Ok(Self::from_ref(state)) // <---- added this line
}
} |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
davidpdrsn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After posting this, I think I was able to solve this, but would love to see if there is a better way.