mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
Middleware для явного указания кодировки в заголовке Content-Type.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use crate::app_state::{AppState, app_state};
|
||||
use crate::middlewares::authorization::JWTAuthorization;
|
||||
use crate::middlewares::content_type::ContentTypeBootstrap;
|
||||
use actix_web::dev::{ServiceFactory, ServiceRequest};
|
||||
use actix_web::{App, Error, HttpServer};
|
||||
use dotenvy::dotenv;
|
||||
@@ -81,7 +82,7 @@ async fn main() {
|
||||
let (app, api) = App::new()
|
||||
.into_utoipa_app()
|
||||
.app_data(app_state.clone())
|
||||
.service(get_api_scope("/api/v1"))
|
||||
.service(get_api_scope("/api/v1").wrap(ContentTypeBootstrap))
|
||||
.split_for_parts();
|
||||
|
||||
let rapidoc_service = RapiDoc::with_openapi("/api-docs-json", api).path("/api-docs");
|
||||
@@ -95,7 +96,7 @@ async fn main() {
|
||||
app.service(rapidoc_service.custom_html(patched_rapidoc_html))
|
||||
})
|
||||
.workers(4)
|
||||
.bind(("0.0.0.0", 8080))
|
||||
.bind(("0.0.0.0", 5050))
|
||||
.unwrap()
|
||||
.run()
|
||||
.await
|
||||
|
||||
64
src/middlewares/content_type.rs
Normal file
64
src/middlewares/content_type.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use actix_web::Error;
|
||||
use actix_web::body::{BoxBody, EitherBody};
|
||||
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform, forward_ready};
|
||||
use actix_web::http::header;
|
||||
use actix_web::http::header::HeaderValue;
|
||||
use futures_util::future::LocalBoxFuture;
|
||||
use std::future::{Ready, ready};
|
||||
|
||||
/// Middleware to specify the encoding in the Content-Type header.
|
||||
pub struct ContentTypeBootstrap;
|
||||
|
||||
impl<S, B> Transform<S, ServiceRequest> for ContentTypeBootstrap
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
|
||||
type Error = Error;
|
||||
type Transform = ContentTypeMiddleware<S>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(ContentTypeMiddleware { service }))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ContentTypeMiddleware<S> {
|
||||
service: S,
|
||||
}
|
||||
|
||||
impl<'a, S, B> Service<ServiceRequest> for ContentTypeMiddleware<S>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
B: 'static,
|
||||
{
|
||||
type Response = ServiceResponse<EitherBody<B, BoxBody>>;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
forward_ready!(service);
|
||||
|
||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
let fut = self.service.call(req);
|
||||
|
||||
Box::pin(async move {
|
||||
let mut response = fut.await?;
|
||||
|
||||
let headers = response.response_mut().headers_mut();
|
||||
if let Some(content_type) = headers.get("Content-Type") {
|
||||
if content_type == "application/json" {
|
||||
headers.insert(
|
||||
header::CONTENT_TYPE,
|
||||
HeaderValue::from_static("application/json; charset=utf8"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(response.map_into_left_body())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod authorization;
|
||||
pub mod authorization;
|
||||
pub mod content_type;
|
||||
Reference in New Issue
Block a user