Merge pull request #8 from HideyoshiNakazone/minor-fixes-and-reformating
Re-formats Code and Minor Fixes
This commit is contained in:
@@ -11,10 +11,14 @@ pub struct ConfigLimits {
|
||||
pub fn get_config_limits() -> ConfigLimits {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let max_requests = env::var("MAX_REQUESTS").unwrap_or("10".to_string())
|
||||
.parse::<u32>().unwrap();
|
||||
let expiration_time = env::var("EXPIRATION_TIME").unwrap_or("604800".to_string())
|
||||
.parse::<usize>().unwrap();
|
||||
let max_requests = env::var("MAX_REQUESTS")
|
||||
.unwrap_or("10".to_string())
|
||||
.parse::<u32>()
|
||||
.unwrap();
|
||||
let expiration_time = env::var("EXPIRATION_TIME")
|
||||
.unwrap_or("604800".to_string())
|
||||
.parse::<usize>()
|
||||
.unwrap();
|
||||
|
||||
ConfigLimits {
|
||||
max_requests,
|
||||
|
||||
@@ -19,6 +19,6 @@ pub fn get_config_redis() -> ConfigRedis {
|
||||
ConfigRedis {
|
||||
redis_url: url,
|
||||
redis_port: port.parse::<u16>().unwrap(),
|
||||
redis_password: password
|
||||
redis_password: password,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use cached::proc_macro::cached;
|
||||
use std::env;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ConfigServer {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub mod config_auth;
|
||||
pub mod config_email;
|
||||
pub mod config_server;
|
||||
pub mod config_redis;
|
||||
pub mod config_limits;
|
||||
pub mod config_redis;
|
||||
pub mod config_server;
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
use cached::proc_macro::cached;
|
||||
use crate::config::config_auth::get_config_auth;
|
||||
use crate::config::config_limits::get_config_limits;
|
||||
use crate::config::config_redis::get_config_redis;
|
||||
use crate::service::auth_service::AuthService;
|
||||
|
||||
use cached::proc_macro::cached;
|
||||
|
||||
#[cached]
|
||||
pub fn get_depends_auth_service() -> AuthService {
|
||||
AuthService::new(
|
||||
get_config_auth(),
|
||||
get_config_redis(),
|
||||
get_config_limits(),
|
||||
)
|
||||
AuthService::new(get_config_auth(), get_config_redis(), get_config_limits())
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use cached::proc_macro::cached;
|
||||
use crate::config::config_email::get_config_email;
|
||||
use crate::service::email_service::EmailService;
|
||||
|
||||
use cached::proc_macro::cached;
|
||||
|
||||
#[cached]
|
||||
pub fn get_depends_email_service() -> EmailService {
|
||||
EmailService::new(
|
||||
get_config_email()
|
||||
)
|
||||
EmailService::new(get_config_email())
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use crate::model::generic_response::GenericResponse;
|
||||
use crate::model::send_message::{MessageAuthor, SendMessage};
|
||||
use crate::service::auth_service::AuthService;
|
||||
use crate::service::email_service::EmailService;
|
||||
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
|
||||
use crate::service::auth_service::AuthService;
|
||||
|
||||
|
||||
pub async fn send_message(
|
||||
Extension(auth_service): Extension<AuthService>,
|
||||
@@ -25,7 +24,7 @@ pub async fn send_message(
|
||||
}
|
||||
|
||||
match email_service.send_email_smtp(package).await {
|
||||
Ok(_) => {},
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
@@ -34,7 +33,7 @@ pub async fn send_message(
|
||||
message: e.to_string(),
|
||||
}),
|
||||
)
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
auth_service.increase_user_request(&author).await;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
mod config;
|
||||
mod depends;
|
||||
mod handler;
|
||||
mod middleware;
|
||||
mod model;
|
||||
mod route;
|
||||
mod service;
|
||||
mod utils;
|
||||
mod depends;
|
||||
|
||||
use crate::config::config_server;
|
||||
|
||||
|
||||
10
src/route.rs
10
src/route.rs
@@ -1,17 +1,17 @@
|
||||
use crate::config::{config_auth, config_email};
|
||||
|
||||
use crate::depends::depends_auth_service::get_depends_auth_service;
|
||||
use crate::depends::depends_email_service::get_depends_email_service;
|
||||
use crate::handler::health::health_check;
|
||||
use crate::handler::message::send_message;
|
||||
use crate::middleware::auth_middleware::auth_middleware;
|
||||
use crate::service::auth_service::AuthService;
|
||||
use crate::service::email_service::EmailService;
|
||||
|
||||
|
||||
use crate::utils::router_builder::RouterBuilder;
|
||||
use axum::{
|
||||
middleware,
|
||||
routing::{get, post},
|
||||
Extension, Router,
|
||||
};
|
||||
use crate::depends::depends_auth_service::get_depends_auth_service;
|
||||
use crate::depends::depends_email_service::get_depends_email_service;
|
||||
|
||||
fn configure_message_endpoint(router: Router) -> Router {
|
||||
router
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use std::collections::BTreeMap;
|
||||
use redis::{AsyncCommands, ExistenceCheck, SetExpiry, SetOptions};
|
||||
use crate::config::config_auth::ConfigAuth;
|
||||
use crate::model::send_message::MessageAuthor;
|
||||
use reqwest::header::AUTHORIZATION;
|
||||
use crate::config::config_limits::ConfigLimits;
|
||||
use crate::config::config_redis::ConfigRedis;
|
||||
use crate::model::send_message::MessageAuthor;
|
||||
use redis::{AsyncCommands, ExistenceCheck, SetExpiry, SetOptions};
|
||||
use reqwest::header::AUTHORIZATION;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AuthService {
|
||||
@@ -17,8 +17,13 @@ pub struct AuthService {
|
||||
impl AuthService {
|
||||
pub fn new(config_auth: ConfigAuth, config_redis: ConfigRedis, limits: ConfigLimits) -> Self {
|
||||
let client = redis::Client::open(
|
||||
format!("redis://{}:{}", config_redis.redis_url, config_redis.redis_port).as_str()
|
||||
).unwrap();
|
||||
format!(
|
||||
"redis://{}:{}",
|
||||
config_redis.redis_url, config_redis.redis_port
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
AuthService {
|
||||
auth_url: config_auth.auth_url,
|
||||
@@ -56,7 +61,7 @@ impl AuthService {
|
||||
|
||||
pub async fn increase_user_request(&self, user: &MessageAuthor) -> bool {
|
||||
let mut con = self.redis.get_async_connection().await.unwrap();
|
||||
let current_request_key= format!(
|
||||
let current_request_key = format!(
|
||||
"user-message:{}:requests:{}",
|
||||
user.email,
|
||||
chrono::Utc::now().timestamp()
|
||||
@@ -67,12 +72,10 @@ impl AuthService {
|
||||
.conditional_set(ExistenceCheck::NX)
|
||||
.get(false);
|
||||
|
||||
return con.set_options(
|
||||
¤t_request_key,
|
||||
1,
|
||||
set_options
|
||||
).await.expect("Error setting key");
|
||||
|
||||
return con
|
||||
.set_options(¤t_request_key, 1, set_options)
|
||||
.await
|
||||
.expect("Error setting key");
|
||||
}
|
||||
|
||||
async fn count_user_requests(&self, user: &MessageAuthor) -> u32 {
|
||||
@@ -83,11 +86,10 @@ impl AuthService {
|
||||
match con.keys(query_user_requests).await {
|
||||
Ok(r) => {
|
||||
results = r;
|
||||
},
|
||||
Err(e) => {
|
||||
}
|
||||
Err(_e) => {
|
||||
return 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return results.len() as u32;
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use crate::config::config_email::ConfigEmail;
|
||||
use crate::model::send_message::{MessageAuthor, SendMessage};
|
||||
use lettre::{transport::smtp::authentication::Credentials, AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor, Address};
|
||||
use crate::model::send_message::{SendMessage};
|
||||
use lettre::message::Mailbox;
|
||||
use lettre::{
|
||||
transport::smtp::authentication::Credentials, Address, AsyncSmtpTransport, AsyncTransport,
|
||||
Message, Tokio1Executor,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EmailService {
|
||||
@@ -28,12 +31,12 @@ impl EmailService {
|
||||
|
||||
pub async fn send_email_smtp(&self, m: SendMessage) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let sender = m.author.clone().unwrap();
|
||||
let sender_mailbox = Mailbox::new(
|
||||
Some(sender.name), sender.email.parse::<Address>().unwrap()
|
||||
);
|
||||
let sender_mailbox =
|
||||
Mailbox::new(Some(sender.name), sender.email.parse::<Address>().unwrap());
|
||||
|
||||
let recipient_mailbox = Mailbox::new(
|
||||
Some(self.name.clone()), self.email.parse::<Address>().unwrap()
|
||||
Some(self.name.clone()),
|
||||
self.email.parse::<Address>().unwrap(),
|
||||
);
|
||||
|
||||
let body = self.create_email_body(&m);
|
||||
@@ -54,12 +57,7 @@ impl EmailService {
|
||||
|
||||
format!(
|
||||
"From: {} <{}>\nTo: {} <{}>\nSubject: {}\n\n{}",
|
||||
sender.name,
|
||||
sender.email,
|
||||
self.name,
|
||||
self.email,
|
||||
m.subject,
|
||||
m.message
|
||||
sender.name, sender.email, self.name, self.email, m.subject, m.message
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user