mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 11:21:24 +00:00
Move internal middleware into own file
This commit is contained in:
parent
e3462f6664
commit
4bb9d81f51
2 changed files with 106 additions and 107 deletions
|
@ -1,114 +1,9 @@
|
||||||
mod deadline;
|
mod deadline;
|
||||||
|
mod internal;
|
||||||
mod metrics;
|
mod metrics;
|
||||||
mod payload;
|
mod payload;
|
||||||
|
|
||||||
use actix_web::{
|
|
||||||
dev::{Service, ServiceRequest, Transform},
|
|
||||||
http::StatusCode,
|
|
||||||
HttpResponse, ResponseError,
|
|
||||||
};
|
|
||||||
use std::{
|
|
||||||
future::{ready, Future, Ready},
|
|
||||||
pin::Pin,
|
|
||||||
task::{Context, Poll},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub(crate) use self::deadline::Deadline;
|
pub(crate) use self::deadline::Deadline;
|
||||||
|
pub(crate) use self::internal::Internal;
|
||||||
pub(crate) use self::metrics::Metrics;
|
pub(crate) use self::metrics::Metrics;
|
||||||
pub(crate) use self::payload::Payload;
|
pub(crate) use self::payload::Payload;
|
||||||
|
|
||||||
pub(crate) struct Internal(pub(crate) Option<String>);
|
|
||||||
pub(crate) struct InternalMiddleware<S>(Option<String>, S);
|
|
||||||
#[derive(Clone, Debug, thiserror::Error)]
|
|
||||||
#[error("Invalid API Key")]
|
|
||||||
pub(crate) struct ApiError;
|
|
||||||
|
|
||||||
pin_project_lite::pin_project! {
|
|
||||||
#[project = InternalFutureProj]
|
|
||||||
#[project_replace = InternalFutureProjReplace]
|
|
||||||
pub(crate) enum InternalFuture<F> {
|
|
||||||
Internal {
|
|
||||||
#[pin]
|
|
||||||
future: F,
|
|
||||||
},
|
|
||||||
Error {
|
|
||||||
error: Option<ApiError>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ResponseError for ApiError {
|
|
||||||
fn status_code(&self) -> StatusCode {
|
|
||||||
StatusCode::UNAUTHORIZED
|
|
||||||
}
|
|
||||||
|
|
||||||
fn error_response(&self) -> HttpResponse {
|
|
||||||
HttpResponse::build(self.status_code())
|
|
||||||
.content_type("application/json")
|
|
||||||
.body(
|
|
||||||
serde_json::to_string(&serde_json::json!({ "msg": self.to_string() }))
|
|
||||||
.unwrap_or_else(|_| r#"{"msg":"unauthorized"}"#.to_string()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> Transform<S, ServiceRequest> for Internal
|
|
||||||
where
|
|
||||||
S: Service<ServiceRequest, Error = actix_web::Error>,
|
|
||||||
S::Future: 'static,
|
|
||||||
{
|
|
||||||
type Response = S::Response;
|
|
||||||
type Error = S::Error;
|
|
||||||
type InitError = ();
|
|
||||||
type Transform = InternalMiddleware<S>;
|
|
||||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
|
||||||
ready(Ok(InternalMiddleware(self.0.clone(), service)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> Service<ServiceRequest> for InternalMiddleware<S>
|
|
||||||
where
|
|
||||||
S: Service<ServiceRequest, Error = actix_web::Error>,
|
|
||||||
S::Future: 'static,
|
|
||||||
{
|
|
||||||
type Response = S::Response;
|
|
||||||
type Error = S::Error;
|
|
||||||
type Future = InternalFuture<S::Future>;
|
|
||||||
|
|
||||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
||||||
self.1.poll_ready(cx)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
|
||||||
if let Some(value) = req.headers().get("x-api-token") {
|
|
||||||
if let (Ok(header), Some(api_key)) = (value.to_str(), &self.0) {
|
|
||||||
if header == api_key {
|
|
||||||
return InternalFuture::Internal {
|
|
||||||
future: self.1.call(req),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
InternalFuture::Error {
|
|
||||||
error: Some(ApiError),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, T, E> Future for InternalFuture<F>
|
|
||||||
where
|
|
||||||
F: Future<Output = Result<T, E>>,
|
|
||||||
E: From<ApiError>,
|
|
||||||
{
|
|
||||||
type Output = F::Output;
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
match self.as_mut().project() {
|
|
||||||
InternalFutureProj::Internal { future } => future.poll(cx),
|
|
||||||
InternalFutureProj::Error { error } => Poll::Ready(Err(error.take().unwrap().into())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
104
src/middleware/internal.rs
Normal file
104
src/middleware/internal.rs
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
use actix_web::{
|
||||||
|
dev::{Service, ServiceRequest, Transform},
|
||||||
|
http::StatusCode,
|
||||||
|
HttpResponse, ResponseError,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
future::{ready, Future, Ready},
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub(crate) struct Internal(pub(crate) Option<String>);
|
||||||
|
pub(crate) struct InternalMiddleware<S>(Option<String>, S);
|
||||||
|
#[derive(Clone, Debug, thiserror::Error)]
|
||||||
|
#[error("Invalid API Key")]
|
||||||
|
pub(crate) struct ApiError;
|
||||||
|
|
||||||
|
pin_project_lite::pin_project! {
|
||||||
|
#[project = InternalFutureProj]
|
||||||
|
#[project_replace = InternalFutureProjReplace]
|
||||||
|
pub(crate) enum InternalFuture<F> {
|
||||||
|
Internal {
|
||||||
|
#[pin]
|
||||||
|
future: F,
|
||||||
|
},
|
||||||
|
Error {
|
||||||
|
error: Option<ApiError>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResponseError for ApiError {
|
||||||
|
fn status_code(&self) -> StatusCode {
|
||||||
|
StatusCode::UNAUTHORIZED
|
||||||
|
}
|
||||||
|
|
||||||
|
fn error_response(&self) -> HttpResponse {
|
||||||
|
HttpResponse::build(self.status_code()).json(serde_json::json!({
|
||||||
|
"msg": self.to_string(),
|
||||||
|
"code": "invalid-api-token",
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Transform<S, ServiceRequest> for Internal
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Error = actix_web::Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
{
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type InitError = ();
|
||||||
|
type Transform = InternalMiddleware<S>;
|
||||||
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
ready(Ok(InternalMiddleware(self.0.clone(), service)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Service<ServiceRequest> for InternalMiddleware<S>
|
||||||
|
where
|
||||||
|
S: Service<ServiceRequest, Error = actix_web::Error>,
|
||||||
|
S::Future: 'static,
|
||||||
|
{
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type Future = InternalFuture<S::Future>;
|
||||||
|
|
||||||
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
self.1.poll_ready(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||||
|
if let Some(value) = req.headers().get("x-api-token") {
|
||||||
|
if let (Ok(header), Some(api_key)) = (value.to_str(), &self.0) {
|
||||||
|
if header == api_key {
|
||||||
|
return InternalFuture::Internal {
|
||||||
|
future: self.1.call(req),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
InternalFuture::Error {
|
||||||
|
error: Some(ApiError),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, T, E> Future for InternalFuture<F>
|
||||||
|
where
|
||||||
|
F: Future<Output = Result<T, E>>,
|
||||||
|
E: From<ApiError>,
|
||||||
|
{
|
||||||
|
type Output = F::Output;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
match self.as_mut().project() {
|
||||||
|
InternalFutureProj::Internal { future } => future.poll(cx),
|
||||||
|
InternalFutureProj::Error { error } => Poll::Ready(Err(error.take().unwrap().into())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue