Implements Configuration via Env Variables
This commit is contained in:
17
src/config/config_auth.rs
Normal file
17
src/config/config_auth.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use std::env;
|
||||
use cached::proc_macro::cached;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ConfigAuth {
|
||||
pub auth_url: String,
|
||||
}
|
||||
|
||||
#[cached]
|
||||
pub fn get_config_auth() -> ConfigAuth {
|
||||
let url = env::var("AUTH_URL")
|
||||
.expect("AUTH_URL must be set");
|
||||
|
||||
ConfigAuth {
|
||||
auth_url: url,
|
||||
}
|
||||
}
|
||||
22
src/config/config_server.rs
Normal file
22
src/config/config_server.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use std::env;
|
||||
use cached::proc_macro::cached;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ConfigServer {
|
||||
pub(crate) host: String,
|
||||
pub(crate) port: u16,
|
||||
}
|
||||
|
||||
#[cached]
|
||||
pub fn get_config_server() -> ConfigServer {
|
||||
let h = option_env!("HOST")
|
||||
.unwrap_or("localhost").to_string();
|
||||
|
||||
let p = option_env!("PORT").unwrap_or("8500")
|
||||
.parse::<u16>().unwrap();
|
||||
|
||||
ConfigServer {
|
||||
host: h,
|
||||
port: p,
|
||||
}
|
||||
}
|
||||
2
src/config/mod.rs
Normal file
2
src/config/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod config_server;
|
||||
pub mod config_auth;
|
||||
@@ -5,9 +5,7 @@ use axum::extract::State;
|
||||
|
||||
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();
|
||||
println!("Received message: {:?}", response);
|
||||
|
||||
(StatusCode::OK, Json(response))
|
||||
}
|
||||
|
||||
11
src/main.rs
11
src/main.rs
@@ -2,12 +2,21 @@ mod handler;
|
||||
mod middleware;
|
||||
mod model;
|
||||
mod route;
|
||||
mod config;
|
||||
|
||||
use crate::config::config_server;
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenv::dotenv().ok();
|
||||
let server_config = config_server::get_config_server();
|
||||
|
||||
let app = route::create_route();
|
||||
|
||||
println!("🚀 Server started successfully");
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:8500").await.unwrap();
|
||||
let listener = tokio::net::TcpListener::bind(
|
||||
format!("{}:{}", server_config.host, server_config.port)
|
||||
).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use axum::{
|
||||
};
|
||||
use reqwest::header::AUTHORIZATION;
|
||||
use crate::model::send_message::MessageAuthor;
|
||||
use crate::config::config_auth;
|
||||
|
||||
pub async fn auth_middleware(mut request: Request, next: Next) -> Result<Response, StatusCode> {
|
||||
let token = get_token(&request).ok_or(StatusCode::UNAUTHORIZED)?;
|
||||
@@ -30,8 +31,12 @@ fn get_token(req: &Request) -> Option<String> {
|
||||
async fn validate_token(token: &str) -> Option<MessageAuthor> {
|
||||
println!("Received token: {}", token);
|
||||
|
||||
let auth_config = config_auth::get_config_auth();
|
||||
|
||||
let validation_url = format!("{}/user/login/validate", auth_config.auth_url);
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let response = client.post("http://localhost:8070/user/login/validate")
|
||||
let response = client.post(validation_url.as_str())
|
||||
.header(AUTHORIZATION, format!("Bearer {}", token))
|
||||
.send().await.unwrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user