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