-
I'm pretty new to Rust, but I've been diving into the Poem framework, and I'm loving it! The type safety it brings is just awesome. After experimenting with it and building a side project, I have encountered a couple of questions, and I would greatly appreciate your guidance on these matters.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I'm not sure about the second one since i haven't messed about with it. but for your first question, my approach would be to add a example: pub struct PathProtection;
impl<E: Endpoint> Middleware<E> for PathProtection {
type Output = PathProtectionImpl<E>;
fn transform(&self, ep: E) -> Self::Output {
PathProtectionImpl { ep }
}
}
pub struct PathProtectionImpl<E> {
ep: E,
}
#[poem::async_trait]
impl<E: Endpoint> Endpoint for PathProtectionImpl<E> {
type Output = E::Output;
async fn call(&self, req: Request) -> poem::Result<Self::Output> {
const DOC_PATH: &str = "/doc";
if req.original_uri().path().starts_with(DOC_PATH) {
// Do some authentication. Maybe a password from a req.header(name)
let is_allowed = true;
if !is_allowed {
// Fail the request for example ...
// Err(Unauthorized(/* Your error here */))?;
}
}
self.ep.call(req).await
}
} Which you can attach to your app like so: let service = OpenApiService::new(
(
/* Your API */
),
"NexHub Service",
env!("CARGO_PKG_VERSION"),
);
Route::new()
.nest("/docs", service.swagger_ui())
.with(PathProtection) |
Beta Was this translation helpful? Give feedback.
-
Thank you for your response! I have also implemented a middleware to address the first issue. As for the second problem, I resolved it by setting the |
Beta Was this translation helpful? Give feedback.
I'm not sure about the second one since i haven't messed about with it.
but for your first question, my approach would be to add a
Middleware
layer and handle requests that start or contain your docs.example: