Reformats Code
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use std::env;
|
|
||||||
use cached::proc_macro::cached;
|
use cached::proc_macro::cached;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ConfigAuth {
|
pub struct ConfigAuth {
|
||||||
@@ -8,10 +8,7 @@ pub struct ConfigAuth {
|
|||||||
|
|
||||||
#[cached]
|
#[cached]
|
||||||
pub fn get_config_auth() -> ConfigAuth {
|
pub fn get_config_auth() -> ConfigAuth {
|
||||||
let url = env::var("AUTH_URL")
|
let url = env::var("AUTH_URL").expect("AUTH_URL must be set");
|
||||||
.expect("AUTH_URL must be set");
|
|
||||||
|
|
||||||
ConfigAuth {
|
ConfigAuth { auth_url: url }
|
||||||
auth_url: url,
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::env;
|
|
||||||
use cached::proc_macro::cached;
|
use cached::proc_macro::cached;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ConfigServer {
|
pub struct ConfigServer {
|
||||||
@@ -9,14 +9,12 @@ pub struct ConfigServer {
|
|||||||
|
|
||||||
#[cached]
|
#[cached]
|
||||||
pub fn get_config_server() -> ConfigServer {
|
pub fn get_config_server() -> ConfigServer {
|
||||||
let h = option_env!("HOST")
|
let h = option_env!("HOST").unwrap_or("localhost").to_string();
|
||||||
.unwrap_or("localhost").to_string();
|
|
||||||
|
|
||||||
let p = option_env!("PORT").unwrap_or("8500")
|
let p = option_env!("PORT")
|
||||||
.parse::<u16>().unwrap();
|
.unwrap_or("8500")
|
||||||
|
.parse::<u16>()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
ConfigServer {
|
ConfigServer { host: h, port: p }
|
||||||
host: h,
|
}
|
||||||
port: p,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
|
pub mod config_auth;
|
||||||
pub mod config_server;
|
pub mod config_server;
|
||||||
pub mod config_auth;
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
use crate::model::generic_response::GenericResponse;
|
use crate::model::generic_response::GenericResponse;
|
||||||
use axum::{http::StatusCode, response::IntoResponse, Json};
|
use axum::{http::StatusCode, response::IntoResponse, Json};
|
||||||
|
|
||||||
|
|
||||||
pub async fn health_check() -> impl IntoResponse {
|
pub async fn health_check() -> impl IntoResponse {
|
||||||
const MESSAGE: &str = "Server is running";
|
const MESSAGE: &str = "Server is running";
|
||||||
let response = GenericResponse {
|
let response = GenericResponse {
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
use crate::model::send_message::{MessageAuthor, SendMessage};
|
use crate::model::send_message::{MessageAuthor, SendMessage};
|
||||||
use axum::{http::StatusCode, response::IntoResponse, Json, Extension};
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
|
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
|
||||||
|
|
||||||
|
pub async fn send_message(
|
||||||
pub async fn send_message(Extension(auther): Extension<MessageAuthor>, Json(payload): Json<SendMessage>) -> impl IntoResponse {
|
Extension(auther): Extension<MessageAuthor>,
|
||||||
|
Json(payload): Json<SendMessage>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
let mut response = payload.clone();
|
let mut response = payload.clone();
|
||||||
response.author = Some(auther).clone();
|
response.author = Some(auther).clone();
|
||||||
|
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@@ -1,13 +1,12 @@
|
|||||||
|
mod config;
|
||||||
mod handler;
|
mod handler;
|
||||||
mod middleware;
|
mod middleware;
|
||||||
mod model;
|
mod model;
|
||||||
mod route;
|
mod route;
|
||||||
mod config;
|
|
||||||
mod service;
|
mod service;
|
||||||
|
|
||||||
use crate::config::config_server;
|
use crate::config::config_server;
|
||||||
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
@@ -16,8 +15,9 @@ async fn main() {
|
|||||||
let app = route::create_route();
|
let app = route::create_route();
|
||||||
|
|
||||||
println!("🚀 Server started successfully");
|
println!("🚀 Server started successfully");
|
||||||
let listener = tokio::net::TcpListener::bind(
|
let listener =
|
||||||
format!("{}:{}", server_config.host, server_config.port)
|
tokio::net::TcpListener::bind(format!("{}:{}", server_config.host, server_config.port))
|
||||||
).await.unwrap();
|
.await
|
||||||
|
.unwrap();
|
||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,20 @@
|
|||||||
use axum::{response::Response, middleware::Next, extract::Request, http::StatusCode, Extension};
|
|
||||||
use crate::service::auth_service::AuthService;
|
use crate::service::auth_service::AuthService;
|
||||||
|
use axum::{extract::Request, http::StatusCode, middleware::Next, response::Response, Extension};
|
||||||
|
|
||||||
|
pub async fn auth_middleware(
|
||||||
pub async fn auth_middleware(Extension(auth_service): Extension<AuthService>, mut request: Request, next: Next) -> Result<Response, StatusCode> {
|
Extension(auth_service): Extension<AuthService>,
|
||||||
|
mut request: Request,
|
||||||
|
next: Next,
|
||||||
|
) -> Result<Response, StatusCode> {
|
||||||
let token = get_token(&request).ok_or(StatusCode::UNAUTHORIZED)?;
|
let token = get_token(&request).ok_or(StatusCode::UNAUTHORIZED)?;
|
||||||
|
|
||||||
return match auth_service.validate_token(&token).await {
|
return match auth_service.validate_token(&token).await {
|
||||||
Some(author) => {
|
Some(author) => {
|
||||||
request.extensions_mut().insert(author);
|
request.extensions_mut().insert(author);
|
||||||
Ok(next.run(request).await)
|
Ok(next.run(request).await)
|
||||||
},
|
}
|
||||||
None => Err(StatusCode::UNAUTHORIZED)
|
None => Err(StatusCode::UNAUTHORIZED),
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_token(req: &Request) -> Option<String> {
|
fn get_token(req: &Request) -> Option<String> {
|
||||||
@@ -19,4 +22,4 @@ fn get_token(req: &Request) -> Option<String> {
|
|||||||
.get(http::header::AUTHORIZATION)
|
.get(http::header::AUTHORIZATION)
|
||||||
.and_then(|header| header.to_str().ok())
|
.and_then(|header| header.to_str().ok())
|
||||||
.map(|header| header.replace("Bearer ", ""))
|
.map(|header| header.replace("Bearer ", ""))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
pub mod auth_middleware;
|
pub mod auth_middleware;
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
pub mod generic_response;
|
pub mod generic_response;
|
||||||
pub mod send_message;
|
pub mod send_message;
|
||||||
|
|||||||
13
src/route.rs
13
src/route.rs
@@ -1,14 +1,18 @@
|
|||||||
|
use crate::config::config_auth;
|
||||||
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 axum::{routing::{get, post}, Router, middleware, Extension};
|
|
||||||
use crate::config::config_auth;
|
|
||||||
use crate::middleware::auth_middleware::auth_middleware;
|
use crate::middleware::auth_middleware::auth_middleware;
|
||||||
use crate::service::auth_service::AuthService;
|
use crate::service::auth_service::AuthService;
|
||||||
use crate::service::email_service::EmailService;
|
use crate::service::email_service::EmailService;
|
||||||
|
use axum::{
|
||||||
|
middleware,
|
||||||
|
routing::{get, post},
|
||||||
|
Extension, Router,
|
||||||
|
};
|
||||||
|
|
||||||
fn configure_message_endpoint(router: Router) -> Router {
|
fn configure_message_endpoint(router: Router) -> Router {
|
||||||
router.route("/message", post(send_message))
|
router
|
||||||
|
.route("/message", post(send_message))
|
||||||
.layer(middleware::from_fn(auth_middleware))
|
.layer(middleware::from_fn(auth_middleware))
|
||||||
.layer(Extension(AuthService::new(config_auth::get_config_auth())))
|
.layer(Extension(AuthService::new(config_auth::get_config_auth())))
|
||||||
.layer(Extension(EmailService::new()))
|
.layer(Extension(EmailService::new()))
|
||||||
@@ -18,7 +22,6 @@ fn configure_health_endpoint(router: Router) -> Router {
|
|||||||
router.route("/health", get(health_check))
|
router.route("/health", get(health_check))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn create_route() -> Router {
|
pub fn create_route() -> Router {
|
||||||
let mut router = Router::new();
|
let mut router = Router::new();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use reqwest::header::AUTHORIZATION;
|
|
||||||
use crate::config::config_auth::ConfigAuth;
|
use crate::config::config_auth::ConfigAuth;
|
||||||
use crate::model::send_message::MessageAuthor;
|
use crate::model::send_message::MessageAuthor;
|
||||||
|
use reqwest::header::AUTHORIZATION;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AuthService {
|
pub struct AuthService {
|
||||||
@@ -21,9 +20,12 @@ impl AuthService {
|
|||||||
let validation_url = format!("{}/user/login/validate", self.auth_url);
|
let validation_url = format!("{}/user/login/validate", self.auth_url);
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = client.post(validation_url.as_str())
|
let response = client
|
||||||
|
.post(validation_url.as_str())
|
||||||
.header(AUTHORIZATION, format!("Bearer {}", token))
|
.header(AUTHORIZATION, format!("Bearer {}", token))
|
||||||
.send().await.unwrap();
|
.send()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
if response.status().is_success() {
|
if response.status().is_success() {
|
||||||
let text = response.text().await.unwrap();
|
let text = response.text().await.unwrap();
|
||||||
@@ -32,4 +34,4 @@ impl AuthService {
|
|||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,4 +5,4 @@ impl EmailService {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
EmailService {}
|
EmailService {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
pub mod auth_service;
|
pub mod auth_service;
|
||||||
pub mod email_service;
|
pub mod email_service;
|
||||||
|
|||||||
Reference in New Issue
Block a user