mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
0.5.0
Возвращёна реализация сериализации в json для IResponse Добавлены типы для экстракции данных из запросов средствами actix-web Добавлен экстрактор для получения пользователя по токену доступа передаваемому в запросе Добавлен макрос для автоматической реализации ResponseError для ошибок экстракторов Добавлен эндпоинт users/me Из главного проекта исключена зависимость actix-http посредством переноса части тестового функционала в отдельный crate
This commit is contained in:
69
src/extractors/authorized_user.rs
Normal file
69
src/extractors/authorized_user.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use crate::app_state::AppState;
|
||||
use crate::database::driver;
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::base::FromRequestSync;
|
||||
use crate::utility::jwt;
|
||||
use actix_macros::ResponseErrorMessage;
|
||||
use actix_web::body::BoxBody;
|
||||
use actix_web::dev::Payload;
|
||||
use actix_web::{HttpRequest, web};
|
||||
use derive_more::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Display, ResponseErrorMessage)]
|
||||
#[status_code = "actix_web::http::StatusCode::UNAUTHORIZED"]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum Error {
|
||||
#[display("No authorization header found")]
|
||||
NoHeader,
|
||||
|
||||
#[display("Bearer token is required")]
|
||||
UnknownAuthorizationType,
|
||||
|
||||
#[display("No bearer token provided")]
|
||||
NoBearerToken,
|
||||
|
||||
#[display("Invalid or expired access token")]
|
||||
InvalidAccessToken,
|
||||
|
||||
#[display("No user associated with access token")]
|
||||
NoUser,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn into_err(self) -> actix_web::Error {
|
||||
actix_web::Error::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequestSync for User {
|
||||
type Error = actix_web::Error;
|
||||
|
||||
fn from_request_sync(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||
let authorization = req
|
||||
.headers()
|
||||
.get("Authorization")
|
||||
.ok_or(Error::NoHeader.into_err())?
|
||||
.to_str()
|
||||
.map_err(|_| Error::NoHeader.into_err())?
|
||||
.to_string();
|
||||
|
||||
let parts: Vec<&str> = authorization.split(' ').collect();
|
||||
|
||||
if parts.len() == 0 || parts[0] != "Bearer" {
|
||||
return Err(Error::UnknownAuthorizationType.into_err());
|
||||
}
|
||||
|
||||
if parts.len() < 2 {
|
||||
return Err(Error::NoBearerToken.into());
|
||||
}
|
||||
|
||||
let user_id = jwt::verify_and_decode(&parts[1].to_string())
|
||||
.map_err(|_| Error::InvalidAccessToken.into_err())?;
|
||||
|
||||
let app_state = req.app_data::<web::Data<AppState>>().unwrap();
|
||||
|
||||
driver::users::get(&app_state.database, &user_id).map_err(|_| Error::NoUser.into())
|
||||
}
|
||||
}
|
||||
56
src/extractors/base.rs
Normal file
56
src/extractors/base.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use actix_web::dev::Payload;
|
||||
use actix_web::{FromRequest, HttpRequest};
|
||||
use futures_util::future::LocalBoxFuture;
|
||||
use std::future::{Ready, ready};
|
||||
|
||||
pub trait FromRequestAsync: Sized {
|
||||
type Error: Into<actix_web::Error>;
|
||||
|
||||
async fn from_request_async(req: HttpRequest, payload: Payload) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
pub struct AsyncExtractor<T>(T);
|
||||
|
||||
impl<T> AsyncExtractor<T> {
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
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 payload = payload.take();
|
||||
Box::pin(async move {
|
||||
T::from_request_async(req, payload)
|
||||
.await
|
||||
.map(|res| Self(res))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FromRequestSync: Sized {
|
||||
type Error: Into<actix_web::Error>;
|
||||
|
||||
fn from_request_sync(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
pub struct SyncExtractor<T>(T);
|
||||
|
||||
impl<T> SyncExtractor<T> {
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
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)))
|
||||
}
|
||||
}
|
||||
2
src/extractors/mod.rs
Normal file
2
src/extractors/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod authorized_user;
|
||||
pub mod base;
|
||||
27
src/main.rs
27
src/main.rs
@@ -1,33 +1,44 @@
|
||||
use crate::app_state::{AppState, app_state};
|
||||
use crate::routes::auth::sign_in::{sign_in_default, sign_in_vk};
|
||||
use crate::routes::auth::sign_up::{sign_up_default, sign_up_vk};
|
||||
use crate::routes::users::me::me;
|
||||
use actix_web::{App, HttpServer, web};
|
||||
use dotenvy::dotenv;
|
||||
|
||||
mod app_state;
|
||||
|
||||
mod database;
|
||||
mod routes;
|
||||
|
||||
mod test_env;
|
||||
|
||||
mod utility;
|
||||
mod xls_downloader;
|
||||
|
||||
mod parser;
|
||||
mod xls_downloader;
|
||||
|
||||
mod extractors;
|
||||
mod routes;
|
||||
|
||||
mod utility;
|
||||
|
||||
mod test_env;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
unsafe { std::env::set_var("RUST_LOG", "debug") };
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(move || {
|
||||
let auth_scope = web::scope("/auth")
|
||||
.service(sign_in_default)
|
||||
.service(sign_in_vk)
|
||||
.service(sign_up_default)
|
||||
.service(sign_up_vk);
|
||||
let api_scope = web::scope("/api/v1").service(auth_scope);
|
||||
let users_scope = web::scope("/users").service(me);
|
||||
|
||||
App::new().app_data(move || app_state()).service(api_scope)
|
||||
let api_scope = web::scope("/api/v1")
|
||||
.service(auth_scope)
|
||||
.service(users_scope);
|
||||
|
||||
App::new().app_data(app_state()).service(api_scope)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))
|
||||
.unwrap()
|
||||
|
||||
@@ -132,14 +132,15 @@ mod tests {
|
||||
use crate::database::driver;
|
||||
use crate::database::models::{User, UserRole};
|
||||
use crate::routes::auth::sign_in::sign_in_default;
|
||||
use crate::test_env::tests::{static_app_state, test_app, test_app_state, test_env};
|
||||
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
||||
use crate::utility;
|
||||
use actix_http::StatusCode;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::Method;
|
||||
use actix_web::test;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fmt::Write;
|
||||
use actix_test::test_app;
|
||||
|
||||
async fn sign_in_client(data: Request) -> ServiceResponse {
|
||||
let app = test_app(test_app_state(), sign_in_default).await;
|
||||
|
||||
@@ -208,11 +208,12 @@ mod tests {
|
||||
use crate::database::models::UserRole;
|
||||
use crate::routes::auth::sign_up::schema::Request;
|
||||
use crate::routes::auth::sign_up::sign_up_default;
|
||||
use crate::test_env::tests::{static_app_state, test_app, test_app_state, test_env};
|
||||
use actix_http::StatusCode;
|
||||
use crate::test_env::tests::{static_app_state, test_app_state, test_env};
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::dev::ServiceResponse;
|
||||
use actix_web::http::Method;
|
||||
use actix_web::test;
|
||||
use actix_test::test_app;
|
||||
|
||||
struct SignUpPartial {
|
||||
username: String,
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod auth;
|
||||
pub mod users;
|
||||
mod schema;
|
||||
|
||||
@@ -2,7 +2,7 @@ use actix_web::body::EitherBody;
|
||||
use actix_web::error::JsonPayloadError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{HttpRequest, HttpResponse, Responder};
|
||||
use serde::Serialize;
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
pub struct IResponse<T: Serialize, E: Serialize>(pub Result<T, E>);
|
||||
|
||||
@@ -10,11 +10,29 @@ pub trait ErrorToHttpCode {
|
||||
fn to_http_status_code(&self) -> StatusCode;
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize> IResponse<T, E> {
|
||||
pub fn new(result: Result<T, E>) -> Self {
|
||||
IResponse(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize> Serialize for IResponse<T, E> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match &self.0 {
|
||||
Ok(ok) => serializer.serialize_some::<T>(&ok),
|
||||
Err(err) => serializer.serialize_some::<E>(&err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, E: Serialize + ErrorToHttpCode> Responder for IResponse<T, E> {
|
||||
type Body = EitherBody<String>;
|
||||
|
||||
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||
match serde_json::to_string(&self.0) {
|
||||
match serde_json::to_string(&self) {
|
||||
Ok(body) => {
|
||||
let code = match &self.0 {
|
||||
Ok(_) => StatusCode::OK,
|
||||
|
||||
11
src/routes/users/me.rs
Normal file
11
src/routes/users/me.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use crate::app_state::AppState;
|
||||
use crate::database::models::User;
|
||||
use crate::extractors::base::SyncExtractor;
|
||||
use actix_web::{HttpResponse, Responder, get, web};
|
||||
|
||||
#[get("/me")]
|
||||
pub async fn me(user: SyncExtractor<User>, app_state: web::Data<AppState>) -> impl Responder {
|
||||
HttpResponse::Ok().json(user.into_inner())
|
||||
}
|
||||
|
||||
mod schema {}
|
||||
1
src/routes/users/mod.rs
Normal file
1
src/routes/users/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod me;
|
||||
@@ -1,25 +1,14 @@
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use crate::app_state::{app_state, AppState, Schedule};
|
||||
use actix_web::dev::{HttpServiceFactory, Service, ServiceResponse};
|
||||
use actix_web::{test, web, App};
|
||||
use std::sync::LazyLock;
|
||||
use crate::parser::tests::test_result;
|
||||
use actix_web::{web};
|
||||
use std::sync::LazyLock;
|
||||
|
||||
pub fn test_env() {
|
||||
dotenvy::from_path(".env.test").expect("Failed to load test environment file");
|
||||
}
|
||||
|
||||
pub async fn test_app<F>(
|
||||
app_state: web::Data<AppState>,
|
||||
factory: F,
|
||||
) -> impl Service<actix_http::Request, Response = ServiceResponse, Error = actix_web::Error>
|
||||
where
|
||||
F: HttpServiceFactory + 'static,
|
||||
{
|
||||
test::init_service(App::new().app_data(app_state).service(factory)).await
|
||||
}
|
||||
|
||||
pub fn test_app_state() -> web::Data<AppState> {
|
||||
let state = app_state();
|
||||
let mut schedule_lock = state.schedule.lock().unwrap();
|
||||
|
||||
18
src/utility/error.rs
Normal file
18
src/utility/error.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use std::fmt::{Write};
|
||||
use std::fmt::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ResponseErrorMessage<T: Display> {
|
||||
code: T,
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl<T: Display + Serialize> ResponseErrorMessage<T> {
|
||||
pub fn new(code: T) -> Self {
|
||||
let mut message = String::new();
|
||||
write!(&mut message, "{}", code).unwrap();
|
||||
|
||||
Self { code, message }
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod jwt;
|
||||
pub mod jwt;
|
||||
pub mod error;
|
||||
Reference in New Issue
Block a user