diff --git a/src/main.rs b/src/main.rs index 6fab66e..3158fd7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,8 +37,11 @@ use crate::geoip::QueryLocation; use geoip::AsnResult; use geoip::LocationResult; -use crate::templating_engine::View; -use crate::templating_engine::ResponseFormat; +use crate::templating_engine::{ + View, + ResponseFormat, + TemplateSettings, +}; use crate::ipinfo::{AddressCast,AddressInfo,AddressScope}; @@ -48,12 +51,6 @@ pub struct BaseQuery { lang: Option, } -#[derive(serde::Deserialize, serde::Serialize, Clone)] -pub struct QuerySettings { - format: ResponseFormat, - lang: String, -} - #[derive(serde::Deserialize, serde::Serialize, Clone)] pub struct IpQuery { ip: IpAddr, @@ -251,7 +248,7 @@ async fn format_and_language_middleware( next: Next ) -> Response { let format = query.format.unwrap_or(ResponseFormat::TextHtml); - req.extensions_mut().insert(QuerySettings{ + req.extensions_mut().insert(TemplateSettings{ format: format, lang: query.lang.unwrap_or("en".to_string()), }); @@ -261,20 +258,19 @@ async fn format_and_language_middleware( #[axum::debug_handler] async fn hello_world_handler( State(arc_state): State>, - Extension(settings): Extension, + Extension(settings): Extension, ) -> Response { let state = Arc::clone(&arc_state); - state.templating_engine.render_view( - settings.format, + &settings, &View::Message("Hello! There, You, Awesome Creature!".to_string()) ).await } async fn handle_default_route( State(arc_state): State>, - Extension(settings): Extension, + Extension(settings): Extension, SecureClientIp(address): SecureClientIp ) -> Response { @@ -287,21 +283,21 @@ async fn handle_default_route( let result = get_ip_result(&ip_query, &settings.lang, &state).await; state.templating_engine.render_view( - settings.format, + &settings, &View::Index{query: ip_query, result: result} ).await } async fn handle_ip_route( Query(ip_query): Query, - Extension(settings): Extension, + Extension(settings): Extension, State(arc_state): State>, ) -> Response { return handle_ip_request(ip_query, settings, arc_state).await } async fn handle_ip_route_with_path( - Extension(settings): Extension, + Extension(settings): Extension, State(arc_state): State>, extract::Path(address): extract::Path, ) -> Response { @@ -312,7 +308,7 @@ async fn handle_ip_route_with_path( async fn handle_ip_request( ip_query: IpQuery, - settings: QuerySettings, + settings: TemplateSettings, arc_state: Arc, ) -> Response { @@ -320,7 +316,7 @@ async fn handle_ip_request( let result = get_ip_result(&ip_query, &settings.lang, &state).await; state.templating_engine.render_view( - settings.format, + &settings, &View::Ip{query: ip_query, result: result} ).await } @@ -383,14 +379,14 @@ async fn get_ip_result( async fn handle_dig_route( Query(dig_query): Query, - Extension(settings): Extension, + Extension(settings): Extension, State(arc_state): State>, ) -> Response { return handle_dig_request(dig_query, settings, arc_state).await } async fn handle_dig_route_with_path( - Extension(settings): Extension, + Extension(settings): Extension, State(arc_state): State>, extract::Path(name): extract::Path, ) -> Response { @@ -401,7 +397,7 @@ async fn handle_dig_route_with_path( async fn handle_dig_request( dig_query: DigQuery, - settings: QuerySettings, + settings: TemplateSettings, arc_state: Arc, ) -> Response { @@ -410,7 +406,7 @@ async fn handle_dig_request( let dig_result = get_dig_result(&dig_query, &state).await; state.templating_engine.render_view( - settings.format, + &settings, &View::Dig{ query: dig_query, result: dig_result} ).await diff --git a/src/templating_engine.rs b/src/templating_engine.rs index dd7668a..ab7cf1f 100644 --- a/src/templating_engine.rs +++ b/src/templating_engine.rs @@ -40,6 +40,14 @@ impl ToString for ResponseFormat { } } +/* Template Settings */ + +#[derive(serde::Deserialize, serde::Serialize, Clone)] +pub struct TemplateSettings { + pub format: ResponseFormat, + pub lang: String, +} + /* The echoip view */ #[derive(serde::Deserialize, serde::Serialize, Clone)] @@ -76,17 +84,18 @@ pub struct Engine { impl Engine { pub async fn render_view( &self, - format: ResponseFormat, + settings: &TemplateSettings, view: &View, ) -> Response { - match format { + match settings.format { ResponseFormat::TextHtml => { let template_name = view.template_name(); let mut context = tera::Context::new(); context.insert("view", &template_name); //intented for shared macros - context.insert("format", &format.to_string()); + context.insert("format", &settings.format.to_string()); + context.insert("language", &settings.lang); context.insert("data", &view); context.insert("extra", &self.template_config);