Made the query settings template settings

This commit is contained in:
Slatian 2023-02-21 00:21:31 +01:00
parent 52ace5f61f
commit b9fc472120
2 changed files with 30 additions and 25 deletions

View File

@ -37,8 +37,11 @@ use crate::geoip::QueryLocation;
use geoip::AsnResult; use geoip::AsnResult;
use geoip::LocationResult; use geoip::LocationResult;
use crate::templating_engine::View; use crate::templating_engine::{
use crate::templating_engine::ResponseFormat; View,
ResponseFormat,
TemplateSettings,
};
use crate::ipinfo::{AddressCast,AddressInfo,AddressScope}; use crate::ipinfo::{AddressCast,AddressInfo,AddressScope};
@ -48,12 +51,6 @@ pub struct BaseQuery {
lang: Option<String>, lang: Option<String>,
} }
#[derive(serde::Deserialize, serde::Serialize, Clone)]
pub struct QuerySettings {
format: ResponseFormat,
lang: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone)] #[derive(serde::Deserialize, serde::Serialize, Clone)]
pub struct IpQuery { pub struct IpQuery {
ip: IpAddr, ip: IpAddr,
@ -251,7 +248,7 @@ async fn format_and_language_middleware<B>(
next: Next<B> next: Next<B>
) -> Response { ) -> Response {
let format = query.format.unwrap_or(ResponseFormat::TextHtml); let format = query.format.unwrap_or(ResponseFormat::TextHtml);
req.extensions_mut().insert(QuerySettings{ req.extensions_mut().insert(TemplateSettings{
format: format, format: format,
lang: query.lang.unwrap_or("en".to_string()), lang: query.lang.unwrap_or("en".to_string()),
}); });
@ -261,20 +258,19 @@ async fn format_and_language_middleware<B>(
#[axum::debug_handler] #[axum::debug_handler]
async fn hello_world_handler( async fn hello_world_handler(
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
Extension(settings): Extension<QuerySettings>, Extension(settings): Extension<TemplateSettings>,
) -> Response { ) -> Response {
let state = Arc::clone(&arc_state); let state = Arc::clone(&arc_state);
state.templating_engine.render_view( state.templating_engine.render_view(
settings.format, &settings,
&View::Message("Hello! There, You, Awesome Creature!".to_string()) &View::Message("Hello! There, You, Awesome Creature!".to_string())
).await ).await
} }
async fn handle_default_route( async fn handle_default_route(
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
Extension(settings): Extension<QuerySettings>, Extension(settings): Extension<TemplateSettings>,
SecureClientIp(address): SecureClientIp SecureClientIp(address): SecureClientIp
) -> Response { ) -> Response {
@ -287,21 +283,21 @@ async fn handle_default_route(
let result = get_ip_result(&ip_query, &settings.lang, &state).await; let result = get_ip_result(&ip_query, &settings.lang, &state).await;
state.templating_engine.render_view( state.templating_engine.render_view(
settings.format, &settings,
&View::Index{query: ip_query, result: result} &View::Index{query: ip_query, result: result}
).await ).await
} }
async fn handle_ip_route( async fn handle_ip_route(
Query(ip_query): Query<IpQuery>, Query(ip_query): Query<IpQuery>,
Extension(settings): Extension<QuerySettings>, Extension(settings): Extension<TemplateSettings>,
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
) -> Response { ) -> Response {
return handle_ip_request(ip_query, settings, arc_state).await return handle_ip_request(ip_query, settings, arc_state).await
} }
async fn handle_ip_route_with_path( async fn handle_ip_route_with_path(
Extension(settings): Extension<QuerySettings>, Extension(settings): Extension<TemplateSettings>,
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
extract::Path(address): extract::Path<IpAddr>, extract::Path(address): extract::Path<IpAddr>,
) -> Response { ) -> Response {
@ -312,7 +308,7 @@ async fn handle_ip_route_with_path(
async fn handle_ip_request( async fn handle_ip_request(
ip_query: IpQuery, ip_query: IpQuery,
settings: QuerySettings, settings: TemplateSettings,
arc_state: Arc<ServiceSharedState>, arc_state: Arc<ServiceSharedState>,
) -> Response { ) -> Response {
@ -320,7 +316,7 @@ async fn handle_ip_request(
let result = get_ip_result(&ip_query, &settings.lang, &state).await; let result = get_ip_result(&ip_query, &settings.lang, &state).await;
state.templating_engine.render_view( state.templating_engine.render_view(
settings.format, &settings,
&View::Ip{query: ip_query, result: result} &View::Ip{query: ip_query, result: result}
).await ).await
} }
@ -383,14 +379,14 @@ async fn get_ip_result(
async fn handle_dig_route( async fn handle_dig_route(
Query(dig_query): Query<DigQuery>, Query(dig_query): Query<DigQuery>,
Extension(settings): Extension<QuerySettings>, Extension(settings): Extension<TemplateSettings>,
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
) -> Response { ) -> Response {
return handle_dig_request(dig_query, settings, arc_state).await return handle_dig_request(dig_query, settings, arc_state).await
} }
async fn handle_dig_route_with_path( async fn handle_dig_route_with_path(
Extension(settings): Extension<QuerySettings>, Extension(settings): Extension<TemplateSettings>,
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
extract::Path(name): extract::Path<String>, extract::Path(name): extract::Path<String>,
) -> Response { ) -> Response {
@ -401,7 +397,7 @@ async fn handle_dig_route_with_path(
async fn handle_dig_request( async fn handle_dig_request(
dig_query: DigQuery, dig_query: DigQuery,
settings: QuerySettings, settings: TemplateSettings,
arc_state: Arc<ServiceSharedState>, arc_state: Arc<ServiceSharedState>,
) -> Response { ) -> Response {
@ -410,7 +406,7 @@ async fn handle_dig_request(
let dig_result = get_dig_result(&dig_query, &state).await; let dig_result = get_dig_result(&dig_query, &state).await;
state.templating_engine.render_view( state.templating_engine.render_view(
settings.format, &settings,
&View::Dig{ query: dig_query, result: dig_result} &View::Dig{ query: dig_query, result: dig_result}
).await ).await

View File

@ -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 */ /* The echoip view */
#[derive(serde::Deserialize, serde::Serialize, Clone)] #[derive(serde::Deserialize, serde::Serialize, Clone)]
@ -76,17 +84,18 @@ pub struct Engine {
impl Engine { impl Engine {
pub async fn render_view( pub async fn render_view(
&self, &self,
format: ResponseFormat, settings: &TemplateSettings,
view: &View, view: &View,
) -> Response { ) -> Response {
match format { match settings.format {
ResponseFormat::TextHtml => { ResponseFormat::TextHtml => {
let template_name = view.template_name(); let template_name = view.template_name();
let mut context = tera::Context::new(); let mut context = tera::Context::new();
context.insert("view", &template_name); context.insert("view", &template_name);
//intented for shared macros //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("data", &view);
context.insert("extra", &self.template_config); context.insert("extra", &self.template_config);