mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
- Removed "/schedule/update-download-url" endpoint, this mechanism was replaced by Yandex Cloud FaaS. Ура :) - Improved schedule caching mechanism. - Added Telegram WebApp authentication support. - Reworked endpoints responses and errors mechanism. - Refactored application state management. - Make synchronous database operations, middlewares and extractors to asynchronous. - Made user password field optional to support multiple auth methods. - Renamed users table column "version" to "android_version" and made it nullable.
157 lines
3.9 KiB
Rust
157 lines
3.9 KiB
Rust
use actix_web::dev::Payload;
|
||
use actix_web::{FromRequest, HttpRequest};
|
||
use futures_util::future::LocalBoxFuture;
|
||
use std::future::{Ready, ready};
|
||
use std::ops;
|
||
|
||
/// # Async extractor.
|
||
|
||
/// Asynchronous object extractor from a query.
|
||
pub struct AsyncExtractor<T>(T);
|
||
|
||
impl<T> AsyncExtractor<T> {
|
||
#[allow(dead_code)]
|
||
/// Retrieve the object extracted with the extractor.
|
||
pub fn into_inner(self) -> T {
|
||
self.0
|
||
}
|
||
}
|
||
|
||
impl<T> ops::Deref for AsyncExtractor<T> {
|
||
type Target = T;
|
||
|
||
fn deref(&self) -> &Self::Target {
|
||
&self.0
|
||
}
|
||
}
|
||
|
||
impl<T> ops::DerefMut for AsyncExtractor<T> {
|
||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||
&mut self.0
|
||
}
|
||
}
|
||
|
||
pub trait FromRequestAsync: Sized {
|
||
type Error: Into<actix_web::Error>;
|
||
|
||
/// Asynchronous function for extracting data from a query.
|
||
///
|
||
/// returns: Result<Self, Self::Error>
|
||
///
|
||
/// # Examples
|
||
///
|
||
/// ```
|
||
/// struct User {
|
||
/// pub id: String,
|
||
/// pub username: String,
|
||
/// }
|
||
///
|
||
/// // TODO: Я вообще этот экстрактор не использую, нахуя мне тогда писать пример, если я не ебу как его использовать. Я забыл.
|
||
///
|
||
/// #[get("/")]
|
||
/// fn get_user_async(
|
||
/// user: web::AsyncExtractor<User>,
|
||
/// ) -> web::Json<User> {
|
||
/// let user = user.into_inner();
|
||
///
|
||
/// web::Json(user)
|
||
/// }
|
||
/// ```
|
||
async fn from_request_async(
|
||
req: &HttpRequest,
|
||
payload: &mut Payload,
|
||
) -> Result<Self, Self::Error>;
|
||
}
|
||
|
||
impl<T: FromRequestAsync> FromRequest for AsyncExtractor<T> {
|
||
type Error = T::Error;
|
||
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
||
|
||
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
||
let req = req.clone();
|
||
let mut payload = Payload::None;
|
||
|
||
Box::pin(async move {
|
||
T::from_request_async(&req, &mut payload)
|
||
.await
|
||
.map(|res| Self(res))
|
||
})
|
||
}
|
||
}
|
||
|
||
/// # Sync extractor.
|
||
|
||
/// Synchronous object extractor from a query.
|
||
pub struct SyncExtractor<T>(T);
|
||
|
||
impl<T> SyncExtractor<T> {
|
||
/// Retrieving an object extracted with the extractor.
|
||
#[allow(unused)]
|
||
pub fn into_inner(self) -> T {
|
||
self.0
|
||
}
|
||
}
|
||
|
||
impl<T> ops::Deref for SyncExtractor<T> {
|
||
type Target = T;
|
||
|
||
fn deref(&self) -> &Self::Target {
|
||
&self.0
|
||
}
|
||
}
|
||
|
||
impl<T> ops::DerefMut for SyncExtractor<T> {
|
||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||
&mut self.0
|
||
}
|
||
}
|
||
|
||
pub trait FromRequestSync: Sized {
|
||
type Error: Into<actix_web::Error>;
|
||
|
||
/// Synchronous function for extracting data from a query.
|
||
///
|
||
/// returns: Result<Self, Self::Error>
|
||
///
|
||
/// # Examples
|
||
///
|
||
/// ```
|
||
/// struct User {
|
||
/// pub id: String,
|
||
/// pub username: String,
|
||
/// }
|
||
///
|
||
/// impl FromRequestSync for User {
|
||
/// type Error = actix_web::Error;
|
||
///
|
||
/// fn from_request_sync(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||
/// // do magic here.
|
||
///
|
||
/// Ok(User {
|
||
/// id: "qwerty".to_string(),
|
||
/// username: "n08i40k".to_string()
|
||
/// })
|
||
/// }
|
||
/// }
|
||
///
|
||
/// #[get("/")]
|
||
/// fn get_user_sync(
|
||
/// user: web::SyncExtractor<User>,
|
||
/// ) -> web::Json<User> {
|
||
/// let user = user.into_inner();
|
||
///
|
||
/// web::Json(user)
|
||
/// }
|
||
/// ```
|
||
fn from_request_sync(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error>;
|
||
}
|
||
|
||
impl<T: FromRequestSync> FromRequest for SyncExtractor<T> {
|
||
type Error = T::Error;
|
||
type Future = Ready<Result<Self, Self::Error>>;
|
||
|
||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||
ready(T::from_request_sync(req, payload).map(|res| Self(res)))
|
||
}
|
||
}
|