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